KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > jbi > component > lifecycle > LifeCycleAbstract


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  * --------------------------------------------------------------------------
22  * $Id: LifeCycle.java,v 1.2 2005/07/22 10:24:27 alouis Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.petals.jbi.component.lifecycle;
27
28 import javax.jbi.JBIException;
29 import javax.management.AttributeChangeNotification JavaDoc;
30 import javax.management.ListenerNotFoundException JavaDoc;
31 import javax.management.MBeanNotificationInfo JavaDoc;
32 import javax.management.Notification JavaDoc;
33 import javax.management.NotificationBroadcaster JavaDoc;
34 import javax.management.NotificationBroadcasterSupport JavaDoc;
35 import javax.management.NotificationFilter JavaDoc;
36 import javax.management.NotificationListener JavaDoc;
37 import javax.management.ObjectName JavaDoc;
38
39 import org.objectweb.petals.jbi.component.event.StateChangeFailedEvent;
40 import org.objectweb.petals.jbi.component.event.StateChangedEvent;
41 import org.objectweb.petals.util.LoggingUtil;
42 import org.objectweb.petals.util.PetalsRuntimeException;
43
44 /**
45  * Implementation of the <code>LifeCycle</code> mbean. This lifecycle mbean
46  * sends notifications about changes of state.
47  *
48  * @author Adrien LOUIS - EBM WebSourcing
49  */

