KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > BarrierController


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.system;
23
24 import javax.management.Notification JavaDoc;
25 import javax.management.ObjectName JavaDoc;
26
27 /**
28  * BarrierController service.
29  *
30  * A service that controls the lifecycle of a secondary mbean
31  * (the BarrierMbean) that can be used as a dependency for other
32  * services.
33  *
34  * Starting and stopping the barrier mbean (and as a result
35  * all services depending on it) is performed by listening
36  * for any kind of JMX notification. In particular we use
37  * the handback object of a notification subscription to
38  * qualify the start and stop signals.
39  *
40  * Manual control of the barrier is also supported through
41  * startBarrier()/stopBarrier() methods.
42  *
43  * You may subclass BarrierController and override enableOnStartup()
44  * to apply complex logic in deciding whether to initially start
45  * the barrier (e.g. query some other mbean).
46  *
47  * @jmx:mbean
48  * extends="org.jboss.system.ListenerServiceMBean"
49  * name="jboss:service=BarrierController"
50  *
51  * @author <a HREF="dimitris@jboss.org">Dimitris Andreadis</a>
52  * @author Scott.Stark@jboss.org
53  * @version $Revision: 58545 $
54  */

55 public class BarrierController extends ListenerServiceMBeanSupport
56    implements BarrierControllerMBean
57 {
58    // Private Data --------------------------------------------------
59

60    /** The ObjectName of the Barrier MBean */
61    private ObjectName JavaDoc barrierName;
62
63    /** The initial state of the barrier */
64    private Boolean JavaDoc enableOnStartup;
65    
66    /** The notification subscription handback string that starts the barrier */
67    private String JavaDoc startHandback;
68    
69    /** The notification subscription handback string that stops the barrier */
70    private String JavaDoc stopHandback;
71    
72    /** The dynamic subscriptions flag */
73    private Boolean JavaDoc dynamicSubscriptions;
74    
75    // Protected Data ------------------------------------------------
76

77    /** The controlled Barrier */
78    protected Barrier barrier;
79    
80    // Constructors --------------------------------------------------
81

82    /**
83     * Default CTOR
84     */

85    public BarrierController()
86    {
87       // empty
88
}
89    
90    // Attributes ----------------------------------------------------
91

92    /**
93     * The controlled barrier StateString.
94     *
95     * @jmx.managed-attribute
96     */

97    public String JavaDoc getBarrierStateString()
98    {
99       return (barrier != null) ? barrier.getStateString() : null;
100    }
101    
102    /**
103     * The controlled barrier ObjectName.
104     *
105     * @jmx.managed-attribute
106     */

107    public void setBarrierObjectName(ObjectName JavaDoc barrierName)
108    {
109       // set once
110
if (this.barrierName == null)
111       {
112          this.barrierName = barrierName;
113       }
114    }
115    
116    /**
117     * The controlled barrier ObjectName.
118     *
119     * @jmx.managed-attribute
120     */

121    public ObjectName JavaDoc getBarrierObjectName()
122    {
123       return barrierName;
124    }
125
126
127    /**
128     * The initial state of the barrier.
129     *
130     * If set, it overrides the internal call to enableOnStartup()
131     * which will never get called.
132     *
133     * @jmx.managed-attribute
134     */

135    public void setBarrierEnabledOnStartup(Boolean JavaDoc enableOnStartup)
136    {
137       // set once
138
if (this.enableOnStartup == null)
139       {
140          this.enableOnStartup = enableOnStartup;
141       }
142    }
143
144    /**
145     * The initial state of the barrier.
146     *
147     * Use the value set through setBarrierEnabledOnStartup()
148     * otherwise call the internal enableOnStartup() override
149     * to make a decision.
150     *
151     * @jmx.managed-attribute
152     */

153    public Boolean JavaDoc getBarrierEnabledOnStartup()
154    {
155       if (enableOnStartup == null)
156       {
157          // setBarrierEnabledOnStartup() not called
158
// initialize through enableOnStartup()
159
enableOnStartup = enableOnStartup();
160       }
161       return enableOnStartup;
162    }
163    
164    /**
165     * The notification subscription handback string that starts the barrier.
166     *
167     * @jmx.managed-attribute
168     */

169    public void setStartBarrierHandback(String JavaDoc startHandback)
170    {
171       // set once
172
if (this.startHandback == null)
173       {
174          this.startHandback = startHandback;
175       }
176    }
177
178    /**
179     * The notification subscription handback string that starts the barrier.
180     *
181     * @jmx.managed-attribute
182     */

183    public String JavaDoc getStartBarrierHandback()
184    {
185       return startHandback;
186    }
187
188    /**
189     * The notification subscription handback string that stops the barrier.
190     *
191     * @jmx.managed-attribute
192     */

193    public void setStopBarrierHandback(String JavaDoc stopHandback)
194    {
195       // set once
196
if (this.stopHandback == null)
197       {
198          this.stopHandback = stopHandback;
199       }
200    }
201    
202    /**
203     * The notification subscription handback string that stops the barrier.
204     *
205     * @jmx.managed-attribute
206     */

207    public String JavaDoc getStopBarrierHandback()
208    {
209       return stopHandback;
210    }
211    
212    /**
213     * The ability to dynamically subscribe for notifications.
214     *
215     * @jmx.managed-attribute
216     */

217    public void setDynamicSubscriptions(Boolean JavaDoc dynamicSubscriptions)
218    {
219       // set once
220
if (this.dynamicSubscriptions == null)
221       {
222          this.dynamicSubscriptions = dynamicSubscriptions;
223       }
224    }
225    
226    /**
227     * The ability to dynamically subscribe for notifications.
228     *
229     * @jmx.managed-attribute
230     */

231    public Boolean JavaDoc getDynamicSubscriptions()
232    {
233       if (dynamicSubscriptions == null)
234       {
235          dynamicSubscriptions = Boolean.TRUE;
236       }
237       return dynamicSubscriptions;
238    }
239    
240    // Override ------------------------------------------------------
241

242    /**
243     * Override this method to apply complex logic whether
244     * to start the Barrier service upon startup or not.
245     *
246     * This method will be only called once and only if
247     * setBarrierEnabledOnStartup(Boolean) has not been called.
248     *
249     * The default implementation is to return false.
250     */

251    protected Boolean JavaDoc enableOnStartup()
252    {
253       return Boolean.FALSE;
254    }
255    
256    // Lifecycle -----------------------------------------------------
257

258    protected void createService() throws Exception JavaDoc
259    {
260       // create the Barrier
261
barrier = new Barrier(getServiceName());
262       
263       initBarrier();
264    }
265    
266    /**
267     * Coordinates between createService() and postRegister(),
268     * registering the barrier when both the barrier is
269     * created and the mbeanserver is available
270     *
271     * @throws Exception
272     */

273    private void initBarrier() throws Exception JavaDoc
274    {
275       if (barrier != null && getServer() != null)
276       {
277          // register with the MBeanServer
278
getServer().registerMBean(barrier, barrierName);
279          
280          // implicitly call the ServiceController
281
barrier.create();
282          
283          // conditionally start the barrier
284
if (getBarrierEnabledOnStartup().booleanValue())
285          {
286             startBarrier();
287          }
288          // subscribe for notifications
289
subscribe(getDynamicSubscriptions().booleanValue());
290       }
291    }
292    
293    protected void destroyService()
294    {
295       // unsubscribe for notifications
296
unsubscribe();
297
298       try
299       {
300          // implicitly call the ServiceController
301
barrier.destroy();
302       
303          // remove from MBeanServer
304
getServer().unregisterMBean(barrierName);
305       }
306       catch (Throwable JavaDoc e)
307       {
308          log.debug("Unexpected error during destroy", e);
309       }
310       
311       // cleanup
312
barrier = null;
313    }
314    
315    // ListenerServiceMBeanSupport -----------------------------------
316

317    /**
318     * Base on the handback object the decision
319     * for starting/stopping the barrier
320     */

321    public void handleNotification2(Notification JavaDoc n, Object JavaDoc handback)
322    {
323       log.debug("Got notification: " + n);
324       
325       if (startHandback != null && startHandback.equals(handback))
326       {
327          log.debug("Saw '" + handback + "' handback, starting barrier");
328          startBarrier();
329       }
330       else if (stopHandback != null && stopHandback.equals(handback))
331       {
332          log.debug("Saw '" + handback + "' handback, stopping barrier");
333          stopBarrier();
334       }
335    }
336    
337    // Operations ----------------------------------------------------
338

339    /**
340     * Manually start the controlled barrier
341     *
342     * @jmx:managed-operation
343     */

344    public void startBarrier()
345    {
346       try
347       {
348          // implicitly call the ServiceController
349
barrier.start();
350       }
351       catch (Throwable JavaDoc e)
352       {
353          log.warn("Failed to start barrier: " + barrierName, e);
354       }
355    }
356
357    /**
358     * Manually stop the controlled barrier
359     *
360     * @jmx:managed-operation
361     */

362    public void stopBarrier()
363    {
364       try
365       {
366          // implicitly call the ServiceController
367
barrier.stop();
368       }
369       catch (Throwable JavaDoc e)
370       {
371          log.warn("Failed to stop barrier: " + barrierName, e);
372       }
373    }
374    
375    // Overrides ---------------------------------------------------
376

377    @Override JavaDoc
378    public void postRegister(Boolean JavaDoc registrationDone)
379    {
380       super.postRegister(registrationDone);
381       
382       if (Boolean.TRUE.equals(registrationDone))
383       {
384          try
385          {
386             initBarrier();
387          }
388          catch (Exception JavaDoc e)
389          {
390             throw new RuntimeException JavaDoc(e);
391          }
392       }
393    }
394    
395    // Inner Class ---------------------------------------------------
396

397    /**
398     * The controlled barrier MBean interface
399     */

400    public static interface BarrierMBean
401    {
402       /** We just want to expose those attributes */
403       ObjectName JavaDoc getBarrierController();
404       String JavaDoc getStateString();
405       int getState();
406       
407       /** Hook up with the ServiceController */
408       void jbossInternalLifecycle(String JavaDoc method) throws Exception JavaDoc;
409    }
410
411    /**
412     * The controlled barrier MBean class
413     */

414    public static class Barrier extends ServiceMBeanSupport implements BarrierMBean
415    {
416       /** The parent Controller */
417       private ObjectName JavaDoc barrierController;
418       
419       /** CTOR */
420       public Barrier(ObjectName JavaDoc barrierController)
421       {
422          this.barrierController = barrierController;
423       }
424       
425       /** Accessor */
426       public ObjectName JavaDoc getBarrierController()
427       {
428          return barrierController;
429       }
430    }
431 }
432
Popular Tags