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