1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: Service.java,v 1.2 2005/08/30 04:56:14 tanderson Exp $
44 */
45 package org.exolab.jms.service;
46
47
48 /***
49 * <code>Service</code> is an implementation of the {@link Serviceable}
50 * interface that provides default implementations for the {@link #start} and
51 * {@link #stop} methods.
52 *
53 * @version $Revision: 1.2 $ $Date: 2005/08/30 04:56:14 $
54 * @author <a href="mailto:jima@comware.com.au">Jim Alateras</a>
55 * @see Serviceable
56 */
57 public abstract class Service implements Serviceable {
58
59 /***
60 * The name of the service. May be <code>null</code>.
61 */
62 private final String _name;
63
64 /***
65 * Determines if the service is started.
66 */
67 private volatile boolean _started = false;
68
69
70 /***
71 * Construct a new <code>Service</code>, with no name.
72 */
73 public Service() {
74 this(null);
75 }
76
77 /***
78 * Construct a new <code>Service</code>, specifying its name.
79 *
80 * @param name the name of the service. May be <code>null</code>
81 */
82 protected Service(String name) {
83 _name = name;
84 }
85
86 /***
87 * Start the service.
88 *
89 * @throws ServiceException if the service fails to start, or is already
90 * started
91 */
92 public void start() throws ServiceException {
93 if (_started) {
94 throw new ServiceException("Service already started");
95 }
96
97 doStart();
98
99 _started = true;
100 }
101
102 /***
103 * Stop the service.
104 *
105 * @throws ServiceException if the service fails to stop, or is already
106 * stopped
107 */
108 public void stop() throws ServiceException {
109 if (!_started) {
110 throw new ServiceException("Service not started");
111 }
112 doStop();
113
114 _started = false;
115 }
116
117 /***
118 * Convenience method for restarting the service. This operation can
119 * be called regardless the current state of the service.
120 *
121 * @throws ServiceException if the service fails to restart
122 */
123 public void restart() throws ServiceException {
124 if (_started) {
125 stop();
126 }
127 start();
128 }
129
130 /***
131 * Determines if this service is started.
132 *
133 * @return <code>true</code> if the service is started;
134 * otherwise <code>false</code>
135 */
136 public boolean isStarted() {
137 return _started;
138 }
139
140 /***
141 * Return the name of the service.
142 *
143 * @return the service name, or <code>null</code> if none was set.
144 */
145 public String getName() {
146 return _name;
147 }
148
149 /***
150 * Return the state of the object as a string.
151 *
152 * @return a string form of the object state
153 */
154 public String toString() {
155 StringBuffer buf = new StringBuffer("Service:[");
156 buf.append("name=");
157 buf.append(_name);
158 buf.append("started=");
159 buf.append(_started);
160 buf.append("]");
161 return buf.toString();
162 }
163
164 /***
165 * Start the service.
166 *
167 * @throws ServiceException if the service fails to start
168 */
169 protected void doStart() throws ServiceException {
170 }
171
172 /***
173 * Stop the service.
174 *
175 * @throws ServiceException if the service fails to stop
176 */
177 protected void doStop() throws ServiceException {
178 }
179
180 }