50 public abstract class LifeCycleAbstract implements LifeCycleMBean,
51         NotificationBroadcaster JavaDoc {
52
53     protected Object JavaDoc activitySynchronizer;
54
55     protected boolean initialized;
56
57     protected LoggingUtil log;
58
59     protected ObjectName JavaDoc mBeanName;
60
61     protected long sequenceNumber;
62
63     protected String JavaDoc state;
64
65     NotificationBroadcasterSupport JavaDoc notifSupport = new NotificationBroadcasterSupport JavaDoc();
66
67     public LifeCycleAbstract(ObjectName JavaDoc mBeanName, LoggingUtil log) {
68         this.log = log;
69
70         log.call();
71
72         activitySynchronizer = new Object JavaDoc();
73
74         state = SHUTDOWN;
75
76         initialized = false;
77
78         this.mBeanName = mBeanName;
79
80     }
81
82     public void addNotificationListener(NotificationListener JavaDoc arg0,
83             NotificationFilter JavaDoc arg1, Object JavaDoc arg2)
84         throws IllegalArgumentException JavaDoc {
85         notifSupport.addNotificationListener(arg0, arg1, arg2);
86     }
87
88     public abstract void doInit() throws JBIException;
89
90     public abstract void doShutdown() throws JBIException;
91
92     // ////////////////////////////////////////////
93
// LifeCycle implementation
94
// ////////////////////////////////////////////
95

96     public abstract void doStart() throws JBIException;
97
98     public abstract void doStop() throws JBIException;
99
100     /**
101      * @see javax.jbi.management.LifeCycleMBean#getCurrentState()
102      */

103     public String JavaDoc getCurrentState() {
104         if (!(LifeCycleMBean.SHUTDOWN.equals(state)
105                 || LifeCycleMBean.STARTED.equals(state)
106                 || LifeCycleMBean.STOPPED.equals(state) || LifeCycleMBean.UNKNOWN
107                 .equals(state))) {
108             throw new PetalsRuntimeException(
109                     "State '" + state + "' isn't defined by JBI specification !");
110         }
111         return state;
112     }
113
114     //
115
// Notifications support
116
//
117

118     public ObjectName JavaDoc getMBeanName() {
119         return mBeanName;
120     }
121
122     public MBeanNotificationInfo JavaDoc[] getNotificationInfo() {
123         String JavaDoc[] types = new String JavaDoc[] {AttributeChangeNotification.ATTRIBUTE_CHANGE};
124
125         String JavaDoc name = AttributeChangeNotification JavaDoc.class.getName();
126
127         String JavaDoc description = "Notification about a change of the lifeCycle state or a state change failure.";
128
129         MBeanNotificationInfo JavaDoc info = new MBeanNotificationInfo JavaDoc(types, name,
130                 description);
131
132         return new MBeanNotificationInfo JavaDoc[] {info};
133     }
134
135     public void removeNotificationListener(NotificationListener JavaDoc arg0)
136         throws ListenerNotFoundException JavaDoc {
137         notifSupport.removeNotificationListener(arg0);
138     }
139
140     public void setMBeanName(ObjectName JavaDoc beanName) {
141         mBeanName = beanName;
142     }
143
144     // ////////////////////////////////////////////
145
// ElementListener implementation
146
// ////////////////////////////////////////////
147

148     /**
149      * Set the <code>State</code> reference of the current state. An
150      * <code>LifeCycleStateEventAbstract</code> is thrown.
151      *
152      * @param state
153      */

154     public synchronized void setState(String JavaDoc state) throws JBIException {
155         stateChanged(new StateChangedEvent(this, this.state, state));
156
157         this.state = state;
158     }
159
160     /**
161      * Shutdown the component. A notification will be thrown in case of
162      * runtimeException or incoherent state.
163      *
164      * @see javax.jbi.management.LifeCycleMBean#shutdown()
165      * @throws JBIException
166      * in case of runtimeException or incoherent state.
167      */

168     public void shutDown() throws JBIException {
169         log.start();
170
171         if (STOPPED.equals(state)) {
172             synchronized (activitySynchronizer) {
173                 try {
174                     setState(SHUTDOWNING);
175
176                     doShutdown();
177                     
178                     initialized = false;
179
180                     setState(SHUTDOWN);
181                 } catch (Throwable JavaDoc re) {
182                     setState(UNKNOWN);
183
184                     String JavaDoc msg = "An exception occured during the shutdown.";
185
186                     JBIException e = new JBIException(msg, re);
187
188                     log.error(msg, e);
189
190                     stateChangeFailed(new StateChangeFailedEvent(this, e));
191
192                     throw e;
193                 }
194             }
195         } else {
196             String JavaDoc msg = "The Object can not be shutdowned in this state.";
197
198             JBIException e = new JBIException(msg);
199
200             log.error(msg, e);
201
202             stateChangeFailed(new StateChangeFailedEvent(this, e));
203
204             throw e;
205         }
206         log.end();
207     }
208
209     /**
210      * Start the component. A notification will be thrown in case of
211      * runtimeException or incoherent state.
212      *
213      * @see javax.jbi.management.LifeCycleMBean#start()
214      * @throws JBIException
215      * in case of runtimeException or incoherent state.
216      */

217     public void start() throws JBIException {
218         log.start();
219
220         if (SHUTDOWN.equals(state) || STOPPED.equals(state)) {
221             synchronized (activitySynchronizer) {
222                 try {
223                     if (!initialized) {
224                         setState(INITIALIZING);
225
226                         doInit();
227
228                         initialized = true;
229                     }
230                     setState(STARTING);
231
232                     doStart();
233
234                     setState(STARTED);
235                 } catch (Throwable JavaDoc re) {
236                     setState(UNKNOWN);
237
238                     String JavaDoc msg = "An exception occured during the start.";
239
240                     JBIException e = new JBIException(msg, re);
241
242                     log.error(msg, e);
243
244                     stateChangeFailed(new StateChangeFailedEvent(this, e));
245
246                     throw e;
247                 }
248             }
249         } else {
250             String JavaDoc msg = "The Object can not be started in this state.";
251
252             JBIException e = new JBIException(msg);
253
254             log.error(msg, e);
255
256             stateChangeFailed(new StateChangeFailedEvent(this, e));
257
258             throw e;
259         }
260         log.end();
261     }
262
263     /**
264      * <i>stateChanged</i> is called when the state of a lifecycle element
265      * changes. A JMX-notification is thrown.
266      *
267      * @see petals.management.element.ElementStateListener#stateChanged(java.util.EventObject)
268      */

269     public synchronized void stateChanged(StateChangedEvent event) {
270         log.call();
271         if (mBeanName != null) {
272             Notification JavaDoc n = new AttributeChangeNotification JavaDoc(mBeanName,
273                     sequenceNumber++, System.currentTimeMillis(), "State of '"
274                             + mBeanName.toString() + "' changed : "
275                             + event.getOldState() + " -> "
276                             + event.getNewState(), "state", "String", event
277                             .getOldState(), event.getNewState());
278
279             notifSupport.sendNotification(n);
280         }
281     }
282
283     /**
284      * <i>stateChangeFailed</i>> is called when the change of state of a
285      * lifecycle element fails. A JMX-notification throws the relative
286      * JBIException, .
287      *
288      * @see petals.management.element.ElementStateListener#stateChanged(java.util.EventObject)
289      */

290     public synchronized void stateChangeFailed(StateChangeFailedEvent event) {
291         log.call();
292         if (mBeanName != null) {
293             Notification JavaDoc n = new AttributeChangeNotification JavaDoc(mBeanName,
294                     sequenceNumber++, System.currentTimeMillis(),
295                     "State change of '" + mBeanName.toString() + "' failed : "
296                             + event.getException().getMessage(), "exception",
297                     "String", null, event.getException().toString());
298
299             notifSupport.sendNotification(n);
300         }
301     }
302
303     /**
304      * Stop the component. A notification will be thrown in case of
305      * runtimeException or incoherent state.
306      *
307      * @see javax.jbi.management.LifeCycleMBean#stop()
308      * @throws JBIException
309      * in case of runtimeException or incoherent state.
310      */

311     public void stop() throws JBIException {
312         log.start();
313
314         if (STARTED.equals(state)) {
315             synchronized (activitySynchronizer) {
316                 try {
317
318                     setState(STOPPING);
319
320                     doStop();
321
322                     setState(STOPPED);
323
324                 } catch (Throwable JavaDoc re) {
325                     setState(UNKNOWN);
326
327                     String JavaDoc msg = "An exception occured during the stop.";
328
329                     JBIException e = new JBIException(msg, re);
330
331                     log.error(msg, e);
332
333                     stateChangeFailed(new StateChangeFailedEvent(this, e));
334
335                     throw e;
336                 }
337             }
338         } else {
339             String JavaDoc msg = "The Object can not be stoped in this state." + state;
340
341             JBIException e = new JBIException(msg);
342
343             log.error(msg, e);
344
345             stateChangeFailed(new StateChangeFailedEvent(this, e));
346
347             throw e;
348         }
349         log.end();
350     }
351
352 }
353
Popular Tags