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: * @(#)OutboundMessageHandler.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.handler;
030:
031: import com.sun.jbi.StringTranslator;
032: import com.sun.jbi.wsdl11wrapper.Wsdl11WrapperHelper;
033: import com.sun.jbi.binding.jms.EndpointBean;
034: import com.sun.jbi.binding.jms.EndpointStatus;
035: import com.sun.jbi.binding.jms.JMSBindingContext;
036:
037: import com.sun.jbi.binding.jms.mq.MQDestination;
038: import com.sun.jbi.binding.jms.mq.MQManager;
039: import com.sun.jbi.binding.jms.mq.MQSession;
040:
041: import com.sun.jbi.binding.jms.config.ConfigConstants;
042: import java.util.logging.Logger;
043:
044: import javax.jbi.JBIException;
045:
046: import javax.jbi.messaging.MessageExchange;
047:
048: import javax.jms.Destination;
049: import javax.jms.Message;
050:
051: import javax.xml.namespace.QName;
052:
053: /**
054: * This class handles only In MEPS
055: */
056: public class OutboundMessageHandler extends MessageHandlerImpl {
057: /**
058: * Creates a new OutboundMessageHandler object.
059: */
060: public OutboundMessageHandler() {
061: super ();
062: }
063:
064: /**
065: * Checks the message.
066: */
067: public void check() {
068: Message msg = getJMSMessage();
069: MessageExchange exch = getNMSMessage();
070: QName operation = null;
071:
072: EndpointBean eb = null;
073:
074: if (exch != null) {
075: operation = exch.getOperation();
076:
077: try {
078: eb = (EndpointBean) (JMSBindingContext.getInstance()
079: .getRegistry().getEndpoint(exch.getEndpoint()));
080: } catch (javax.jbi.JBIException je) {
081: ;
082: }
083:
084: if ((eb == null)
085: || (!eb.getStatus().equals(EndpointStatus.STARTED))) {
086: setError(mStringTranslator.getString(JMS_NO_ENDPOINT,
087: exch.getExchangeId(), exch.getEndpoint()
088: .getServiceName().toString(), exch
089: .getEndpoint().getEndpointName()));
090:
091: return;
092: }
093:
094: String mep = eb.getMEP(operation.getLocalPart());
095:
096: if (mep == null) {
097: setError(mStringTranslator.getString(
098: JMS_HANDLER_INVALID_OPERATION, exch
099: .getOperation(), exch.getExchangeId(),
100: exch.getEndpoint().getServiceName().toString(),
101: exch.getEndpoint().getEndpointName()));
102:
103: return;
104: }
105: } else {
106: mLogger.severe(mStringTranslator
107: .getString(JMS_IRRECOVERABLE_ERROR));
108: setError(mStringTranslator
109: .getString(JMS_IRRECOVERABLE_ERROR));
110:
111: return;
112: }
113:
114: setBean(eb);
115: setCurrentOperation(operation);
116: }
117:
118: /**
119: * Exceute the handler.
120: */
121: public void execute() {
122: super .execute();
123:
124: /* do sanity first
125: */
126: check();
127:
128: if (!isValid()) {
129: mLogger.severe("**INVALID MESSAGE**");
130: sendNMSStatus();
131:
132: return;
133: }
134:
135: if (getJMSMessage() != null) {
136: processJMSMessage();
137:
138: if (!isValid()) {
139: mLogger.severe(mStringTranslator
140: .getString(JMS_MESSAGE_FAILURE));
141: sendNMSStatus();
142:
143: return;
144: }
145: } else if (getNMSMessage() != null) {
146: processNMSMessage();
147:
148: if (!isValid()) {
149: mLogger.severe(mStringTranslator
150: .getString(JMS_MESSAGE_FAILURE));
151: sendNMSStatus();
152:
153: return;
154: }
155: } else {
156: mLogger.severe(mStringTranslator
157: .getString(JMS_MESSAGE_FAILURE));
158: return;
159: }
160: }
161:
162: /**
163: * Process JMS message.
164: */
165: private void processJMSMessage() {
166: Message msg = getJMSMessage();
167: EndpointBean eb = getBean();
168: MessageAdaptor adaptor = MessageAdaptorFactory.getAdaptor(msg,
169: eb.getDeploymentType());
170:
171: if (adaptor == null) {
172: mLogger.severe(mStringTranslator
173: .getString(JMS_UNSUPPORTED_MESSAGE));
174: setError(mStringTranslator
175: .getString(JMS_UNSUPPORTED_MESSAGE));
176:
177: return;
178: }
179:
180: Wsdl11WrapperHelper wrapperhelper;
181: if (eb.getDeploymentType().trim().equals("WSDL11")) {
182: wrapperhelper = new Wsdl11WrapperHelper(eb
183: .getWsdlDefinition());
184: adaptor.setEpilogueProcessor(wrapperhelper);
185: }
186:
187: if (eb == null) {
188: mLogger
189: .severe(mStringTranslator
190: .getString(JMS_NO_ENDPOINT));
191: setError(mStringTranslator.getString(JMS_NO_ENDPOINT));
192:
193: return;
194: }
195:
196: String inputtype = eb.getOutputType(getCurrentOperation()
197: .getLocalPart());
198:
199: if (inputtype == null) {
200: mLogger.severe(mStringTranslator
201: .getString(JMS_INCONSISTENT_MESSAGE_TYPE));
202: setError(mStringTranslator
203: .getString(JMS_INCONSISTENT_MESSAGE_TYPE));
204:
205: return;
206: }
207:
208: if (!adaptor.getName().equals(inputtype.trim())) {
209: mLogger.severe(mStringTranslator
210: .getString(JMS_INCONSISTENT_MESSAGE_TYPE));
211: setError(mStringTranslator
212: .getString(JMS_INCONSISTENT_MESSAGE_TYPE));
213:
214: //send errorr
215: return;
216: }
217:
218: setNMSHeaders();
219:
220: MessageExchange ex = getNMSMessage();
221:
222: if (ex == null) {
223: mLogger.severe(mStringTranslator.getString(
224: JMS_JMS_NMR_CONVERSION_FAILED, "NULL"));
225: setError(mStringTranslator.getString(
226: JMS_JMS_NMR_CONVERSION_FAILED, "NULL"));
227:
228: //free up any resources here. set them to null
229: return;
230: }
231:
232: adaptor.convertJMStoNMSMessage(getJMSMessage(), ex);
233:
234: if (ex == null) {
235: mLogger.severe(mStringTranslator.getString(
236: JMS_JMS_NMR_CONVERSION_FAILED, adaptor.getError()));
237: setError(mStringTranslator.getString(
238: JMS_JMS_NMR_CONVERSION_FAILED, adaptor.getError()));
239:
240: //free up any resources here. set them to null
241: return;
242: }
243:
244: if (!send(ex)) {
245: mLogger.severe("**SEND FAILED**");
246: setError("**SEND FAILED**" + "\n" + getError());
247:
248: return;
249: }
250:
251: Object replyto = MessageExchangeHelper.getDestinationName(
252: adaptor.getJMSReplyTo(), eb);
253: mLogger.fine("**REPLY DESTINATION " + replyto);
254:
255: registerMessage(ex.getExchangeId(), replyto);
256:
257: return;
258: }
259:
260: /**
261: * Process the NMR message.
262: */
263: private void processNMSMessage() {
264: MessageExchange exch = getNMSMessage();
265:
266: if (canTerminateProvider()) {
267: return;
268: }
269:
270: String oper = getCurrentOperation().getLocalPart();
271: mLogger.fine("**OPERATION** " + oper);
272:
273: EndpointBean eb = getBean();
274: mLogger.fine("**OUTPUT-TYPE**" + eb.getInputType(oper));
275:
276: MessageAdaptor adaptor = MessageAdaptorFactory.getAdaptor(eb
277: .getInputType(oper), eb.getDeploymentType());
278: MQSession session = getSession();
279:
280: if (session == null) {
281: setError(mStringTranslator
282: .getString(JMS_CANNOT_GET_SESSION));
283: mLogger.severe(mStringTranslator
284: .getString(JMS_CANNOT_GET_SESSION));
285:
286: return;
287: }
288:
289: Message msg = session.createJMSMessage(eb.getInputType(oper));
290:
291: if (adaptor == null) {
292: setError(mStringTranslator
293: .getString(JMS_UNSUPPORTED_MESSAGE));
294: mLogger.severe(mStringTranslator
295: .getString(JMS_UNSUPPORTED_MESSAGE));
296:
297: return;
298: }
299:
300: Wsdl11WrapperHelper wrapperhelper;
301: if (eb.getDeploymentType().trim().equals("WSDL11")) {
302: wrapperhelper = new Wsdl11WrapperHelper(eb
303: .getWsdlDefinition());
304: adaptor.setEpilogueProcessor(wrapperhelper);
305: }
306: MQManager man = JMSBindingContext.getInstance().getMQManager();
307:
308: MQDestination dest = man.getTemporaryQueue();
309:
310: if (dest == null) {
311: setError(mStringTranslator
312: .getString(JMS_CANNOT_GET_TEMP_DEST));
313: mLogger.severe(mStringTranslator
314: .getString(JMS_CANNOT_GET_TEMP_DEST));
315:
316: return;
317: }
318:
319: adaptor.setJMSReplyTo(dest.getDestination());
320:
321: adaptor.convertNMStoJMSMessage(exch, msg);
322:
323: if (!adaptor.isValid()) {
324: setError(adaptor.getError());
325:
326: return;
327: }
328:
329: String id = (String) exch
330: .getProperty(MessageProperties.JMS_CORRELATION_ID);
331:
332: if (id == null) {
333: id = exch.getExchangeId();
334: }
335:
336: String destname = (String) eb
337: .getValue(ConfigConstants.DESTINATION_NAME);
338: mLogger.fine("**SENDING MESSAGE TO " + destname);
339:
340: // register upfront to avoid race conditions
341: // we can deregister if not appllicable
342: registerMessage(id, exch);
343:
344: if (!send(msg, destname, session)) {
345: mLogger
346: .severe(mStringTranslator
347: .getString(JMS_CANNOT_SEND));
348: setError(mStringTranslator.getString(JMS_CANNOT_SEND));
349: man.releaseSession(session);
350: deRegisterMessage(id);
351:
352: return;
353: }
354:
355: //send the message with reply to set to temporary queue
356: clear();
357:
358: if ((exch.getPattern().toString().trim()
359: .equals(ConfigConstants.IN_ONLY))) {
360: // life of an outonly ends here
361: mLogger.fine(mStringTranslator
362: .getString(JMS_SEND_STATUS_NMR));
363:
364: // dont register response not expected
365: deRegisterMessage(id);
366: sendNMSStatus();
367: }
368:
369: man.releaseSession(session);
370: }
371:
372: /**
373: * Sends the JMS error.
374: */
375: private void sendJMSError() {
376: EndpointBean eb = getBean();
377:
378: String mep = eb.getMEP(getCurrentOperation().getLocalPart());
379:
380: if ((mep == null)
381: || (mep.trim().equals(ConfigConstants.IN_ONLY))) {
382: return;
383: }
384:
385: MessageAdaptor adaptor = MessageAdaptorFactory.getAdaptor(eb
386: .getOutputType(getCurrentOperation().getLocalPart()),
387: "");
388:
389: if (adaptor == null) {
390: mLogger.severe(mStringTranslator
391: .getString(JMS_IRRECOVERABLE_ERROR));
392:
393: return;
394: }
395:
396: MQSession session = getSession();
397:
398: if (session == null) {
399: mLogger.severe(mStringTranslator
400: .getString(JMS_IRRECOVERABLE_ERROR));
401:
402: return;
403: }
404:
405: Message msg = session.createJMSMessage(eb
406: .getOutputType(getCurrentOperation().getLocalPart()));
407: adaptor.updateJMSErrorMessage(msg, getError());
408:
409: MessageExchange exch = getNMSMessage();
410: Object replyto = MessageExchangeHelper.getDestinationName(
411: adaptor.getJMSReplyTo(), eb);
412:
413: if (!send(msg, replyto, session)) {
414: ;
415: }
416: }
417: }
|