TransportImpl.java
上传用户:xfwatch
上传日期:2020-12-14
资源大小:872k
文件大小:6k
源码类别:

中间件编程

开发平台:

Java

  1. /*
  2.  * JBoss, Home of Professional Open Source
  3.  * Copyright 2008, Red Hat, Inc., and others contributors as indicated
  4.  * by the @authors tag. All rights reserved.
  5.  * See the copyright.txt in the distribution for a
  6.  * full listing of individual contributors.
  7.  * This copyrighted material is made available to anyone wishing to use,
  8.  * modify, copy, or redistribute it subject to the terms and conditions
  9.  * of the GNU Lesser General Public License, v. 2.1.
  10.  * This program is distributed in the hope that it will be useful, but WITHOUT A
  11.  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12.  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  13.  * You should have received a copy of the GNU Lesser General Public License,
  14.  * v.2.1 along with this distribution; if not, write to the Free Software
  15.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16.  * MA  02110-1301, USA.
  17.  */
  18. package org.jboss.blacktie.jatmibroker.core.transport.hybrid;
  19. import java.util.Properties;
  20. import javax.jms.Connection;
  21. import javax.jms.Destination;
  22. import javax.jms.JMSException;
  23. import javax.jms.Session;
  24. import javax.naming.Context;
  25. import javax.naming.NameNotFoundException;
  26. import org.apache.log4j.LogManager;
  27. import org.apache.log4j.Logger;
  28. import org.jboss.blacktie.jatmibroker.core.transport.EventListener;
  29. import org.jboss.blacktie.jatmibroker.core.transport.OrbManagement;
  30. import org.jboss.blacktie.jatmibroker.core.transport.Receiver;
  31. import org.jboss.blacktie.jatmibroker.core.transport.Sender;
  32. import org.jboss.blacktie.jatmibroker.core.transport.Transport;
  33. import org.jboss.blacktie.jatmibroker.xatmi.ConnectionException;
  34. import org.omg.CORBA.ORBPackage.InvalidName;
  35. import org.omg.CosNaming.NamingContextPackage.AlreadyBound;
  36. import org.omg.CosNaming.NamingContextPackage.CannotProceed;
  37. import org.omg.CosNaming.NamingContextPackage.NotFound;
  38. import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
  39. public class TransportImpl implements Runnable, Transport {
  40. private static final Logger log = LogManager.getLogger(TransportImpl.class);
  41. private Thread callbackThread;
  42. private OrbManagement orbManagement;
  43. private Context context;
  44. private Connection connection;
  45. private Session session;
  46. private Properties properties;
  47. TransportImpl(OrbManagement orbManagement, Context context,
  48. Connection connection, Properties properties) throws InvalidName,
  49. NotFound, CannotProceed,
  50. org.omg.CosNaming.NamingContextPackage.InvalidName,
  51. AdapterInactive, AlreadyBound, JMSException {
  52. log.debug("Creating transport");
  53. this.orbManagement = orbManagement;
  54. this.connection = connection;
  55. callbackThread = new Thread(this);
  56. callbackThread.setDaemon(true);
  57. callbackThread.start();
  58. /*
  59.  * String username = (String) properties.get("StompConnectUsr"); String
  60.  * password = (String) properties.get("StompConnectPwd"); if (username
  61.  * != null) { connection = factory.createConnection(username, password);
  62.  * } else { connection = factory.createConnection(); }
  63.  */
  64. this.connection.start();
  65. this.context = context;
  66. this.session = this.connection.createSession(false,
  67. Session.AUTO_ACKNOWLEDGE);
  68. this.properties = properties;
  69. log.debug("Created transport");
  70. }
  71. public void close() throws ConnectionException {
  72. log.debug("Close called");
  73. orbManagement.close();
  74. try {
  75. session.close();
  76. } catch (Throwable t) {
  77. throw new ConnectionException(-1, "Could not close the session", t);
  78. } finally {
  79. try {
  80. connection.close();
  81. } catch (Throwable t) {
  82. throw new ConnectionException(-1,
  83. "Could not close the connection", t);
  84. }
  85. }
  86. log.debug("Closed");
  87. }
  88. public void run() {
  89. log.debug("Running the orb");
  90. orbManagement.getOrb().run();
  91. }
  92. public Sender getSender(String serviceName) throws ConnectionException {
  93. log.debug("Get sender: " + serviceName);
  94. try {
  95. Destination destination = (Destination) context.lookup("/queue/"
  96. + serviceName);
  97. log.trace("Resolved destination");
  98. return new JMSSenderImpl(session, destination);
  99. } catch (NameNotFoundException e) {
  100. throw new ConnectionException(
  101. org.jboss.blacktie.jatmibroker.xatmi.Connection.TPENOENT,
  102. "Could not resolve destination: " + serviceName, e);
  103. } catch (Throwable t) {
  104. throw new ConnectionException(-1,
  105. "Could not create a service sender: " + t.getMessage(), t);
  106. }
  107. }
  108. public Sender createSender(Object destination) {
  109. String callback_ior = (String) destination;
  110. log.debug("Creating a sender for: " + callback_ior);
  111. org.omg.CORBA.Object serviceFactoryObject = orbManagement.getOrb()
  112. .string_to_object(callback_ior);
  113. CorbaSenderImpl sender = new CorbaSenderImpl(serviceFactoryObject,
  114. callback_ior);
  115. log.debug("Created sender");
  116. return sender;
  117. }
  118. public Receiver getReceiver(String serviceName) throws ConnectionException {
  119. log.debug("Creating a receiver: " + serviceName);
  120. try {
  121. Destination destination = (Destination) context.lookup("/queue/"
  122. + serviceName);
  123. log.debug("Resolved destination");
  124. return new JMSReceiverImpl(session, destination, properties);
  125. } catch (NameNotFoundException e) {
  126. throw new ConnectionException(
  127. org.jboss.blacktie.jatmibroker.xatmi.Connection.TPENOENT,
  128. "Could not resolve destination: " + serviceName, e);
  129. } catch (Throwable t) {
  130. throw new ConnectionException(-1,
  131. "Could not create the receiver on: " + serviceName, t);
  132. }
  133. }
  134. public Receiver createReceiver() throws ConnectionException {
  135. log.debug("Creating a receiver");
  136. return new CorbaReceiverImpl(null, orbManagement, properties);
  137. }
  138. public Receiver createReceiver(EventListener eventListener)
  139. throws ConnectionException {
  140. log.debug("Creating a receiver with event listener");
  141. return new CorbaReceiverImpl(eventListener, orbManagement, properties);
  142. }
  143. public OrbManagement getOrbManagement() {
  144. return orbManagement;
  145. }
  146. }