KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > service > Service


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.1 2004/11/26 01:51:01 tanderson Exp $
44  */

45 package org.exolab.jms.service;
46
47
48 /**
49  * Service is an implementation of the {@link Servicable} interface that
50  * provides default implementations for the {@link #start} and {@link #stop}
51  * methods.
52  *
53  * @version $Revision: 1.1 $ $Date: 2004/11/26 01:51:01 $
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
61      */

62     private String JavaDoc _name = null;
63
64     /**
65      * The state of the service
66      */

67     private volatile ServiceState _state = ServiceState.STOPPED;
68
69     /**
70      * The default constructor for the Service does nothing.
71      */

72     protected Service() {
73     }
74
75     /**
76      * Instantiates a service with the specified name
77      *
78      * @param name the unique name of the service
79      */

80     protected Service(String JavaDoc name) {
81         _name = name;
82     }
83
84     /**
85      * Start the service.
86      *
87      * @throws ServiceException if the service fails to start, or is already
88      * running
89      */

90     public synchronized void start() throws ServiceException {
91         if (_state.isStopped()) {
92             _state = ServiceState.RUNNING;
93         } else {
94             throw new ServiceException("Failed to start service " + this);
95         }
96     }
97
98     /**
99      * Stop the service.
100      *
101      * @throws ServiceException if the service fails to stop, or is already
102      * stopped
103      */

104     public synchronized void stop() throws ServiceException {
105         if (_state.isRunning()) {
106             _state = ServiceState.STOPPED;
107         } else {
108             throw new ServiceException("Failed to stop service " + this);
109         }
110     }
111
112     /**
113      * Convenience method for restarting the service. This operation can
114      * be called regardless the current state of the service.
115      *
116      * @throws ServiceException if the service fails to restart
117      */

118     public synchronized void restart() throws ServiceException {
119         if (_state.isRunning()) {
120             stop();
121         }
122         start();
123     }
124
125     /**
126      * Return the current state of the service.
127      *
128      * @return the current state of the service
129      */

130     public ServiceState getState() {
131         return _state;
132     }
133
134     /**
135      * Return the name of the service.
136      *
137      * @return the service name
138      */

139     public String JavaDoc getName() {
140         return _name;
141     }
142
143     /**
144      * Return the state of the object as a string
145      *
146      * @return a string form of the object state
147      */

148     public String JavaDoc toString() {
149         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Service:[");
150         buf.append("name=");
151         buf.append(_name);
152         buf.append("state=");
153         buf.append(_state);
154         buf.append("]");
155         return buf.toString();
156     }
157
158     /**
159      * Set the state of the service
160      *
161      * @param state the service state
162      */

163     protected void setState(ServiceState state) {
164         _state = state;
165     }
166     
167 }
168
Popular Tags