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: * @(#)MessageExchangeHelper.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.binding.jms.EndpointBean;
032: import com.sun.jbi.binding.jms.JMSBindingContext;
033:
034: import com.sun.jbi.binding.jms.config.ConfigConstants;
035: import java.util.logging.Logger;
036:
037: import javax.jbi.messaging.DeliveryChannel;
038: import javax.jbi.messaging.ExchangeStatus;
039: import javax.jbi.messaging.Fault;
040: import javax.jbi.messaging.InOnly;
041: import javax.jbi.messaging.InOut;
042: import javax.jbi.messaging.MessageExchange;
043: import javax.jbi.messaging.MessageExchangeFactory;
044: import javax.jbi.messaging.NormalizedMessage;
045: import javax.jbi.messaging.RobustInOnly;
046:
047: /**
048: * Helper class for processing message exchange.
049: *
050: * @author Sun Microsystems Inc.
051: */
052: public class MessageExchangeHelper {
053: /**
054: * Logger.
055: */
056: private static Logger sLog = Logger
057: .getLogger("com.sun.jbi.binding.jms");
058:
059: /**
060: * Creates a new instance of MessageExchangeHelper.
061: */
062: public MessageExchangeHelper() {
063: }
064:
065: /**
066: * Gets the destination name.
067: *
068: * @param dest destination JMS.
069: * @param eb endpoint bean.
070: *
071: * @return destination name.
072: */
073: public static Object getDestinationName(javax.jms.Destination dest,
074: EndpointBean eb) {
075: if (dest == null) {
076: return eb.getValue(ConfigConstants.REPLY_TO);
077: }
078:
079: return dest;
080:
081: /*
082: String replyto = null;
083:
084: try
085: {
086: if (dest instanceof javax.jms.Queue)
087: {
088: replyto = ((javax.jms.Queue) (dest)).getQueueName();
089: }
090: else if (dest instanceof javax.jms.Topic)
091: {
092: replyto = ((javax.jms.Topic) dest).getTopicName();
093: }
094: }
095: catch (Exception e)
096: {
097: ;
098: }
099: */
100:
101: }
102:
103: /**
104: * Gets the error.
105: *
106: * @param msg NMR message.
107: *
108: * @return exception.
109: */
110: public static Exception getError(MessageExchange msg) {
111: if (msg == null) {
112: return null;
113: }
114:
115: if (msg.getPattern().toString().trim().equals(
116: ConfigConstants.IN_OUT)) {
117: return (((InOut) (msg)).getError());
118: } else if (msg.getPattern().toString().trim().equals(
119: ConfigConstants.IN_ONLY)) {
120: return null;
121: } else if (msg.getPattern().toString().trim().equals(
122: ConfigConstants.ROBUST_IN_ONLY)) {
123: return (((RobustInOnly) (msg)).getError());
124: } else {
125: return null;
126: }
127: }
128:
129: /**
130: * Gets fault from NMR msg.
131: *
132: * @param msg NMR msg.
133: *
134: * @return Fault.
135: */
136: public static Fault getFault(MessageExchange msg) {
137: if (msg == null) {
138: return null;
139: }
140:
141: if (msg.getPattern().toString().trim().equals(
142: ConfigConstants.IN_OUT)) {
143: return (((InOut) (msg)).getFault());
144: } else if (msg.getPattern().toString().trim().equals(
145: ConfigConstants.IN_ONLY)) {
146: return null;
147: } else if (msg.getPattern().toString().trim().equals(
148: ConfigConstants.ROBUST_IN_ONLY)) {
149: return (((RobustInOnly) (msg)).getFault());
150: } else {
151: return null;
152: }
153: }
154:
155: /**
156: * Sets the input message.
157: *
158: * @param msg NMR message.
159: * @param nm normalized message.
160: */
161: public static void setInMessage(MessageExchange msg,
162: NormalizedMessage nm) {
163: if (msg == null) {
164: return;
165: }
166:
167: try {
168: if (msg.getPattern().toString().trim().equals(
169: ConfigConstants.IN_OUT)) {
170: ((InOut) (msg)).setInMessage(nm);
171: } else if (msg.getPattern().toString().trim().equals(
172: ConfigConstants.IN_ONLY)) {
173: ((InOnly) (msg)).setInMessage(nm);
174: } else if (msg.getPattern().toString().trim().equals(
175: ConfigConstants.ROBUST_IN_ONLY)) {
176: ((RobustInOnly) (msg)).setInMessage(nm);
177: }
178: } catch (Exception e) {
179: e.printStackTrace();
180: }
181: }
182:
183: /**
184: * Gets the normalized message from NMR msg.
185: *
186: * @param msg NMR msg.
187: *
188: * @return normalized msg.
189: */
190: public static NormalizedMessage getInMessage(MessageExchange msg) {
191: if (msg == null) {
192: sLog.severe("Returnign nulll in getInMessage()");
193:
194: return null;
195: }
196:
197: if (msg.getPattern().toString().trim().equals(
198: ConfigConstants.IN_OUT)) {
199: return (((InOut) (msg)).getInMessage());
200: } else if (msg.getPattern().toString().trim().equals(
201: ConfigConstants.IN_ONLY)) {
202: return (((InOnly) (msg)).getInMessage());
203: } else if (msg.getPattern().toString().trim().equals(
204: ConfigConstants.ROBUST_IN_ONLY)) {
205: return (((RobustInOnly) (msg)).getInMessage());
206: } else {
207: return null;
208: }
209: }
210:
211: /**
212: * Gets the outbound message.
213: *
214: * @param msg NMR msg.
215: *
216: * @return normalized message.
217: */
218: public static NormalizedMessage getOutMessage(MessageExchange msg) {
219: if (msg == null) {
220: return null;
221: }
222:
223: if (msg.getPattern().toString().trim().equals(
224: ConfigConstants.IN_OUT)) {
225: return (((InOut) (msg)).getOutMessage());
226: } else if (msg.getPattern().toString().trim().equals(
227: ConfigConstants.IN_ONLY)) {
228: return null;
229: } else if (msg.getPattern().toString().trim().equals(
230: ConfigConstants.ROBUST_IN_ONLY)) {
231: return null;
232: } else {
233: return null;
234: }
235: }
236:
237: /**
238: * Creates a new exchange based on pattern. This method can be replaced
239: * by the MessageExchangeFactory class.
240: *
241: * @param pattern mep.
242: *
243: * @return me.
244: */
245: public static MessageExchange createExchange(String pattern) {
246: MessageExchange tmpExchng = null;
247:
248: try {
249: DeliveryChannel chnl = JMSBindingContext.getInstance()
250: .getChannel();
251: MessageExchangeFactory factory = chnl
252: .createExchangeFactory();
253:
254: if (pattern.trim().equals(ConfigConstants.IN_OUT)) {
255: tmpExchng = factory.createInOutExchange();
256: } else if (pattern.trim().equals(ConfigConstants.IN_ONLY)) {
257: tmpExchng = factory.createInOnlyExchange();
258: } else if (pattern.toString().trim().equals(
259: ConfigConstants.ROBUST_IN_ONLY)) {
260: tmpExchng = factory.createRobustInOnlyExchange();
261: }
262:
263: return tmpExchng;
264: } catch (Exception e) {
265: e.printStackTrace();
266:
267: return null;
268: }
269: }
270:
271: /**
272: * Updates the message exchange.
273: *
274: * @param msg message exchange.
275: * @param in in message.
276: *
277: * @return true / false depending if update was successful or not
278: */
279: public static boolean updateInMessage(MessageExchange msg,
280: NormalizedMessage in) {
281: if (msg == null) {
282: return false;
283: }
284:
285: if (in != null) {
286: return createIn(msg, in);
287: }
288:
289: return true;
290: }
291:
292: /**
293: * Updates the Me with an outbound message.
294: *
295: * @param msg ME.
296: * @param out NM msg.
297: * @param error Error.
298: * @param fault Fault.
299: *
300: * @return true if udpated.
301: */
302: public static boolean updateOutMessage(MessageExchange msg,
303: NormalizedMessage out, Exception error, Fault fault) {
304: if (msg == null) {
305: return false;
306: }
307:
308: if (out != null) {
309: return createOut(msg, out);
310: }
311:
312: if (fault != null) {
313: return createFault(msg, fault);
314: }
315:
316: if (error != null) {
317: return createError(msg, error);
318: }
319:
320: try {
321: msg.setStatus(ExchangeStatus.DONE);
322: } catch (Exception e) {
323: e.printStackTrace();
324:
325: return false;
326: }
327:
328: return true;
329: }
330:
331: /**
332: * Creates error in ME.
333: *
334: * @param msg ME.
335: * @param error exception.
336: *
337: * @return true if updated.
338: */
339: private static boolean createError(MessageExchange msg,
340: Exception error) {
341: try {
342: if (error == null) {
343: msg.setError(new Exception("Unknown Exception"));
344:
345: return true;
346: }
347: msg.setError(error);
348:
349: /* Should do onlt setError or setStatus , should not do both
350: */
351:
352: // msg.setStatus(ExchangeStatus.ERROR);
353: } catch (Exception me) {
354: me.printStackTrace();
355:
356: return false;
357: }
358:
359: return true;
360: }
361:
362: /**
363: * Creates fault message in ME.
364: *
365: * @param msg ME.
366: * @param flt fault message.
367: *
368: * @return true if udpated.
369: */
370: private static boolean createFault(MessageExchange msg, Fault flt) {
371: if (flt == null) {
372: return false;
373: }
374:
375: try {
376: if (msg.getPattern().toString().trim().equals(
377: ConfigConstants.IN_ONLY)) {
378: msg.setStatus(ExchangeStatus.ERROR);
379: } else if (msg.getPattern().toString().trim().equals(
380: ConfigConstants.OUT_ONLY)) {
381: msg.setStatus(ExchangeStatus.ERROR);
382: }
383:
384: msg.setFault(flt);
385: } catch (Exception e) {
386: e.printStackTrace();
387:
388: return false;
389: }
390:
391: return true;
392: }
393:
394: /**
395: * Creates inbound messagge in ME.
396: *
397: * @param msg ME.
398: * @param in inbound message.
399: *
400: * @return true if updated.
401: */
402: private static boolean createIn(MessageExchange msg,
403: NormalizedMessage in) {
404: try {
405: if (msg.getPattern().toString().trim().equals(
406: ConfigConstants.IN_OUT)) {
407: ((InOut) (msg)).setInMessage(in);
408: } else if (msg.getPattern().toString().trim().equals(
409: ConfigConstants.IN_ONLY)) {
410: ((InOnly) (msg)).setInMessage(in);
411: } else if (msg.getPattern().toString().trim().equals(
412: ConfigConstants.ROBUST_IN_ONLY)) {
413: ((RobustInOnly) (msg)).setInMessage(in);
414: } else {
415: return false;
416: }
417: } catch (Exception e) {
418: e.printStackTrace();
419:
420: return false;
421: }
422:
423: return true;
424: }
425:
426: /**
427: * Creates the out message.
428: *
429: * @param msg ME
430: * @param out out message
431: *
432: * @return true /false.
433: */
434: private static boolean createOut(MessageExchange msg,
435: NormalizedMessage out) {
436: try {
437: if (msg.getPattern().toString().trim().equals(
438: ConfigConstants.IN_OUT)) {
439: ((InOut) (msg)).setOutMessage(out);
440: } else if (msg.getPattern().toString().trim().equals(
441: ConfigConstants.IN_ONLY)) {
442: msg.setStatus(ExchangeStatus.DONE);
443: } else if (msg.getPattern().toString().trim().equals(
444: ConfigConstants.ROBUST_IN_ONLY)) {
445: msg.setStatus(ExchangeStatus.DONE);
446: } else {
447: return false;
448: }
449: } catch (Exception e) {
450: e.printStackTrace();
451:
452: return false;
453: }
454:
455: return true;
456: }
457: }
|