View Javadoc

1    /****************************************************************************
2    * Copyright (c) 2005, 2006, 2007, 2008, 2009 Imola Informatica.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the LGPL License v2.1
5    * which accompanies this distribution, and is available at
6    * http://www.gnu.org/licenses/lgpl.html
7    ****************************************************************************/
8   /*
9    * RuntimeContext.java
10   *
11   */
12  
13  package it.imolinfo.jbi4corba.jbi.component.runtime;
14  
15  import java.util.MissingResourceException;
16  import java.util.logging.Logger;
17  import javax.jbi.JBIException;
18  import javax.jbi.component.ComponentContext;
19  import javax.jbi.messaging.DeliveryChannel;
20  import javax.jbi.messaging.MessageExchange;
21  import javax.jbi.messaging.MessagingException;
22  
23  /**
24   * This class is used to store and retrieve the information that
25   * should be available anywhere in the component runtime. Each instance variable
26   * of this class will be initialized at various points of the component runtime using
27   * setter methods on this class. For example, the ComponentContext and related resources
28   * from this class are not available to the component runtime until the jbi framework calls
29   * the init method of the ComponentLifeCycle of this component.
30   *
31   * @author <a href="mailto:mpiraccini@imolinfo.it">Marco Piraccini</a>
32   */
33  public class RuntimeContext {
34      
35      private static RuntimeContext sRuntimeContext;
36      
37      /** Creates a new instance of RuntimeContext */
38      private RuntimeContext() {
39      }
40      
41      public static RuntimeContext getInstance() {
42          if ( sRuntimeContext == null ) {
43              synchronized (RuntimeContext.class) {
44                  if ( sRuntimeContext == null ) {
45                      sRuntimeContext = new RuntimeContext();
46                  }
47              }
48          }
49          return sRuntimeContext;
50      }
51      
52      /**
53       * Holds value of property ComponentContext.
54       */
55      private ComponentContext mComponentContext;
56      
57      /**
58       * Getter for property mComponentContext.
59       *
60       * @return Value of property mComponentContext.
61       */
62      public ComponentContext getComponentContext() {
63          return this.mComponentContext;
64      }
65      
66      /**
67       * Setter for property ComponentContext.
68       *
69       * @param componentContext New value of property ComponentContext.
70       */
71      public void setComponentContext(ComponentContext componentContext) {
72          this.mComponentContext = componentContext;        
73          this.mDeliveryChannel = null;
74      }
75      
76      /**
77       * Holds value of property DeliveryChannel.
78       */
79      private DeliveryChannel mDeliveryChannel;
80      
81      /**
82       * Getter for property DeliveryChannel.
83       *
84       * @return Value of property DeliveryChannel.
85       */
86      public DeliveryChannel getDeliveryChannel() {
87          return this.mDeliveryChannel;
88      }
89      
90      /**
91       * opens the delivery channel to accept the message exchange objects or 
92       * send the message exchange objects
93       */
94      public void openDeliveryChannel() {
95          try {
96              // open the delivery channel from the component context
97              ComponentContext compCtx = getComponentContext();
98              this.mDeliveryChannel = compCtx.getDeliveryChannel();
99          } catch (MessagingException ex) {
100             RuntimeHelper.logDebug(ex);
101         } catch ( Exception ex) {
102             RuntimeHelper.logDebug(ex);
103         }
104     }
105     /**
106      * closes the delivery channel as part of the component shutdown process.
107      */
108     public void closeDeliveryChannel() {
109         try {
110             // closes delivery channel
111             if ( this.mDeliveryChannel != null ) {
112                 this.mDeliveryChannel.close();
113             }
114         } catch (MessagingException ex) {
115             RuntimeHelper.logDebug(ex);
116         } finally {
117             // clear channel in the runtime context
118             this.mDeliveryChannel = null;
119         }
120     }
121         
122     /** default logger*/
123     private Logger mDefLogger;
124     /**
125      * returns the default Logger for the component runtime.
126      */
127     private Logger getDefaultLogger() {        
128         if ( mDefLogger == null ) {
129            this.mDefLogger = Logger.getLogger(RuntimeContext.class.getName(), null); 
130         }
131         return this.mDefLogger;
132     }
133     
134     /**
135      * Logger object.
136      */
137     private Logger mLogger;
138     
139     /**
140      * Sets the logger.
141      *
142      * @param name name for the Logger.
143      * @param resourceBundle resource bundle for the logger. can be null.
144      */
145     public void setLogger(String name, String resourceBundle) {
146         
147         if (this.mComponentContext != null) {
148             // get the logger from component context if the component context is not null
149             try {
150                 this.mLogger = this.mComponentContext.getLogger(name, resourceBundle);
151             } catch (MissingResourceException ex) {
152                 ex.printStackTrace();
153             } catch (JBIException ex) {
154                 ex.printStackTrace();
155             }
156         } else {
157             this.mLogger = Logger.getLogger(name, resourceBundle);
158         }
159     }
160     
161     /**
162      * Returns the logger.
163      *
164      * @return Logger
165      */
166     public Logger  getLogger() {
167         
168         if (this.mLogger == null) {
169             // if nobody set the logger, then return the default 
170             // logger 
171             return getDefaultLogger();
172         }
173         return this.mLogger;
174     }
175         
176     /**
177      * Returns the Component Name if the ComponentContext is set. else null
178      * @return component name
179      */
180     public String getComponentName() {
181         String componentName = null;
182         
183         if (this.mComponentContext != null) {
184             componentName = this.mComponentContext.getComponentName();
185         }        
186         return componentName;
187     }
188     
189     /**
190      * Holds value of property MessageExchangeHandlerFactory.
191      */
192     private MessageExchangeHandlerFactory mMEHandlerFactory;
193     
194     /**
195      * Getter for property MessageExchangeHandlerFactory.
196      *
197      * @return Value of property MessageExchangeHandlerFactory.
198      */
199     public MessageExchangeHandlerFactory getMessageExchangeHandlerFactory() {
200         return this.mMEHandlerFactory;
201     }
202     
203     /**
204      * Setter for property MessageExchangeHandlerFactory.
205      *
206      * @param componentContext New value of property MessageExchangeHandlerFactory.
207      */
208     public void setMessageExchangeHandlerFactory(MessageExchangeHandlerFactory factory) {
209         this.mMEHandlerFactory = factory;
210     }
211     /**
212      * helper method to create a new message exchange handler using the message exchange
213      * factory set on the runtime context.
214      */
215     public MessageExchangeHandler newMessageExchangeHandler(MessageExchange msgExchange) {
216         MessageExchangeHandlerFactory factory = getMessageExchangeHandlerFactory();
217         if ( factory == null ) {
218             return null;
219         }
220         return factory.newHandler(msgExchange);
221     }
222 }