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: * @(#)TextMessageAdaptor.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:
033: import java.io.Reader;
034: import java.io.StringReader;
035: import java.io.StringWriter;
036: import java.io.Writer;
037:
038: import java.util.logging.Logger;
039:
040: import javax.jbi.messaging.Fault;
041: import javax.jbi.messaging.MessageExchange;
042: import javax.jbi.messaging.NormalizedMessage;
043:
044: import javax.jbi.messaging.MessageExchange.Role;
045:
046: import javax.jms.JMSException;
047: import javax.jms.Message;
048: import javax.jms.TextMessage;
049:
050: import javax.xml.transform.Result;
051: import javax.xml.transform.Source;
052: import javax.xml.transform.Transformer;
053: import javax.xml.transform.TransformerFactory;
054:
055: import javax.xml.transform.stream.StreamResult;
056: import javax.xml.transform.stream.StreamSource;
057:
058: /**
059: * Text message adaptor.
060: *
061: * @author Sun Microsystems Inc.
062: */
063: public class TextMessageAdaptor extends MessageAdaptorImpl {
064: /**
065: * Creates a new TextMessageAdaptor object.
066: */
067: public TextMessageAdaptor() {
068: mLogger.info(mStringTranslator
069: .getString(JMS_TEXT_ADAPTOR_INVOKED));
070: }
071:
072: /**
073: * Gets name.
074: *
075: * @return string.
076: */
077: public String getName() {
078: return MessageProperties.TEXT_MESSAGE;
079: }
080:
081: /**
082: * Convert JMS to NMR msg.
083: *
084: * @param msg JMS msg.
085: * @param exch NMR exchange.
086: */
087: public void convertJMStoNMSMessage(Message msg, MessageExchange exch) {
088: super .convertJMStoNMSMessage(msg, exch);
089:
090: javax.jms.TextMessage textmsg = null;
091:
092: try {
093: Source stream = getSource((TextMessage) msg);
094: String status = getJMSStatus(msg);
095: NormalizedMessage nm = null;
096: Fault flt = null;
097: Exception exp = null;
098:
099: if (status.trim().equals(MessageProperties.SUCCESS)) {
100: nm = exch.createMessage();
101: nm.setContent(stream);
102: } else if (status.trim().equals(MessageProperties.FAULT)) {
103: flt = exch.createFault();
104: flt.setContent(stream);
105: } else if (status.trim().equals(MessageProperties.ERROR)) {
106: String expstring = getSourceAsString(stream);
107:
108: if (expstring == null) {
109: expstring = new String(mStringTranslator
110: .getString(JMS_NO_ERROR_RETURNED));
111: }
112:
113: exp = new Exception(expstring);
114: }
115:
116: if (exch.getRole().equals(MessageExchange.Role.PROVIDER)) {
117: MessageExchangeHelper.updateOutMessage(exch, nm, exp,
118: flt);
119: } else {
120: MessageExchangeHelper.updateInMessage(exch, nm);
121: }
122: } catch (Throwable e) {
123: e.printStackTrace();
124: setError(mStringTranslator.getString(JMS_ERROR_JMS_NMR));
125: setError(e.getMessage());
126: exch = null;
127: }
128: }
129:
130: /**
131: * Convert NMR to JMS msg.
132: *
133: * @param exch NMR msg.
134: * @param msg JMS msg.
135: */
136: public void convertNMStoJMSMessage(MessageExchange exch, Message msg) {
137: super .convertNMStoJMSMessage(exch, msg);
138:
139: javax.jms.TextMessage txtmsg = (javax.jms.TextMessage) msg;
140:
141: NormalizedMessage nm = null;
142:
143: if (exch.getRole().equals(MessageExchange.Role.PROVIDER)) {
144: nm = MessageExchangeHelper.getInMessage(exch);
145: } else {
146: nm = MessageExchangeHelper.getOutMessage(exch);
147: }
148:
149: Fault flt = MessageExchangeHelper.getFault(exch);
150: Exception exp = MessageExchangeHelper.getError(exch);
151:
152: mLogger.fine("Pattern " + exch.getPattern());
153: mLogger.fine("Service " + exch.getEndpoint().getServiceName());
154:
155: String txt = null;
156: String status = MessageProperties.UNKNOWN;
157:
158: if (nm != null) {
159: /* could be fault or error
160: */
161: txt = getOutputString(nm);
162: status = MessageProperties.SUCCESS;
163: } else if (flt != null) {
164: txt = getFaultString(flt);
165: status = MessageProperties.FAULT;
166: } else if (exp != null) {
167: txt = getErrorString(exp);
168: status = MessageProperties.ERROR;
169: } else {
170: txt = new String(mStringTranslator
171: .getString(JMS_NO_ERROR_RETURNED));
172: }
173:
174: setStatus(txtmsg, status);
175:
176: try {
177: txtmsg.setText(txt);
178: } catch (JMSException je) {
179: je.printStackTrace();
180: setException(je);
181: }
182: }
183:
184: /**
185: * Update JMS error msg.
186: *
187: * @param msg JMS msg.
188: * @param obj Object.
189: */
190: public void updateJMSErrorMessage(Message msg, Object obj) {
191: if (obj instanceof java.lang.String) {
192: String s = (String) obj;
193: } else {
194: ;
195: }
196: }
197:
198: /**
199: * Get error message.
200: *
201: * @param e exception.
202: *
203: * @return string.
204: */
205: private String getErrorString(Exception e) {
206: StringBuffer sb = new StringBuffer();
207:
208: StackTraceElement[] stckTrElem = e.getStackTrace();
209:
210: if (stckTrElem != null) {
211: for (int i = 0; i < stckTrElem.length; i++) {
212: String stckTrace = stckTrElem[i].toString();
213: sb.append(stckTrace);
214: sb.append("\n");
215: }
216: }
217:
218: return sb.toString();
219: }
220:
221: /**
222: * Get fault string.
223: *
224: * @param flt fault string.
225: *
226: * @return string.
227: */
228: private String getFaultString(Fault flt) {
229: Source src = flt.getContent();
230:
231: return getSourceAsString(src);
232: }
233:
234: /**
235: * Gets the output string.
236: *
237: * @param nm NM message.
238: *
239: * @return string.
240: */
241: private String getOutputString(NormalizedMessage nm) {
242: Source src = nm.getContent();
243:
244: return getSourceAsString(src);
245: }
246:
247: /**
248: * Returns the source.
249: *
250: * @param textmsg JMS text message.
251: *
252: * @return source.
253: */
254: private Source getSource(TextMessage textmsg) {
255: StreamSource stream = null;
256:
257: try {
258: mLogger.fine("JMS Message *" + textmsg.getText() + "*");
259:
260: String s = textmsg.getText();
261: StringReader reader = new StringReader(s.trim());
262: stream = new StreamSource(reader);
263: } catch (Exception e) {
264: ;
265: }
266:
267: return stream;
268: }
269:
270: /**
271: * Get source as string.
272: *
273: * @param doc soource doc.
274: *
275: * @return string.
276: */
277: private String getSourceAsString(Source doc) {
278: String s = null;
279: StringWriter out = null;
280:
281: try {
282: out = new StringWriter();
283:
284: TransformerFactory tFactory = TransformerFactory
285: .newInstance();
286: Transformer trans = tFactory.newTransformer();
287: StreamResult result = new StreamResult(out);
288: trans.transform(doc, result);
289: s = result.getWriter().toString();
290: out.close();
291: } catch (Throwable t) {
292: t.printStackTrace();
293: s = new String(mStringTranslator.getString(
294: JMS_OUTPUT_MSG_ERROR, t.getMessage()));
295: setError(t.getMessage());
296: }
297:
298: return s;
299: }
300: }
|