View Javadoc

1   /*
2    * The contents of this file are subject to the terms
3    * of the Common Development and Distribution License
4    * (the "License").  You may not use this file except
5    * in compliance with the License.
6    *
7    * You can obtain a copy of the license at
8    * https://open-esb.dev.java.net/public/CDDLv1.0.html.
9    * See the License for the specific language governing
10   * permissions and limitations under the License.
11   *
12   * When distributing Covered Code, include this CDDL
13   * HEADER in each file and include the License file at
14   * https://open-esb.dev.java.net/public/CDDLv1.0.html.
15   * If applicable add the following below this CDDL HEADER,
16   * with the fields enclosed by brackets "[]" replaced with
17   * your own identifying information: Portions Copyright
18   * [year] [name of copyright owner]
19   */
20  
21  /*
22   * Copyright 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
23   */
24  
25  /*
26   * AbstractComponentLifeCycle.java
27   *
28   */
29  
30  package it.imolinfo.jbi4ejb.jbi.component.runtime;
31  
32  import it.imolinfo.jbi4ejb.Logger;
33  import it.imolinfo.jbi4ejb.LoggerFactory;
34  import it.imolinfo.jbi4ejb.jbi.Messages;
35  import it.imolinfo.jbi4ejb.jbi.component.Jbi4EjbLifeCycle;
36  
37  import javax.jbi.JBIException;
38  import javax.jbi.component.ComponentContext;
39  import javax.jbi.component.ComponentLifeCycle;
40  import javax.management.MBeanServer;
41  import javax.management.ObjectName;
42  import javax.management.StandardMBean;
43  
44  /**
45   * This class is an abstract implemenation of the ComponentLifeCycle. It implements
46   * the component lifecycle methods and provides necessary hooks to the extended classes
47   * to supply the component specific implemenation for the lifecycle functionality.
48   *
49   * @see javax.jbi.ComponentLifeCycle
50   *
51   * @author Sun Microsystems, Inc.
52   */
53  public abstract class AbstractComponentLifeCycle implements ComponentLifeCycle {
54  	/** The logger. */
55      private static final Logger LOG = LoggerFactory.getLogger( AbstractComponentLifeCycle.class);    
56      private static final Messages MESSAGES = Messages.getMessages( AbstractComponentLifeCycle.class);
57  	
58      /** reference to the component runtime */
59      private ComponentRuntime mCompRuntime;
60      /** Extension Mbean Name*/
61      private ObjectName mExtensionMBeanName;
62      /** Extension Mbean Implemenation*/
63      private StandardMBean mExtensionMBeanImpl;
64      /** a message receiver that accepts messages from delivery channel */
65      private MessageExchangeReceiver mMsgExchReceiver;
66      
67      /** private constructor  */
68      private AbstractComponentLifeCycle() {}
69      /**
70       * constructor for the ComponentLifecycle implementation.
71       * @param compRuntime
72       */
73      protected AbstractComponentLifeCycle(ComponentRuntime compRuntime) {
74          this.mCompRuntime = compRuntime;
75      }
76      
77      ///////////////////////////////////////////////////////////////////////////
78      // ComponentLifeCycle interface implemenation
79      ///////////////////////////////////////////////////////////////////////////
80      
81      /**
82       * Initialize the component.
83       * @param context
84       * @throws javax.jbi.JBIException
85       * @see javax.jbi.component.ComponentLifeCycle#init(javax.jbi.component.ComponentContext)
86       */
87      public final void init(ComponentContext context) throws JBIException {
88          
89          if ( context == null  ) {
90              throw new JBIException("Null Component Context received in " +
91                      "Component Lifecycle init ");
92          }
93          // initialize the content
94          initContext(context);
95          // initialize the message exchange handler factory
96          initMessageExchangeHandlerFactory();
97          // initialize the message receiver
98          initMessageExchangeReceiver();
99          // create and register extension mbean
100         registerExtensionMBean();
101         // call the onInit to allow other initialization that is specific to the component
102         onInit();
103         
104                 LOG.info("EJB000201_Component_initialized", new Object[]{RuntimeHelper.getComponentName()});
105     }
106     
107     /**
108      * Start the component.
109      * @throws javax.jbi.JBIException
110      * @see javax.jbi.component.ComponentLifeCycle#start()
111      */
112     public final void start() throws JBIException {
113         
114         // open the delivery channel from the component context
115         openDeliveryChannel();
116         
117         activateServiceProviders();
118         activateServiceConsumers();
119         
120         startMessageExchangeProcessing();
121         // call onStart to perform component specific tasks on start
122         onStart();
123         //RuntimeHelper.logInfo("Component " + RuntimeHelper.getComponentName() + " started");
124         LOG.info("EJB000202_Component_started", new Object[]{RuntimeHelper.getComponentName()});
125     }
126     
127     /**
128      * Stop the component.
129      * @throws javax.jbi.JBIException
130      * @see javax.jbi.component.ComponentLifeCycle#stop()
131      */
132     public final void stop() throws JBIException {
133         
134         stopMessageExchangeProcessing();
135         // deactivate endpoints
136         deactivateServiceConsumers();
137         deactivateServiceProviders();
138         // close channel. No further messages to be processed.
139         closeDeliveryChannel();
140         // call onStop to perform component specific tasks on stop
141         onStop();
142         LOG.info("EJB000203_Component_stopped", new Object[]{RuntimeHelper.getComponentName()});
143             }
144     
145     /**
146      * Shut down the component.
147      * @throws javax.jbi.JBIException
148      * @see javax.jbi.component.ComponentLifeCycle#shutDown()
149      */
150     public final void shutDown() throws JBIException {
151         // remove the message receiver.
152         shutdownMessageExchangeReceiver();
153         // unregister extension mbean and remove it
154         unregisterExtensionMBean();
155         // call onShutdown to perform component specific tasks on shutdown
156         onShutDown();
157         LOG.info("EJB000204_Component_shut_down", new Object[]{RuntimeHelper.getComponentName()});
158     }
159     
160     /**
161      * this is a default implementation which does not have any extension mbeans. extended
162      * classes can return the mbean name for the extension mbeans if the component supports.
163      *
164      *
165      * @see javax.jbi.component.ComponentLifeCycle#getExtensionMBeanName()
166      * @return
167      */
168     public final ObjectName getExtensionMBeanName() {
169         return this.mExtensionMBeanName;
170     }
171     /**
172      * if there is an extension mbean supported by the component, then register
173      * it with the mbean server.
174      */
175     private void registerExtensionMBean()  throws JBIException {
176         
177         this.mExtensionMBeanName = this.createExtensionMBeanName();
178         
179         if ( this.mExtensionMBeanName == null ) {
180             RuntimeHelper.logDebug("No Extension MBean is registerd with MBeanServer for " +
181                     RuntimeHelper.getComponentName());
182             return;
183         }
184         // create the extension mbean implemenation if the object name is created.
185         this.mExtensionMBeanImpl = this.createExtensionMBean();
186         
187         if ( this.mExtensionMBeanImpl == null ) {
188             this.mExtensionMBeanName = null;
189             RuntimeHelper.logDebug("No Extension MBean is registerd with MBeanServer for " +
190                     RuntimeHelper.getComponentName());
191             return;
192         }
193         // register with mbean only if object name and implementation are non null
194         try {
195             MBeanServer mbServer = RuntimeHelper.getComponentContext().getMBeanServer();
196             mbServer.registerMBean(this.mExtensionMBeanImpl, this.mExtensionMBeanName);
197         } catch (Exception e) {
198             this.mExtensionMBeanName = null;
199             this.mExtensionMBeanImpl = null;
200             String msg=MESSAGES.getString("EJB000205_Can_not_register_extension_MBean_with_MBeanServer", new Object[] {RuntimeHelper.getComponentName()});
201             LOG.error(msg,e);
202             throw new JBIException(msg,e);
203 
204         }
205     }
206     /**
207      * remove the registered extension mbean from the mbean server.
208      */
209     private void unregisterExtensionMBean()  throws JBIException {
210         if ( this.mExtensionMBeanName != null ) {
211             try {
212                 MBeanServer mbServer = RuntimeHelper.getComponentContext().getMBeanServer();
213                 mbServer.unregisterMBean(this.mExtensionMBeanName);
214             } catch (Exception e) {
215             	String msg=MESSAGES.getString("EJB000206_Can_not_register_extension_MBean_from_MBeanServer", new Object[] {RuntimeHelper.getComponentName()});
216                 LOG.error(msg,e);
217                 throw new JBIException(msg,e);
218             } finally {
219                 this.mExtensionMBeanName = null;
220                 this.mExtensionMBeanImpl = null;
221             }
222         }
223     }
224     
225     /**
226      *
227      * @return
228      */
229     public final ComponentRuntime getComponentRuntime() {
230         return this.mCompRuntime;
231     }
232     /**
233      * initializes the RuntimeContext using ComponentContext passed by init method.
234      * @param context
235      */
236     private void initContext(ComponentContext context) {
237         RuntimeContext.getInstance().setComponentContext(context);
238         RuntimeContext.getInstance().setLogger(this.getClass().getPackage().getName(), null);
239     }
240     /**
241      * creates a message receiver object as part of the component initialization.
242      * @throws javax.jbi.JBIException
243      */
244     private void initMessageExchangeReceiver() throws JBIException {
245         try {
246             
247             // create message receiver
248             this.mMsgExchReceiver = createMessageExchangeReceiver();
249             if ( this.mMsgExchReceiver != null ) {
250                 this.mMsgExchReceiver.initReceiver();
251             }
252         } catch (Exception ex) {
253         	String msg=MESSAGES.getString("EJB000207_Failure_in__init_Message_Exchange_Receiver");
254             LOG.error(msg,ex);
255             throw new JBIException(msg,ex);
256         }
257     }
258     /**
259      * removes the message receiver as part of the component shutdown process
260      * @throws javax.jbi.JBIException
261      */
262     private void shutdownMessageExchangeReceiver() throws JBIException {
263         try {
264             if ( this.mMsgExchReceiver != null ) {
265                 this.mMsgExchReceiver.shutdownReceiver();
266             }
267         } catch (Exception ex) {
268             ex.printStackTrace();
269         } finally {
270             this.mMsgExchReceiver = null;
271         }
272     }
273     /**
274      * allows the component to accept the message exchange objects from the
275      * delivery channel and process it as part of the component startup process.
276      * @throws javax.jbi.JBIException
277      */
278     private void startMessageExchangeProcessing() throws JBIException {
279         try {
280             // start message processing
281             if ( this.mMsgExchReceiver != null ) {
282                 this.mMsgExchReceiver.startProcessing();
283             }
284         } catch (Exception ex) {
285         	String msg=MESSAGES.getString("EJB000208_Failure_in__start_Message_Exchange_Processing");
286             LOG.error(msg,ex);
287             throw new JBIException(msg,ex);
288         }
289     }
290     /**
291      * stops the component from accepting the message exchange objects from the
292      * delivery channel as part of the component stop process
293      * @throws javax.jbi.JBIException
294      */
295     private void stopMessageExchangeProcessing() throws JBIException {
296         try {
297             // stop message processing
298             if ( this.mMsgExchReceiver != null ) {
299                 this.mMsgExchReceiver.stopProcessing();
300             }
301         } catch (Exception ex) {
302         	String msg=MESSAGES.getString("EJB000209_Failure_in__stop_Message_Exchange_Processing");
303             LOG.error(msg,ex);
304             throw new JBIException(msg,ex);
305         }
306         
307     }
308     /**
309      * opens the delivery channel to accept the message exchange objects or
310      * send the message exchange objects
311      */
312     private void openDeliveryChannel() {
313         RuntimeContext.getInstance().openDeliveryChannel();
314     }
315     /**
316      * closes the delivery channel as part of the component shutdown process.
317      */
318     private void closeDeliveryChannel() {
319         RuntimeContext.getInstance().closeDeliveryChannel();
320     }
321     /**
322      * create jmx object name for the extension mbean
323      */
324     protected abstract ObjectName createExtensionMBeanName();
325     /**
326      * create mbean implemenation for the extension mbean as a StandardMBean
327      */
328     protected abstract StandardMBean createExtensionMBean();
329     /**
330      * initializes the message exchange handler factory. components implement this
331      * method to provide their own handler factory
332      * @throws javax.jbi.JBIException
333      */
334     protected abstract void initMessageExchangeHandlerFactory() throws JBIException; 
335     /**
336      * creates a message receiver object that handles receiving and processing
337      * the message exchanges from the delivery channel. Component should implement
338      * this method to provide the MessageReceiver.
339      *
340      * Component may return null indicating that they don't need the message receiver
341      * that can receive and process message exchanges from delivery channel. For example,
342      * components that have only service consumers which send a synchronous messages to
343      * providers don't need this.
344      * @throws java.lang.Exception
345      * @return
346      */
347     protected abstract MessageExchangeReceiver createMessageExchangeReceiver() throws Exception;
348     /**
349      * service providers initialization such as creating the service descriptions and
350      * activating the service endpoints should be done. If the component supports
351      * deployment, deployment manager should be notified to active service providers
352      * from the deployed service units.
353      * This method is invoked in the implementation of ComponentLifeCycle start
354      * method
355      * @throws javax.jbi.JBIException
356      */
357     protected abstract void activateServiceProviders() throws JBIException;
358     /**
359      * disable the service providers so that the service consumers can not
360      * find the service endpoints for this service provider.
361      * This method is invoked in the implementation of ComponentLifeCycle stop
362      * method
363      * @throws javax.jbi.JBIException
364      */
365     protected abstract void deactivateServiceProviders() throws JBIException;
366     /**
367      * service consumer initialization such as creating the service descriptions and
368      * activating the protocol specific external endpoints should be done. If the component
369      * supports  deployment, deployment manager should be notified to initialize service
370      * consumers from the deployed service units.
371      * This method is invoked in the implementation of ComponentLifeCycle start
372      * method
373      * @throws javax.jbi.JBIException
374      */
375     protected abstract void activateServiceConsumers() throws JBIException;
376     /**
377      * disables the service consumers so that the service consumer can not
378      * send message exchange to the providers. This is called in the implemenation
379      * of component lifecycle stop method
380      * This method is invoked in the implementation of ComponentLifeCycle stop
381      * method
382      * @throws javax.jbi.JBIException
383      */
384     protected abstract void deactivateServiceConsumers() throws JBIException;
385     /**
386      * chance to extended classes to do the component specific init
387      * @throws javax.jbi.JBIException
388      */
389     protected void onInit() throws JBIException {
390         //NOOP
391     }
392     /**
393      * chance to extended classes to do the component specific start
394      * @throws javax.jbi.JBIException
395      */
396     protected void onStart() throws JBIException {
397         //NOOP
398     }
399     /**
400      * chance to extended classes to do the component specific stop
401      * @throws javax.jbi.JBIException
402      */
403     protected void onStop() throws JBIException {
404         //NOOP
405     }
406     /**
407      * chance to extended classes to do the component specific shutdown
408      * @throws javax.jbi.JBIException
409      */
410     protected void onShutDown() throws JBIException {
411         //NOOP
412     }
413     
414 }