001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * FileBindingLifeCycle.java
005: * SUN PROPRIETARY/CONFIDENTIAL.
006: * This software is the proprietary information of Sun Microsystems, Inc.
007: * Use is subject to license terms.
008: *
009: */
010: package com.sun.jbi.binding.file;
011:
012: import com.sun.jbi.binding.file.util.ConfigData;
013: import com.sun.jbi.binding.file.util.ConfigReader;
014: import com.sun.jbi.binding.file.util.StringTranslator;
015:
016: import java.io.File;
017:
018: import java.util.logging.Logger;
019:
020: import javax.jbi.component.ComponentContext;
021: import javax.jbi.messaging.DeliveryChannel;
022: import javax.jbi.messaging.MessagingException;
023:
024: /**
025: * This is the Lifecycle class of the File Binding. The init and the start()
026: * methods of this class are invoked by the framework when the component is
027: * started
028: *
029: * @author Sun Microsystems, Inc.
030: */
031: public class FileBindingLifeCycle implements
032: javax.jbi.component.ComponentLifeCycle, FileBindingResources {
033: /**
034: * Binding context.
035: */
036: private ComponentContext mContext = null;
037:
038: /**
039: * Binding Channel Object
040: */
041: private DeliveryChannel mChannel;
042:
043: /**
044: * Deployment registry.
045: */
046: private DeploymentRegistry mRegistry;
047:
048: /**
049: * Manages the endpoints.
050: */
051: private EndpointManager mManager;
052:
053: /**
054: * Resolver object.
055: */
056: private FileBindingResolver mResolver;
057:
058: /**
059: * Deployer object.
060: */
061: private FileBindingSUManager mSUManager;
062:
063: /**
064: * File receiver object for receiving messages from NMS.
065: */
066: private FileReceiver mFileReceiver;
067:
068: /**
069: * Logger Object
070: */
071: private Logger mLog;
072:
073: /**
074: * Helper for i18n.
075: */
076: private StringTranslator mTranslator;
077:
078: /**
079: * Thread for file receiver.
080: */
081: private Thread mReceiverThread;
082:
083: /**
084: * Flag to hold the result of init
085: */
086: private boolean mInitSuccess = false;
087:
088: /**
089: * Get the ObjectName for any MBean that is provided for managing this
090: * binding. In this case, there is none, so a null is returned.
091: *
092: * @return ObjectName is always null.
093: */
094: public javax.management.ObjectName getExtensionMBeanName() {
095: return null;
096: }
097:
098: /**
099: * Sets the resolver.
100: *
101: * @param resolver resolver object.
102: */
103: public void setResolver(FileBindingResolver resolver) {
104: mResolver = resolver;
105: }
106:
107: /**
108: * Sets the SU Manager.
109: *
110: * @param manager su manager.
111: */
112: public void setSUManager(FileBindingSUManager manager) {
113: mSUManager = manager;
114: }
115:
116: /**
117: * Initialize the File BC. This performs initialization required by the
118: * File BC but does not make it ready to process messages. This method is
119: * called immediately after installation of the File BC. It is also
120: * called when the JBI framework is starting up, and any time the BC is
121: * being restarted after previously being shut down through a call to
122: * shutdown().
123: *
124: * @param jbiContext the JBI environment context
125: *
126: * @throws javax.jbi.JBIException if the BC is unable to initialize.
127: */
128: public void init(javax.jbi.component.ComponentContext jbiContext)
129: throws javax.jbi.JBIException {
130: FileBindingContext fbccontext = FileBindingContext
131: .getInstance();
132: fbccontext.setContext(jbiContext);
133:
134: Logger logger = jbiContext.getLogger("binding.file", null);
135: fbccontext.setLogger(logger);
136:
137: // jbiContext.
138: mLog = fbccontext.getLogger();
139: mTranslator = new StringTranslator();
140: mLog.info(mTranslator.getString(FBC_INIT_START));
141:
142: if (null == jbiContext) {
143: throw new javax.jbi.JBIException(mTranslator
144: .getString(FBC_INVALID_BINDINGCONTEXT));
145: }
146:
147: mContext = jbiContext;
148:
149: try {
150: mManager = new EndpointManager();
151: mSUManager.setValues(mManager, mResolver);
152: } catch (Exception e) {
153: e.printStackTrace();
154: throw new javax.jbi.JBIException(mTranslator
155: .getString(FBC_INIT_FAILED), e);
156: }
157:
158: try {
159: mRegistry = DeploymentRegistry.getInstance();
160: } catch (Exception e) {
161: e.printStackTrace();
162: }
163:
164: /* This a guard variable to check if init is a success
165: * Nothing should be done in start in case init is a failure
166: */
167: mInitSuccess = true;
168: }
169:
170: /**
171: * Shutdown the BC. This performs cleanup before the BC is terminated. Once
172: * this has been called, init() must be called before the BC can be
173: * started again with a call to start().
174: *
175: * @throws javax.jbi.JBIException if the BC is unable to shut down.
176: */
177: public void shutDown() throws javax.jbi.JBIException {
178: mLog.info(mTranslator.getString(FBC_SHUTDOWN_BEGIN));
179: mSUManager = null;
180: mRegistry.clearRegistry();
181: mLog.info(mTranslator.getString(FBC_SHUTDOWN_END));
182: }
183:
184: /**
185: * Start the File BC. This makes the File BC ready to process messages.
186: * This method is called after init() completes when the JBI framework is
187: * starting up, and when the BC is being restarted after a previous call
188: * to shutdown(). If stop() was called previously but shutdown() was not,
189: * start() can be called without a call to init().
190: *
191: * @throws javax.jbi.JBIException if the BC is unable to start.
192: */
193: public void start() throws javax.jbi.JBIException {
194: mLog.info(mTranslator.getString(FBC_START_BEGIN));
195:
196: if (!mInitSuccess) {
197: mLog.severe(mTranslator.getString(FBC_INIT_FAILED));
198:
199: return;
200: }
201:
202: String compRoot = mContext.getInstallRoot();
203: String schemaFile = compRoot + File.separatorChar
204: + ConfigData.SCHEMA_FILE;
205:
206: if (compRoot == null) {
207: mLog.severe(mTranslator.getString(FBC_INVALID_COMPROOT));
208:
209: return;
210: }
211:
212: try {
213: mChannel = mContext.getDeliveryChannel();
214:
215: //mChannel.setResolver(mResolver);
216: } catch (MessagingException me) {
217: mLog.severe(mTranslator
218: .getString(FBC_BINDINGCHANNEL_FAILED)
219: + me.getMessage());
220:
221: return;
222: }
223:
224: mFileReceiver = new FileReceiver(mChannel);
225: mReceiverThread = new Thread(mFileReceiver);
226: mReceiverThread.start();
227: mLog.info(mTranslator.getString(FBC_START_END));
228: }
229:
230: /**
231: * Stop the BC. This makes the BC stop accepting messages for processing.
232: * After a call to this method, start() can be called again without first
233: * calling init().
234: *
235: * @throws javax.jbi.JBIException if the BC is unable to stop.
236: */
237: public void stop() throws javax.jbi.JBIException {
238: mLog.info(mTranslator.getString(FBC_STOP_BEGIN));
239:
240: try {
241: if (mReceiverThread != null) {
242: mFileReceiver.stopReceiving();
243: mReceiverThread.interrupt();
244: mReceiverThread.join();
245: }
246:
247: if (mChannel != null) {
248: mChannel.close();
249: }
250: } catch (Exception e) {
251: e.printStackTrace();
252: }
253:
254: mLog.info(mTranslator.getString(FBC_STOP_END));
255: }
256: }
|