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