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: * @(#)Receiver.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.xslt;
030:
031: import com.sun.jbi.engine.xslt.framework.WorkManager;
032: import com.sun.jbi.engine.xslt.util.StringTranslator;
033:
034: import java.util.logging.Logger;
035:
036: import javax.jbi.component.ComponentContext;
037: import javax.jbi.messaging.DeliveryChannel;
038: import javax.jbi.messaging.MessageExchange;
039: import javax.jbi.messaging.MessagingException;
040:
041: //import javax.jbi.engine.EngineContext;
042:
043: /**
044: * This class implements runnable. Its run in a seprate thread. This blocks and
045: * pulls messages from NMS. It then submits the received normalized messages
046: * for further processing.
047: *
048: * @author Sun Microsystems Inc.
049: */
050: public class Receiver implements Runnable, TEResources {
051: /**
052: *
053: */
054: private ComponentContext mContext = null;
055:
056: /**
057: * Refernce to a DeliveryChannel passed to command object. This is used by
058: * command object to send reply to sender.
059: */
060: private DeliveryChannel mChannel = null;
061:
062: /**
063: * Logger for logging information.
064: */
065: private Logger mLogger = null;
066:
067: /**
068: *
069: */
070: private MessageExchange mExchange = null;
071:
072: /**
073: *
074: */
075: private StringTranslator mTranslator = null;
076:
077: /**
078: *
079: */
080: private WorkManager mWorkManager = null;
081:
082: /**
083: * Boolean flag for controlling life cycle of this thread.
084: */
085: private boolean mRunFlag = true;
086:
087: /**
088: * Constructor for creating instance of this class.
089: *
090: * @param context for receiving environment parameters.
091: */
092: public Receiver(ComponentContext context) {
093: mContext = context;
094: mTranslator = new StringTranslator("com.sun.jbi.engine.xslt",
095: this .getClass().getClassLoader());
096: mLogger = TransformationEngineContext.getInstance().getLogger(
097: "");
098: mLogger.info(mTranslator
099: .getString(TEResources.RECEIVER_CONSTRUCTOR));
100:
101: init();
102: }
103:
104: /**
105: * This is called to gracefully stop the ReceiverThread.
106: */
107: public synchronized void cease() {
108: mLogger.info(mTranslator.getString(TEResources.RECEVIER_CEASE));
109: mRunFlag = false;
110:
111: if (mWorkManager != null) {
112: mWorkManager.cease();
113: }
114:
115: mLogger.info(mTranslator
116: .getString(TEResources.RECEVIER_CEASE_END));
117: }
118:
119: /**
120: * It polls NMS periodically for input messages. It blocks for 6 sec on
121: * receive call to receive messages.Depending on runFlag staus either it
122: * tries to again poll NMS or exits.
123: */
124: public void exec() {
125: mLogger.info(mTranslator.getString(TEResources.RECEVIER_EXEC));
126: mWorkManager.start();
127:
128: try {
129: mChannel = mContext.getDeliveryChannel();
130:
131: if (mChannel == null) {
132: mLogger.severe(mTranslator
133: .getString(TEResources.CHANNEL_NULL));
134: }
135: } catch (MessagingException ex) {
136: mLogger.severe(mTranslator
137: .getString(TEResources.RECEIVER_TERMINATED));
138: ex.printStackTrace();
139:
140: return;
141: }
142:
143: while (mRunFlag) {
144: try {
145: mExchange = mChannel.accept();
146: if (mExchange != null) {
147: mLogger.info(mTranslator.getString(
148: TEResources.GOT_MESSAGEEXCHANGE, mExchange
149: .getExchangeId()));
150: ServiceExecutor executor = new ServiceExecutor(
151: mChannel, mExchange);
152:
153: while (!mWorkManager.processCommand(executor)) {
154: //block till command is processed
155: }
156: }
157: /*else
158: {
159: mLogger.severe(mTranslator.getString(
160: TEResources.TE_ERR_MESSEXC_RCVD_NULL));
161: }*/
162: } catch (MessagingException mex) {
163: mLogger.info(mTranslator
164: .getString(TEResources.RECEIVER_EXIT));
165:
166: //mex.printStackTrace();
167: break;
168: } catch (Exception ex) {
169: ex.printStackTrace();
170: }
171: }
172:
173: mLogger.info(mTranslator
174: .getString(TEResources.RECECIVER_FINISHED));
175: }
176:
177: /**
178: * This is called for starting the ReceiverThread.
179: */
180: public void run() {
181: mLogger.info(mTranslator.getString(TEResources.RECEIVER_START));
182: exec();
183: mLogger.info(mTranslator
184: .getString(TEResources.RECEIVER_START_END));
185: }
186:
187: /**
188: *
189: */
190: private void init() {
191: mLogger.info(mTranslator.getString(TEResources.RECEIVER_INIT));
192: mWorkManager = WorkManager.getWorkManager(mTranslator
193: .getString(TEResources.XSLT));
194: mLogger.info(mTranslator
195: .getString(TEResources.RECEIVER_INIT_END));
196: }
197: }
|