001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * SequencingEngineLifecycle.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license term
009: */
010: package com.sun.jbi.engine.sequencing;
011:
012: import com.sun.jbi.engine.sequencing.util.StringTranslator;
013:
014: import java.util.logging.Logger;
015:
016: import javax.jbi.component.Component;
017: import javax.jbi.component.ComponentContext;
018: import javax.jbi.component.ComponentLifeCycle;
019: import javax.jbi.messaging.DeliveryChannel;
020:
021: /**
022: * This class implements EngineLifeCycle. The JBI framework will start this
023: * engine class automatically when JBI framework starts up.
024: *
025: * @author Sun Microsystems, Inc.
026: */
027: public class SequencingEngineLifeCycle implements ComponentLifeCycle,
028: SequencingEngineResources {
029: /**
030: * Component context passed down from framework to this sequencing engine.
031: */
032: private ComponentContext mContext = null;
033:
034: /**
035: * Engine channel
036: */
037: private DeliveryChannel mChannel;
038:
039: /**
040: * Deployment registry to keep track of deployments.
041: */
042: private DeploymentRegistry mRegistry;
043:
044: /**
045: * Refernce to logger.
046: */
047: private Logger mLog = null;
048:
049: /**
050: * Receiver object for this engine.
051: */
052: private MessageReceiver mMessageReceiver;
053:
054: /**
055: * Deployer object.
056: */
057: private SequencingEngineSUManager mSUManager;
058:
059: /**
060: * Service manager.
061: */
062: private ServiceManager mManager;
063:
064: /**
065: * Translator object for internationalization.
066: */
067: private StringTranslator mTranslator;
068:
069: /**
070: * Thread object for receiver.
071: */
072: private Thread mReceiverThread;
073:
074: /**
075: * Denotes if init is a success / failure.
076: */
077: private boolean mInitSuccess = false;
078:
079: /**
080: * Get the JMX ObjectName for any additional MBean for this BC. If there is
081: * none, return null.
082: *
083: * @return ObjectName the JMX object name of the additional MBean or null
084: * if there is no additional MBean.
085: */
086: public javax.management.ObjectName getExtensionMBeanName() {
087: return null;
088: }
089:
090: /**
091: * Sets the SU manager.
092: *
093: * @param manager su manager.
094: */
095: public void setSUManager(SequencingEngineSUManager manager) {
096: mSUManager = manager;
097: }
098:
099: /**
100: * Initialize the sequence engine. This performs initialization required by
101: * the sequencing engine but does not make it ready to process messages.
102: * This method is called immediately after installation of the sequencing
103: * engine. It is also called when the JBI framework is starting up, and
104: * any time the sequencing engine is being restarted after previously
105: * being shut down through a call to shutdown().
106: *
107: * @param jbiContext the JBI environment mContext
108: *
109: * @throws javax.jbi.JBIException if the sequencing engine is unable to
110: * initialize.
111: */
112: public void init(javax.jbi.component.ComponentContext jbiContext)
113: throws javax.jbi.JBIException {
114: mTranslator = new StringTranslator();
115:
116: if (jbiContext == null) {
117: throw new javax.jbi.JBIException(mTranslator
118: .getString(SEQ_INVALID_BINDINGCONTEXT));
119: }
120:
121: /* Assign values */
122: mContext = jbiContext;
123:
124: SequencingEngineContext seqContext = SequencingEngineContext
125: .getInstance();
126: seqContext.setContext(mContext);
127: mLog = mContext.getLogger("engine.sequencing", null);
128: seqContext.setLogger(mLog);
129: mLog.info(mTranslator.getString(SEQ_INIT_START));
130: mManager = new ServiceManager();
131: mSUManager.setValues(mContext, mManager);
132:
133: try {
134: mRegistry = DeploymentRegistry.getInstance();
135: } catch (Exception e) {
136: e.printStackTrace();
137: }
138:
139: mInitSuccess = true;
140: }
141:
142: /**
143: * Shutdown the SE. This performs cleanup before the SE is terminated. Once
144: * this has been called, init() must be called before the SE can be
145: * started again with a call to start().
146: *
147: * @throws javax.jbi.JBIException if the SE is unable to shut down.
148: */
149: public void shutDown() throws javax.jbi.JBIException {
150: mLog.info(mTranslator.getString(SEQ_SHUTDOWN_BEGIN));
151: mSUManager = null;
152: mManager = null;
153: mRegistry.clearRegistry();
154:
155: /* Unregister any other MBeans
156: */
157: mLog.info(mTranslator.getString(SEQ_SHUTDOWN_END));
158: }
159:
160: /**
161: * Start the seqeuncing engine. This makes the SE ready to process
162: * messages. This method is called after init() completes when the JBI
163: * framework is starting up, and when the sequencing engine is being
164: * restarted after a previous call to shutdown(). If stop() was called
165: * previously but shutdown() was not, start() can be called without a call
166: * to init().
167: *
168: * @throws javax.jbi.JBIException if the sequencing engine is unable to
169: * start.
170: */
171: public void start() throws javax.jbi.JBIException {
172: mLog.info(mTranslator.getString(SEQ_START_BEGIN));
173:
174: if (!mInitSuccess) {
175: mLog.severe(mTranslator.getString(SEQ_INIT_FAILED));
176:
177: return;
178: }
179:
180: mChannel = SequencingEngineContext.getInstance().getContext()
181: .getDeliveryChannel();
182:
183: if (mChannel == null) {
184: mLog
185: .severe(mTranslator
186: .getString(SEQ_ENGINECHANNEL_FAILED));
187:
188: return;
189: }
190:
191: mManager.setContext(mContext);
192: mMessageReceiver = new MessageReceiver(mChannel);
193: mReceiverThread = new Thread(mMessageReceiver);
194: mReceiverThread.start();
195: mLog.info(mTranslator.getString(SEQ_START_END));
196: }
197:
198: /**
199: * Stop the sequqencing engine. This makes the SE stop accepting messages
200: * for processing. After a call to this method, start() can be called
201: * again without first calling init().
202: *
203: * @throws javax.jbi.JBIException if the sequencing engine is unable to
204: * stop.
205: */
206: public void stop() throws javax.jbi.JBIException {
207: mLog.info(mTranslator.getString(SEQ_STOP_BEGIN));
208:
209: try {
210: if (mMessageReceiver != null) {
211: mMessageReceiver.stopNewRequests();
212: }
213:
214: if (mMessageReceiver != null) {
215: mMessageReceiver.stopReceiving();
216: }
217:
218: if (mChannel != null) {
219: mChannel.close();
220: }
221: } catch (Exception e) {
222: e.printStackTrace();
223: }
224:
225: mLog.info(mTranslator.getString(SEQ_STOP_END));
226: }
227: }
|