001: // Copyright (c) 2004-2005 Sun Microsystems Inc., All Rights Reserved.
002:
003: /*
004: * MessageExchangeHelper.java
005: *
006: * SUN PROPRIETARY/CONFIDENTIAL
007: * This software is the proprietary information of Sun Microsystems, Inc.
008: * Use is subject to license term
009: */
010: package com.sun.jbi.engine.sequencing.util;
011:
012: import com.sun.jbi.engine.sequencing.PatternRegistry;
013: import com.sun.jbi.engine.sequencing.SequencingEngineContext;
014:
015: import java.util.logging.Logger;
016:
017: import javax.jbi.messaging.DeliveryChannel;
018: import javax.jbi.messaging.ExchangeStatus;
019: import javax.jbi.messaging.Fault;
020: import javax.jbi.messaging.InOnly;
021: import javax.jbi.messaging.InOut;
022: import javax.jbi.messaging.MessageExchange;
023: import javax.jbi.messaging.MessageExchangeFactory;
024: import javax.jbi.messaging.NormalizedMessage;
025: import javax.jbi.messaging.RobustInOnly;
026:
027: /**
028: * Helper class to process message exchanges.
029: *
030: * @author Sun Microsystems, Inc.
031: */
032: public class MessageExchangeHelper {
033: /**
034: * Logger.
035: */
036: private static Logger sLog = Logger
037: .getLogger("com.sun.jbi.engine.seqeuncing");
038:
039: /**
040: * Creates a new instance of MessageExchangeHelper.
041: */
042: public MessageExchangeHelper() {
043: }
044:
045: /**
046: * Returns error.
047: *
048: * @param msg ME
049: *
050: * @return exception exception
051: */
052: public static Exception getError(MessageExchange msg) {
053: if (msg == null) {
054: return null;
055: }
056:
057: if (msg.getPattern().toString().trim().equals(
058: PatternRegistry.IN_OUT)) {
059: return (((InOut) (msg)).getError());
060: } else if (msg.getPattern().toString().trim().equals(
061: PatternRegistry.IN_ONLY)) {
062: return null;
063: } else if (msg.getPattern().toString().trim().equals(
064: PatternRegistry.ROBUST_IN_ONLY)) {
065: return (((RobustInOnly) (msg)).getError());
066: } else {
067: return null;
068: }
069: }
070:
071: /**
072: * Returns fault.
073: *
074: * @param msg message exchange from which fault has to be extracted.
075: *
076: * @return Fault fault
077: */
078: public static Fault getFault(MessageExchange msg) {
079: if (msg == null) {
080: return null;
081: }
082:
083: if (msg.getPattern().toString().trim().equals(
084: PatternRegistry.IN_OUT)) {
085: return (((InOut) (msg)).getFault());
086: } else if (msg.getPattern().toString().trim().equals(
087: PatternRegistry.IN_ONLY)) {
088: return null;
089: } else if (msg.getPattern().toString().trim().equals(
090: PatternRegistry.ROBUST_IN_ONLY)) {
091: return (((RobustInOnly) (msg)).getFault());
092: } else {
093: return null;
094: }
095: }
096:
097: /**
098: * Returns the in message.
099: *
100: * @param msg message exchange
101: *
102: * @return normalized message
103: */
104: public static NormalizedMessage getInMessage(MessageExchange msg) {
105: if (msg == null) {
106: sLog.severe("Returnign nulll in getInMessage()");
107:
108: return null;
109: }
110:
111: if (msg.getPattern().toString().trim().equals(
112: PatternRegistry.IN_OUT)) {
113: return (((InOut) (msg)).getInMessage());
114: } else if (msg.getPattern().toString().trim().equals(
115: PatternRegistry.IN_ONLY)) {
116: return (((InOnly) (msg)).getInMessage());
117: } else if (msg.getPattern().toString().trim().equals(
118: PatternRegistry.ROBUST_IN_ONLY)) {
119: return (((RobustInOnly) (msg)).getInMessage());
120: } else {
121: return null;
122: }
123: }
124:
125: /**
126: * Returns the out message.
127: *
128: * @param msg message exchange
129: *
130: * @return normalized message
131: */
132: public static NormalizedMessage getOutMessage(MessageExchange msg) {
133: if (msg == null) {
134: return null;
135: }
136:
137: if (msg.getPattern().toString().trim().equals(
138: PatternRegistry.IN_OUT)) {
139: return (((InOut) (msg)).getOutMessage());
140: } else if (msg.getPattern().toString().trim().equals(
141: PatternRegistry.IN_ONLY)) {
142: return null;
143: } else if (msg.getPattern().toString().trim().equals(
144: PatternRegistry.ROBUST_IN_ONLY)) {
145: return null;
146: } else {
147: return null;
148: }
149: }
150:
151: /**
152: * Creates an exchange corresponding to pattern.
153: *
154: * @param pattern pattern
155: *
156: * @return message exchange
157: */
158: public static MessageExchange createExchange(String pattern) {
159: sLog.fine("Creating exchange for pattern " + pattern);
160:
161: MessageExchange tmpExchng = null;
162:
163: try {
164: DeliveryChannel chnl = SequencingEngineContext
165: .getInstance().getChannel();
166:
167: MessageExchangeFactory factory = chnl
168: .createExchangeFactory();
169:
170: if (pattern.trim().equals(PatternRegistry.IN_OUT)) {
171: tmpExchng = factory.createInOutExchange();
172: } else if (pattern.trim().equals(PatternRegistry.IN_ONLY)) {
173: tmpExchng = factory.createInOnlyExchange();
174: } else if (pattern.trim().equals(
175: PatternRegistry.ROBUST_IN_ONLY)) {
176: tmpExchng = factory.createRobustInOnlyExchange();
177: }
178:
179: return tmpExchng;
180: } catch (Exception e) {
181: e.printStackTrace();
182:
183: return null;
184: }
185: }
186:
187: /**
188: * Updates the message exchange.
189: *
190: * @param msg message exchange.
191: * @param in in message
192: *
193: * @return true / false depending if update was successful or not
194: */
195: public static boolean updateInMessage(MessageExchange msg,
196: NormalizedMessage in) {
197: if (msg == null) {
198: return false;
199: }
200:
201: if (in != null) {
202: return createIn(msg, in);
203: }
204:
205: return true;
206: }
207:
208: /**
209: * Updates the message accding to the arguments provided.
210: *
211: * @param msg ME
212: * @param out Out message
213: * @param error errror message
214: * @param fault fault
215: *
216: * @return true if update is successfull.
217: */
218: public static boolean updateMessage(MessageExchange msg,
219: NormalizedMessage out, Exception error, Fault fault) {
220: if (msg == null) {
221: return false;
222: }
223:
224: if (out != null) {
225: return createOut(msg, out);
226: }
227:
228: if (fault != null) {
229: return createFault(msg, fault);
230: }
231:
232: if (error != null) {
233: return createError(msg, error);
234: }
235:
236: try {
237: msg.setStatus(ExchangeStatus.DONE);
238:
239: if (msg.getProperty(ConfigData.RESPONSE_SEQ_ID) == null) {
240: String id = SequencingEngineUtil.getTrackingId();
241: msg.setProperty(ConfigData.RESPONSE_SEQ_ID, id);
242: }
243: } catch (Exception e) {
244: e.printStackTrace();
245:
246: return false;
247: }
248:
249: return true;
250: }
251:
252: /**
253: * Creates an error message.
254: *
255: * @param msg ME
256: * @param error exception
257: *
258: * @return true/ false.
259: */
260: private static boolean createError(MessageExchange msg,
261: Exception error) {
262: try {
263: if (error == null) {
264: msg.setError(new Exception("Unknown Exception"));
265:
266: return true;
267: }
268:
269: msg.setError(error);
270:
271: if (msg.getProperty(ConfigData.RESPONSE_SEQ_ID) == null) {
272: String id = SequencingEngineUtil.getTrackingId();
273: msg.setProperty(ConfigData.RESPONSE_SEQ_ID, id);
274: }
275: } catch (Exception me) {
276: me.printStackTrace();
277:
278: return false;
279: }
280:
281: return true;
282: }
283:
284: /**
285: * Creates a fault message.
286: *
287: * @param msg ME
288: * @param flt fault
289: *
290: * @return true / false
291: */
292: private static boolean createFault(MessageExchange msg, Fault flt) {
293: if (flt == null) {
294: return false;
295: }
296:
297: try {
298: if (msg.getPattern().toString().trim().equals(
299: PatternRegistry.IN_ONLY)) {
300: msg.setStatus(ExchangeStatus.ERROR);
301: }
302:
303: msg.setFault(flt);
304: } catch (Exception e) {
305: e.printStackTrace();
306:
307: return false;
308: }
309:
310: return true;
311: }
312:
313: /**
314: * Creates an In Message.
315: *
316: * @param msg ME
317: * @param in in message
318: *
319: * @return true / false.
320: */
321: private static boolean createIn(MessageExchange msg,
322: NormalizedMessage in) {
323: String id = SequencingEngineUtil.getTrackingId();
324:
325: try {
326: if (msg.getPattern().toString().trim().equals(
327: PatternRegistry.IN_OUT)) {
328: ((InOut) (msg)).setInMessage(in);
329: msg.setProperty(ConfigData.REQUEST_SEQ_ID, id);
330: } else if (msg.getPattern().toString().trim().equals(
331: PatternRegistry.IN_ONLY)) {
332: ((InOnly) (msg)).setInMessage(in);
333: msg.setProperty(ConfigData.REQUEST_SEQ_ID, id);
334: } else if (msg.getPattern().toString().trim().equals(
335: PatternRegistry.ROBUST_IN_ONLY)) {
336: ((RobustInOnly) (msg)).setInMessage(in);
337: msg.setProperty(ConfigData.REQUEST_SEQ_ID, id);
338: } else {
339: return false;
340: }
341: } catch (Exception e) {
342: e.printStackTrace();
343:
344: return false;
345: }
346:
347: return true;
348: }
349:
350: /**
351: * Creates the out message.
352: *
353: * @param msg ME
354: * @param out out message
355: *
356: * @return true /false.
357: */
358: private static boolean createOut(MessageExchange msg,
359: NormalizedMessage out) {
360: String id = SequencingEngineUtil.getTrackingId();
361:
362: try {
363: if (msg.getPattern().toString().trim().equals(
364: PatternRegistry.IN_OUT)) {
365: ((InOut) (msg)).setOutMessage(out);
366: msg.setProperty(ConfigData.RESPONSE_SEQ_ID, id);
367: } else if (msg.getPattern().toString().trim().equals(
368: PatternRegistry.IN_ONLY)) {
369: msg.setStatus(ExchangeStatus.DONE);
370: msg.setProperty(ConfigData.RESPONSE_SEQ_ID, id);
371: } else if (msg.getPattern().toString().trim().equals(
372: PatternRegistry.ROBUST_IN_ONLY)) {
373: msg.setStatus(ExchangeStatus.DONE);
374: msg.setProperty(ConfigData.RESPONSE_SEQ_ID, id);
375: } else {
376: return false;
377: }
378: } catch (Exception e) {
379: e.printStackTrace();
380:
381: return false;
382: }
383:
384: return true;
385: }
386: }
|