PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/esb-components/jms-binding/runtime/src/com/sun/jbi/binding/jms/handler/OutboundMessageHandler.java

https://bitbucket.org/rsaqc/openesb-core
Java | 440 lines | 289 code | 93 blank | 58 comment | 49 complexity | 22a276f562dc25fd5c65f125aa924965 MD5 | raw file
  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-esb.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-esb.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)OutboundMessageHandler.java
  24. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  25. *
  26. * END_HEADER - DO NOT EDIT
  27. */
  28. package com.sun.jbi.binding.jms.handler;
  29. import com.sun.jbi.StringTranslator;
  30. import com.sun.jbi.wsdl11wrapper.Wsdl11WrapperHelper;
  31. import com.sun.jbi.binding.jms.EndpointBean;
  32. import com.sun.jbi.binding.jms.EndpointStatus;
  33. import com.sun.jbi.binding.jms.JMSBindingContext;
  34. import com.sun.jbi.binding.jms.mq.MQDestination;
  35. import com.sun.jbi.binding.jms.mq.MQManager;
  36. import com.sun.jbi.binding.jms.mq.MQSession;
  37. import com.sun.jbi.binding.jms.config.ConfigConstants;
  38. import java.util.logging.Logger;
  39. import javax.jbi.JBIException;
  40. import javax.jbi.messaging.MessageExchange;
  41. import javax.jms.Destination;
  42. import javax.jms.Message;
  43. import javax.xml.namespace.QName;
  44. /**
  45. * This class handles only In MEPS
  46. */
  47. public class OutboundMessageHandler
  48. extends MessageHandlerImpl
  49. {
  50. /**
  51. * Creates a new OutboundMessageHandler object.
  52. */
  53. public OutboundMessageHandler()
  54. {
  55. super();
  56. }
  57. /**
  58. * Checks the message.
  59. */
  60. public void check()
  61. {
  62. Message msg = getJMSMessage();
  63. MessageExchange exch = getNMSMessage();
  64. QName operation = null;
  65. EndpointBean eb = null;
  66. if (exch != null)
  67. {
  68. operation = exch.getOperation();
  69. try
  70. {
  71. eb = (EndpointBean) (
  72. JMSBindingContext.getInstance().getRegistry()
  73. .getEndpoint(exch.getEndpoint())
  74. );
  75. }
  76. catch (javax.jbi.JBIException je)
  77. {
  78. ;
  79. }
  80. if ((eb == null) || (!eb.getStatus().equals(EndpointStatus.STARTED)))
  81. {
  82. setError(mStringTranslator.getString(JMS_NO_ENDPOINT,
  83. exch.getExchangeId(), exch.getEndpoint().getServiceName().toString(),
  84. exch.getEndpoint().getEndpointName()));
  85. return;
  86. }
  87. String mep = eb.getMEP(operation.getLocalPart());
  88. if (mep == null)
  89. {
  90. setError(mStringTranslator.getString(
  91. JMS_HANDLER_INVALID_OPERATION, exch.getOperation(),
  92. exch.getExchangeId(), exch.getEndpoint().getServiceName().toString(),
  93. exch.getEndpoint().getEndpointName()));
  94. return;
  95. }
  96. }
  97. else
  98. {
  99. mLogger.severe(mStringTranslator.getString(JMS_IRRECOVERABLE_ERROR));
  100. setError(mStringTranslator.getString(JMS_IRRECOVERABLE_ERROR));
  101. return;
  102. }
  103. setBean(eb);
  104. setCurrentOperation(operation);
  105. }
  106. /**
  107. * Exceute the handler.
  108. */
  109. public void execute()
  110. {
  111. super.execute();
  112. /* do sanity first
  113. */
  114. check();
  115. if (!isValid())
  116. {
  117. mLogger.severe("**INVALID MESSAGE**");
  118. sendNMSStatus();
  119. return;
  120. }
  121. if (getJMSMessage() != null)
  122. {
  123. processJMSMessage();
  124. if (!isValid())
  125. {
  126. mLogger.severe(mStringTranslator.getString(JMS_MESSAGE_FAILURE));
  127. sendNMSStatus();
  128. return;
  129. }
  130. }
  131. else if (getNMSMessage() != null)
  132. {
  133. processNMSMessage();
  134. if (!isValid())
  135. {
  136. mLogger.severe(mStringTranslator.getString(JMS_MESSAGE_FAILURE));
  137. sendNMSStatus();
  138. return;
  139. }
  140. }
  141. else
  142. {
  143. mLogger.severe(mStringTranslator.getString(JMS_MESSAGE_FAILURE));
  144. return;
  145. }
  146. }
  147. /**
  148. * Process JMS message.
  149. */
  150. private void processJMSMessage()
  151. {
  152. Message msg = getJMSMessage();
  153. EndpointBean eb = getBean();
  154. MessageAdaptor adaptor = MessageAdaptorFactory.getAdaptor(msg,
  155. eb.getDeploymentType());
  156. if (adaptor == null)
  157. {
  158. mLogger.severe(mStringTranslator.getString(JMS_UNSUPPORTED_MESSAGE));
  159. setError(
  160. mStringTranslator.getString(JMS_UNSUPPORTED_MESSAGE));
  161. return;
  162. }
  163. Wsdl11WrapperHelper wrapperhelper;
  164. if (eb.getDeploymentType().trim().equals("WSDL11"))
  165. {
  166. wrapperhelper = new Wsdl11WrapperHelper(eb.getWsdlDefinition());
  167. adaptor.setEpilogueProcessor(wrapperhelper);
  168. }
  169. if (eb == null)
  170. {
  171. mLogger.severe(mStringTranslator.getString(JMS_NO_ENDPOINT));
  172. setError(mStringTranslator.getString(JMS_NO_ENDPOINT));
  173. return;
  174. }
  175. String inputtype = eb.getOutputType(getCurrentOperation().getLocalPart());
  176. if (inputtype == null)
  177. {
  178. mLogger.severe(
  179. mStringTranslator.getString(JMS_INCONSISTENT_MESSAGE_TYPE));
  180. setError(
  181. mStringTranslator.getString(JMS_INCONSISTENT_MESSAGE_TYPE));
  182. return;
  183. }
  184. if (!adaptor.getName().equals(inputtype.trim()))
  185. {
  186. mLogger.severe(
  187. mStringTranslator.getString(JMS_INCONSISTENT_MESSAGE_TYPE));
  188. setError(
  189. mStringTranslator.getString(JMS_INCONSISTENT_MESSAGE_TYPE));
  190. //send errorr
  191. return;
  192. }
  193. setNMSHeaders();
  194. MessageExchange ex = getNMSMessage();
  195. if (ex == null)
  196. {
  197. mLogger.severe(mStringTranslator.getString(JMS_JMS_NMR_CONVERSION_FAILED, "NULL"));
  198. setError(mStringTranslator.getString(JMS_JMS_NMR_CONVERSION_FAILED, "NULL"));
  199. //free up any resources here. set them to null
  200. return;
  201. }
  202. adaptor.convertJMStoNMSMessage(getJMSMessage(), ex);
  203. if (ex == null)
  204. {
  205. mLogger.severe(mStringTranslator.getString(JMS_JMS_NMR_CONVERSION_FAILED,
  206. adaptor.getError()));
  207. setError(mStringTranslator.getString(JMS_JMS_NMR_CONVERSION_FAILED,
  208. adaptor.getError()));
  209. //free up any resources here. set them to null
  210. return;
  211. }
  212. if (!send(ex))
  213. {
  214. mLogger.severe("**SEND FAILED**");
  215. setError("**SEND FAILED**" + "\n" + getError());
  216. return;
  217. }
  218. Object replyto =
  219. MessageExchangeHelper.getDestinationName(adaptor.getJMSReplyTo(), eb);
  220. mLogger.fine("**REPLY DESTINATION " + replyto);
  221. registerMessage(ex.getExchangeId(), replyto);
  222. return;
  223. }
  224. /**
  225. * Process the NMR message.
  226. */
  227. private void processNMSMessage()
  228. {
  229. MessageExchange exch = getNMSMessage();
  230. if (canTerminateProvider())
  231. {
  232. return;
  233. }
  234. String oper = getCurrentOperation().getLocalPart();
  235. mLogger.fine("**OPERATION** " + oper);
  236. EndpointBean eb = getBean();
  237. mLogger.fine("**OUTPUT-TYPE**" + eb.getInputType(oper));
  238. MessageAdaptor adaptor =
  239. MessageAdaptorFactory.getAdaptor(eb.getInputType(oper),
  240. eb.getDeploymentType());
  241. MQSession session = getSession();
  242. if (session == null)
  243. {
  244. setError(mStringTranslator.getString(JMS_CANNOT_GET_SESSION));
  245. mLogger.severe(mStringTranslator.getString(JMS_CANNOT_GET_SESSION));
  246. return;
  247. }
  248. Message msg = session.createJMSMessage(eb.getInputType(oper));
  249. if (adaptor == null)
  250. {
  251. setError(
  252. mStringTranslator.getString(JMS_UNSUPPORTED_MESSAGE));
  253. mLogger.severe(
  254. mStringTranslator.getString(JMS_UNSUPPORTED_MESSAGE));
  255. return;
  256. }
  257. Wsdl11WrapperHelper wrapperhelper;
  258. if (eb.getDeploymentType().trim().equals("WSDL11"))
  259. {
  260. wrapperhelper = new Wsdl11WrapperHelper(eb.getWsdlDefinition());
  261. adaptor.setEpilogueProcessor(wrapperhelper);
  262. }
  263. MQManager man = JMSBindingContext.getInstance().getMQManager();
  264. MQDestination dest = man.getTemporaryQueue();
  265. if (dest == null)
  266. {
  267. setError(mStringTranslator.getString(JMS_CANNOT_GET_TEMP_DEST));
  268. mLogger.severe(mStringTranslator.getString(JMS_CANNOT_GET_TEMP_DEST));
  269. return;
  270. }
  271. adaptor.setJMSReplyTo(dest.getDestination());
  272. adaptor.convertNMStoJMSMessage(exch, msg);
  273. if (!adaptor.isValid())
  274. {
  275. setError(adaptor.getError());
  276. return;
  277. }
  278. String id =
  279. (String) exch.getProperty(MessageProperties.JMS_CORRELATION_ID);
  280. if (id == null)
  281. {
  282. id = exch.getExchangeId();
  283. }
  284. String destname =
  285. (String) eb.getValue(ConfigConstants.DESTINATION_NAME);
  286. mLogger.fine("**SENDING MESSAGE TO " + destname);
  287. // register upfront to avoid race conditions
  288. // we can deregister if not appllicable
  289. registerMessage(id, exch);
  290. if (!send(msg, destname, session))
  291. {
  292. mLogger.severe(mStringTranslator.getString(JMS_CANNOT_SEND));
  293. setError(mStringTranslator.getString(JMS_CANNOT_SEND));
  294. man.releaseSession(session);
  295. deRegisterMessage(id);
  296. return;
  297. }
  298. //send the message with reply to set to temporary queue
  299. clear();
  300. if ((exch.getPattern().toString().trim().equals(ConfigConstants.IN_ONLY)))
  301. {
  302. // life of an outonly ends here
  303. mLogger.fine(mStringTranslator.getString(JMS_SEND_STATUS_NMR));
  304. // dont register response not expected
  305. deRegisterMessage(id);
  306. sendNMSStatus();
  307. }
  308. man.releaseSession(session);
  309. }
  310. /**
  311. * Sends the JMS error.
  312. */
  313. private void sendJMSError()
  314. {
  315. EndpointBean eb = getBean();
  316. String mep = eb.getMEP(getCurrentOperation().getLocalPart());
  317. if ((mep == null) || (mep.trim().equals(ConfigConstants.IN_ONLY)))
  318. {
  319. return;
  320. }
  321. MessageAdaptor adaptor =
  322. MessageAdaptorFactory.getAdaptor(eb.getOutputType(getCurrentOperation().
  323. getLocalPart()), "");
  324. if (adaptor == null)
  325. {
  326. mLogger.severe(mStringTranslator.getString(JMS_IRRECOVERABLE_ERROR));
  327. return;
  328. }
  329. MQSession session = getSession();
  330. if (session == null)
  331. {
  332. mLogger.severe(mStringTranslator.getString(JMS_IRRECOVERABLE_ERROR));
  333. return;
  334. }
  335. Message msg =
  336. session.createJMSMessage(eb.getOutputType(getCurrentOperation().
  337. getLocalPart()));
  338. adaptor.updateJMSErrorMessage(msg, getError());
  339. MessageExchange exch = getNMSMessage();
  340. Object replyto =
  341. MessageExchangeHelper.getDestinationName(adaptor.getJMSReplyTo(), eb);
  342. if (!send(msg, replyto, session))
  343. {
  344. ;
  345. }
  346. }
  347. }