001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)JMSBindingLifeCycle.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.binding.jms;
030:
031: import com.sun.jbi.StringTranslator;
032:
033: import com.sun.jbi.binding.jms.config.Config;
034: import com.sun.jbi.binding.jms.config.ConfigConstants;
035:
036: import com.sun.jbi.binding.jms.deploy.EndpointRegistry;
037: import com.sun.jbi.binding.jms.deploy.JMSBindingSUManager;
038:
039: import com.sun.jbi.binding.jms.framework.WorkManager;
040:
041: import com.sun.jbi.binding.jms.mq.MQManager;
042:
043: import java.io.File;
044:
045: import java.util.logging.Logger;
046:
047: import javax.jbi.JBIException;
048:
049: import javax.jbi.component.ComponentContext;
050:
051: import javax.jbi.messaging.DeliveryChannel;
052: import javax.jbi.messaging.MessagingException;
053: import java.net.URI;
054:
055: /**
056: * This is the Lifecycle class of the JMS Binding. The init and the start()
057: * methods of this class are invoked by the framework when the component is
058: * started.
059: *
060: * @author Sun Microsystems Inc.
061: * @version
062: */
063: public class JMSBindingLifeCycle implements
064: javax.jbi.component.ComponentLifeCycle, JMSBindingResources {
065: /**
066: * Compoent context.
067: */
068: private ComponentContext mContext = null;
069:
070: /**
071: * Binding Channel Object.
072: */
073: private DeliveryChannel mChannel;
074:
075: /**
076: * Endpoint manager.
077: */
078: private EndpointManager mManager;
079:
080: /**
081: * Registry.
082: */
083: private EndpointRegistry mRegistry;
084:
085: /**
086: * JMS binding context.
087: */
088: private JMSBindingContext mJMSContext;
089: /**
090: * Resolver object.
091: */
092: private JMSBindingResolver mResolver;
093:
094: /**
095: * Service unit manager.
096: */
097: private JMSBindingSUManager mSUManager = null;
098:
099: /**
100: * Receiver.
101: */
102: private JMSReceiver mJMSReceiver;
103:
104: /**
105: * Logger Object.
106: */
107: private Logger mLogger;
108:
109: /**
110: * Helper for i18n.
111: */
112: private StringTranslator mStringTranslator;
113:
114: /**
115: * Receiver thread.
116: */
117: private Thread mReceiverThread;
118:
119: /**
120: * Flag to hold the result of init.
121: */
122: private boolean mInitSuccess = false;
123:
124: /**
125: * Get the ObjectName for any MBean that is provided for managing this
126: * binding. In this case, there is none, so a null is returned.
127: *
128: * @return ObjectName is always null.
129: */
130: public javax.management.ObjectName getExtensionMBeanName() {
131: return null;
132: }
133:
134: /**
135: * Resolver object.
136: *
137: * @param resolver resolver implementation.
138: */
139: public void setResolver(JMSBindingResolver resolver) {
140: mResolver = resolver;
141: }
142:
143: /**
144: * Sets the su manager.
145: *
146: * @param manager su manager implementation.
147: */
148: public void setSUManager(JMSBindingSUManager manager) {
149: mSUManager = manager;
150: }
151:
152: /**
153: * Initialises the life cycle.
154: *
155: * @param jbiContext Component context.
156: *
157: * @throws javax.jbi.JBIException exception.
158: */
159: public void init(javax.jbi.component.ComponentContext jbiContext)
160: throws javax.jbi.JBIException {
161: Logger logger = jbiContext.getLogger("", null);
162: init(jbiContext, logger);
163: }
164:
165: /**
166: * Initialize the JMS BC. This performs initialization required by the JMS
167: * BC but does not make it ready to process messages. This method is
168: * called immediately after installation of the JMS BC. It is also called
169: * when the JBI framework is starting up, and any time the BC is being
170: * restarted after previously being shut down through a call to
171: * shutdown().
172: *
173: * @param jbiContext the JBI environment context
174: * @param logger logger object.
175: *
176: * @throws javax.jbi.JBIException if the BC is unable to initialize.
177: */
178: public void init(javax.jbi.component.ComponentContext jbiContext,
179: Logger logger) throws javax.jbi.JBIException {
180: mLogger = logger;
181:
182: try {
183: if (null == jbiContext) {
184: throw new javax.jbi.JBIException(mStringTranslator
185: .getString(JMS_INVALID_BINDINGCONTEXT));
186: }
187:
188: mJMSContext = JMSBindingContext.getInstance();
189: mJMSContext.setContext(jbiContext);
190: mJMSContext.setLogger(logger);
191: mContext = jbiContext;
192: mStringTranslator = mJMSContext.getStringTranslator();
193: mLogger.info(mStringTranslator.getString(JMS_INIT_START));
194: mRegistry = new EndpointRegistry();
195:
196: MessageRegistry mMessageRegistry = new MessageRegistry();
197: mJMSContext.setMessageRegistry(mMessageRegistry);
198: mJMSContext.setRegistry(mRegistry);
199: mManager = new EndpointManager();
200: mJMSContext.setEndpointManager(mManager);
201: mSUManager.setValues(mContext, mManager, mResolver);
202:
203: Config config = new Config(getConfigFileName(jbiContext
204: .getInstallRoot()));
205:
206: // Initialize the configuration object
207: config.loadConfiguration(jbiContext.getComponentName());
208:
209: if (!config.isValid()) {
210: mLogger.severe(mStringTranslator
211: .getString(JMS_INVALID_CONIFG_INFO));
212: throw new Exception(mStringTranslator
213: .getString(JMS_INVALID_CONIFG_INFO)
214: + "\n" + config.getError());
215: }
216:
217: mLogger.setLevel(config.getLogLevel());
218: config.printDetails();
219: mJMSContext.setConfig(config);
220: } catch (Exception e) {
221: e.printStackTrace();
222: throw new javax.jbi.JBIException(mStringTranslator
223: .getString(JMS_INIT_FAILED), e);
224: }
225:
226: /* This a guard variable to check if init is a success
227: * Nothing should be done in start in case init is a failure
228: */
229: mInitSuccess = true;
230: }
231:
232: /**
233: * Shutdown the BC. This performs cleanup before the BC is terminated. Once
234: * this has been called, init() must be called before the BC can be
235: * started again with a call to start().
236: *
237: * @throws javax.jbi.JBIException if the BC is unable to shut down.
238: */
239: public void shutDown() throws javax.jbi.JBIException {
240: mLogger.info(mStringTranslator.getString(JMS_SHUTDOWN_BEGIN));
241: mSUManager = null;
242: if (mRegistry != null) {
243: mRegistry.clearEndpoints();
244: }
245: mLogger.info(mStringTranslator.getString(JMS_SHUTDOWN_END));
246: }
247:
248: /**
249: * Start the JMS BC. This makes the JSM BC ready to process messages. This
250: * method is called after init() completes when the JBI framework is
251: * starting up, and when the BC is being restarted after a previous call
252: * to shutdown(). If stop() was called previously but shutdown() was not,
253: * start() can be called without a call to init().
254: *
255: * @throws javax.jbi.JBIException if the BC is unable to start.
256: * @throws JBIException
257: */
258: public void start() throws javax.jbi.JBIException {
259: mLogger.info(mStringTranslator.getString(JMS_START_BEGIN));
260:
261: /* check if init is success */
262: if (!mInitSuccess) {
263: mLogger
264: .severe(mStringTranslator
265: .getString(JMS_INIT_FAILED));
266:
267: return;
268: }
269:
270: String compRoot = mContext.getInstallRoot();
271: String schemaFile = compRoot + File.separatorChar + "schema"
272: + File.separatorChar
273: + ConfigConstants.DEPL_CONFIG_SCHEMA_FILE;
274:
275: if (compRoot == null) {
276: mLogger.severe(mStringTranslator
277: .getString(JMS_INVALID_COMPROOT));
278:
279: return;
280: }
281:
282: MQManager man = null;
283:
284: String provurl = mJMSContext.getConfig().getProviderUrl();
285: String initctx = mJMSContext.getConfig()
286: .getInitialContextFactory();
287: try {
288:
289: File f = null;
290: URI furi = null;
291: try {
292: furi = URI.create(provurl);
293: } catch (Exception e) {
294: ;
295: }
296: if (furi != null) {
297: if (furi.getScheme().equals("file")) {
298: f = new File(furi);
299: if (!f.isDirectory()) {
300: /*
301: throw new JBIException(mStringTranslator.getString(
302: JMS_OBJECTSTORE_NOT_FOUND, provurl));
303: */
304: File f1 = new File(compRoot
305: + File.separatorChar + "imqobjects");
306: f1.mkdir();
307: mLogger.warning(mStringTranslator.getString(
308: JMS_OBJECTSTORE_NOT_FOUND, provurl));
309: mLogger.warning(mStringTranslator.getString(
310: JMS_DEFAULT_OBJECT_STORE, f1
311: .getAbsolutePath()));
312: provurl = f1.toURI().toString();
313:
314: }
315: }
316: }
317: } catch (Exception e) {
318: /*
319: throw new JBIException(mStringTranslator.getString(
320: JMS_OBJECTSTORE_NOT_FOUND, provurl));
321: */
322: mLogger.warning(mStringTranslator.getString(
323: JMS_OBJECTSTORE_NOT_FOUND, provurl));
324: }
325:
326: man = new MQManager(mJMSContext.getConfig()
327: .getInitialContextFactory(), provurl, mJMSContext
328: .getConfig().getSecurityPrincipal(), mJMSContext
329: .getConfig().getSecurityCredentials(), mJMSContext
330: .getConfig().getSecurityLevel());
331:
332: if (!man.init()) {
333: throw new JBIException(man.getError());
334: }
335:
336: mJMSContext.setMQManager(man);
337:
338: if (man == null) {
339: mLogger.severe(mStringTranslator
340: .getString(JMS_INITIAL_CONTEXT_ERROR));
341: throw new JBIException(mStringTranslator
342: .getString(JMS_INITIAL_CONTEXT_ERROR));
343: }
344:
345: try {
346: mChannel = mContext.getDeliveryChannel();
347: } catch (MessagingException me) {
348: mLogger.severe(mStringTranslator
349: .getString(JMS_BINDINGCHANNEL_FAILED)
350: + me.getMessage());
351:
352: return;
353: }
354:
355: WorkManager jmsworkman = WorkManager
356: .getWorkManager(ConfigConstants.JMS_WORK_MANAGER);
357:
358: try {
359: jmsworkman.setMinThreads(mJMSContext.getConfig()
360: .getMinThreadCount());
361: jmsworkman.setMaxThreads(mJMSContext.getConfig()
362: .getMinThreadCount());
363: jmsworkman.start();
364: mJMSContext.setJMSWorkManager(jmsworkman);
365:
366: mManager.setChannel(mChannel);
367: mManager.setConnectionManager(man);
368: mRegistry.init();
369:
370: mJMSReceiver = new JMSReceiver(mChannel);
371: mReceiverThread = new Thread(mJMSReceiver);
372: mReceiverThread.start();
373: } catch (Throwable t) {
374: t.printStackTrace();
375: throw new JBIException("**CANNOT START JMS BINDING**");
376: }
377:
378: mLogger.info(mStringTranslator.getString(JMS_START_END));
379: }
380:
381: /**
382: * Stop the BC. This makes the BC stop accepting messages for processing.
383: * After a call to this method, start() can be called again without first
384: * calling init().
385: *
386: * @throws javax.jbi.JBIException if the BC is unable to stop.
387: */
388: public void stop() throws javax.jbi.JBIException {
389: mLogger.info(mStringTranslator.getString(JMS_STOP_BEGIN));
390:
391: try {
392: if (mJMSReceiver != null) {
393: mJMSReceiver.stopReceiving();
394: }
395:
396: if (mChannel != null) {
397: mChannel.close();
398: }
399: if (mManager != null) {
400: mManager.releaseTemporarySession();
401: }
402:
403: } catch (Exception e) {
404: e.printStackTrace();
405: }
406:
407: mLogger.info(mStringTranslator.getString(JMS_STOP_END));
408: }
409:
410: /**
411: * Creates the configuration file name.
412: *
413: * @param componentRoot - component root directory.
414: *
415: * @return the configuration file name.
416: */
417: private String getConfigFileName(String componentRoot) {
418: return (componentRoot + ConfigConstants.JMS_CONFIG_FILE_NAME);
419: }
420: }
|