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: * @(#)MessageProcessor.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.deploy.EndpointRegistry;
034:
035: import com.sun.jbi.binding.jms.handler.InboundMessageHandler;
036: import com.sun.jbi.binding.jms.handler.MessageHandler;
037: import com.sun.jbi.binding.jms.handler.OutboundMessageHandler;
038:
039: import com.sun.jbi.binding.jms.config.ConfigConstants;
040: import java.net.URI;
041:
042: import java.text.DateFormat;
043: import java.text.SimpleDateFormat;
044:
045: import java.util.Calendar;
046: import java.util.Date;
047: import java.util.GregorianCalendar;
048: import java.util.Iterator;
049: import java.util.List;
050:
051: import java.util.logging.Logger;
052:
053: import javax.jbi.messaging.DeliveryChannel;
054:
055: import javax.jbi.messaging.MessageExchange;
056:
057: import javax.jbi.messaging.MessageExchange.Role;
058: import javax.xml.namespace.QName;
059:
060: /**
061: * Processes all the messages from the NMR.
062: *
063: * @author Sun Microsystems Inc.
064: */
065: public class MessageProcessor implements JMSBindingResources {
066: /**
067: * String representing property name in exchange.
068: */
069: private static final String FILENAME_PROPERTY = "FILENAME";
070:
071: /**
072: * String representing property name in exchange.
073: */
074: private static final String FILEEXTENSION_PROPERTY = "FILEEXTENSION";
075:
076: /**
077: * Channel for this binding.
078: */
079: private DeliveryChannel mChannel;
080:
081: /**
082: * Endpoint Bean for the exhange being processed.
083: */
084: private EndpointBean mEndpointBean;
085:
086: /**
087: * Registry for storing active deployments.
088: */
089: private EndpointRegistry mRegistry;
090:
091: /**
092: * Logger object.
093: */
094: private Logger mLogger;
095:
096: /**
097: * Message exchange object.
098: */
099: private MessageExchange mExchange;
100: /**
101: *
102: */
103: private String mError = "";
104: /**
105: * Helper for i18n.
106: */
107: private StringTranslator mStringTranslator;
108:
109: /**
110: * Creates a new MessageProcessor object.
111: *
112: * @param chnl binding channel.
113: */
114: public MessageProcessor(DeliveryChannel chnl) {
115: mChannel = chnl;
116: mRegistry = JMSBindingContext.getInstance().getRegistry();
117: mLogger = JMSBindingContext.getInstance().getLogger();
118: mStringTranslator = JMSBindingContext.getInstance()
119: .getStringTranslator();
120: }
121:
122: /**
123: * Gets the date.
124: *
125: * @return date string.
126: */
127: public String getDate() {
128: Calendar cal = new GregorianCalendar();
129: Date dt = cal.getTime();
130: DateFormat format = new SimpleDateFormat("ddHHmmssSS");
131: String sdate = format.format(dt);
132:
133: return sdate;
134: }
135:
136: /**
137: * Sets the exchange.
138: *
139: * @param exch message exchange.
140: */
141: public void setExchange(MessageExchange exch) {
142: mExchange = exch;
143: }
144:
145: /**
146: * Command pattern for executing in a thread pool.
147: *
148: * @return message handler.
149: */
150: public MessageHandler process() {
151: mLogger.info("Processing message " + mExchange.getExchangeId());
152: if (mExchange == null) {
153: mLogger.severe("Set message exchange first");
154:
155: return null;
156: }
157:
158: if (!valid()) {
159: sendError();
160:
161: return null;
162: }
163: mLogger.info("Message is Valid ");
164: MessageHandler handler = null;
165: URI pattern = mExchange.getPattern();
166: String pat = pattern.toString().trim();
167:
168: if (pat.trim().equals(ConfigConstants.IN_OUT.trim())
169: || pat.trim().equals(ConfigConstants.IN_ONLY.trim())
170: || pat.trim().equals(
171: ConfigConstants.ROBUST_IN_ONLY.trim())) {
172: mLogger.info(mStringTranslator.getString(
173: JMS_RECEIVED_INBOUND, pat, mExchange
174: .getExchangeId()));
175:
176: if (mExchange.getRole().equals(
177: MessageExchange.Role.PROVIDER)) {
178: handler = processOutbound();
179: } else {
180: handler = processInbound();
181: }
182: } else if (pat.trim().equals(
183: ConfigConstants.IN_OPTIONAL_OUT.trim())) {
184: mLogger.info(mStringTranslator.getString(
185: JMS_RECEIVED_INOPTIONALOUT, mExchange
186: .getExchangeId()));
187: } else {
188: mLogger.info(mStringTranslator.getString(
189: JMS_RECEIVED_UNSUPPORTED_MEP, mExchange
190: .getExchangeId()));
191: }
192:
193: return handler;
194: }
195:
196: /**
197: * Sets the error.
198: *
199: * @param err error string.
200: */
201: private void setError(String err) {
202: mError = err;
203: }
204:
205: /**
206: * Processes inbound.
207: *
208: * @return message handler.
209: */
210: private MessageHandler processInbound() {
211: MessageHandler hndl = new InboundMessageHandler();
212:
213: if (hndl != null) {
214: hndl.setNMSMessage(mExchange);
215: hndl.setBean(mEndpointBean);
216: }
217:
218: return hndl;
219: }
220:
221: /**
222: * Processes outbound.
223: *
224: * @return message handler.
225: */
226: private MessageHandler processOutbound() {
227: MessageHandler hndl = new OutboundMessageHandler();
228:
229: if (hndl != null) {
230: hndl.setNMSMessage(mExchange);
231: hndl.setBean(mEndpointBean);
232: }
233:
234: return hndl;
235: }
236:
237: /**
238: * Sends the error.
239: */
240: private void sendError() {
241: /**
242: * Sends an error if servicename or operationb is not supported. or if
243: * the binding is not in a position to accept new requests.
244: */
245: Exception exp = new Exception(mError);
246:
247: try {
248: mExchange.setError(exp);
249: mChannel.send(mExchange);
250: } catch (Exception e) {
251: e.printStackTrace();
252: }
253: }
254:
255: /**
256: * Validity.
257: *
258: * @return true or false.
259: */
260: private boolean valid() {
261: Exception exc;
262: String sname = mExchange.getEndpoint().getServiceName()
263: .toString();
264: String epname = mExchange.getEndpoint().getEndpointName();
265: QName intername = mExchange.getInterfaceName();
266: mLogger.severe("RECVD MSG : " + sname + " " + epname);
267: if ((sname == null) || (epname == null)) {
268: mLogger.severe(mStringTranslator.getString(
269: JMS_START_DEPLOYMENT_FAILED_BEANNULL, mExchange
270: .getExchangeId()));
271: setError(mStringTranslator.getString(
272: JMS_START_DEPLOYMENT_FAILED_BEANNULL, mExchange
273: .getExchangeId()));
274:
275: return false;
276: }
277: try {
278: if (mExchange.getRole().equals(
279: MessageExchange.Role.PROVIDER)) {
280: mEndpointBean = mRegistry.getEndpoint(sname + epname);
281: } else {
282: mEndpointBean = mRegistry.getEndpoint(sname);
283:
284: /**
285: * There is a possiblity that the message was routed initially
286: * with the interface name and not the service name
287: * So we have to try an endpoint with interface name now
288: */
289:
290: if (mEndpointBean == null) {
291: if (intername != null) {
292: mEndpointBean = mRegistry
293: .findEndpointByInterface(intername
294: .toString().trim());
295: }
296: }
297: }
298: } catch (Exception e) {
299: mEndpointBean = null;
300: }
301:
302: if (mEndpointBean == null) {
303: mLogger.severe(mStringTranslator.getString(
304: JMS_START_DEPLOYMENT_FAILED_BEANNULL, mExchange
305: .getExchangeId()));
306: setError(mStringTranslator.getString(
307: JMS_START_DEPLOYMENT_FAILED_BEANNULL, mExchange
308: .getExchangeId()));
309:
310: return false;
311: }
312:
313: if (!mEndpointBean.getStatus().equals(EndpointStatus.STARTED)) {
314: mLogger.severe(mStringTranslator
315: .getString(JMS_ENDPOINT_NOT_STARTED, mExchange
316: .getExchangeId()));
317: setError(mStringTranslator
318: .getString(JMS_ENDPOINT_NOT_STARTED, mExchange
319: .getExchangeId()));
320:
321: return false;
322: }
323:
324: /**
325: *Look at only the operation name, namespace is not necessary
326: * WSDL 2.0 spec says that any two operations with same name
327: * should be same
328: */
329: String operation = mExchange.getOperation().getLocalPart();
330:
331: List l = mEndpointBean.getAllOperations();
332: Iterator it = l.iterator();
333: boolean found = false;
334:
335: while (it.hasNext()) {
336: OperationBean op = (OperationBean) it.next();
337:
338: if (op.getName().trim().equals(operation.trim())) {
339: found = true;
340:
341: break;
342: }
343: }
344:
345: if (!found) {
346: mLogger.severe(mStringTranslator.getString(
347: JMS_UNSUPPORED_OPERATION, operation, mExchange
348: .getExchangeId()));
349: setError(mStringTranslator.getString(
350: JMS_UNSUPPORED_OPERATION, operation, mExchange
351: .getExchangeId()));
352:
353: return false;
354: }
355:
356: return true;
357: }
358:
359: }
|