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