KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > transport > jms > JMSTransport


1 /*
2  * Copyright 2001, 2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.axis.transport.jms;
18
19 import org.apache.axis.AxisEngine;
20 import org.apache.axis.AxisFault;
21 import org.apache.axis.MessageContext;
22 import org.apache.axis.client.Call;
23 import org.apache.axis.client.Transport;
24 import org.apache.axis.components.jms.JMSVendorAdapter;
25 import org.apache.axis.components.jms.JMSVendorAdapterFactory;
26 import org.apache.axis.components.logger.LogFactory;
27 import org.apache.axis.utils.Messages;
28 import org.apache.commons.logging.Log;
29
30 import java.util.HashMap JavaDoc;
31
32 /**
33  * JMSTransport is the JMS-specific implemenation of org.apache.axis.client.Transport.
34  * It implements the setupMessageContextImpl() function to set JMS-specific message
35  * context fields and transport chains.
36  *
37  * There are two
38  * Connector and connection factory
39  * properties are passed in during instantiation and are in turn passed through
40  * when creating a connector.
41  *
42  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
43  * @author Richard Chung (rchung@sonicsoftware.com)
44  * @author Dave Chappell (chappell@sonicsoftware.com)
45  * @author Ray Chun (rchun@sonicsoftware.com)
46  */

47 public class JMSTransport extends Transport
48 {
49     protected static Log log =
50             LogFactory.getLog(JMSTransport.class.getName());
51
52     private static HashMap JavaDoc vendorConnectorPools = new HashMap JavaDoc();
53
54     private HashMap JavaDoc defaultConnectorProps;
55     private HashMap JavaDoc defaultConnectionFactoryProps;
56
57     static
58     {
59         // add a shutdown hook to close JMS connections
60
Runtime.getRuntime().addShutdownHook(
61             new Thread JavaDoc()
62             {
63                 public void run()
64                 {
65                     JMSTransport.closeAllConnectors();
66                 }
67             }
68         );
69     }
70
71     public JMSTransport()
72     {
73         transportName = "JMSTransport";
74     }
75
76     // this cons is provided for clients that instantiate the JMSTransport directly
77
public JMSTransport(HashMap JavaDoc connectorProps,
78                         HashMap JavaDoc connectionFactoryProps)
79     {
80         this();
81         defaultConnectorProps = connectorProps;
82         defaultConnectionFactoryProps = connectionFactoryProps;
83     }
84
85     /**
86      * Set up any transport-specific derived properties in the message context.
87      * @param context the context to set up
88      * @param message the client service instance
89      * @param engine the engine containing the registries
90      * @throws AxisFault if service cannot be found
91      */

92     public void setupMessageContextImpl(MessageContext context,
93                                         Call message,
94                                         AxisEngine engine)
95         throws AxisFault
96     {
97         if (log.isDebugEnabled()) {
98             log.debug("Enter: JMSTransport::setupMessageContextImpl");
99         }
100
101         JMSConnector connector = null;
102         HashMap JavaDoc connectorProperties = null;
103         HashMap JavaDoc connectionFactoryProperties = null;
104
105         JMSVendorAdapter vendorAdapter = null;
106         JMSURLHelper jmsurl = null;
107
108         // a security context is required to create/use JMSConnectors
109
String JavaDoc username = message.getUsername();
110         String JavaDoc password = message.getPassword();
111
112         // the presence of an endpoint address indicates whether the client application
113
// is instantiating the JMSTransport directly (deprecated) or indirectly via JMS URL
114
String JavaDoc endpointAddr = message.getTargetEndpointAddress();
115         if (endpointAddr != null)
116         {
117             try
118             {
119                 // performs minimal validation ('jms:/destination?...')
120
jmsurl = new JMSURLHelper(new java.net.URL JavaDoc(endpointAddr));
121
122                 // lookup the appropriate vendor adapter
123
String JavaDoc vendorId = jmsurl.getVendor();
124                 if (vendorId == null)
125                     vendorId = JMSConstants.JNDI_VENDOR_ID;
126
127                 if (log.isDebugEnabled())
128                     log.debug("JMSTransport.setupMessageContextImpl(): endpt=" + endpointAddr +
129                               ", vendor=" + vendorId);
130
131                 vendorAdapter = JMSVendorAdapterFactory.getJMSVendorAdapter(vendorId);
132                 if (vendorAdapter == null)
133                 {
134                     throw new AxisFault("cannotLoadAdapterClass:" + vendorId);
135                 }
136
137                 // populate the connector and connection factory properties tables
138
connectorProperties = vendorAdapter.getJMSConnectorProperties(jmsurl);
139                 connectionFactoryProperties = vendorAdapter.getJMSConnectionFactoryProperties(jmsurl);
140             }
141             catch (java.net.MalformedURLException JavaDoc e)
142             {
143                 log.error(Messages.getMessage("malformedURLException00"), e);
144                 throw new AxisFault(Messages.getMessage("malformedURLException00"), e);
145             }
146         }
147         else
148         {
149             // the JMSTransport was instantiated directly, use the default adapter
150
vendorAdapter = JMSVendorAdapterFactory.getJMSVendorAdapter();
151             if (vendorAdapter == null)
152             {
153                 throw new AxisFault("cannotLoadAdapterClass");
154             }
155
156             // use the properties passed in to the constructor
157
connectorProperties = defaultConnectorProps;
158             connectionFactoryProperties = defaultConnectionFactoryProps;
159         }
160
161         try
162         {
163             connector = JMSConnectorManager.getInstance().getConnector(connectorProperties, connectionFactoryProperties,
164                                                            username, password, vendorAdapter);
165         }
166         catch (Exception JavaDoc e)
167         {
168             log.error(Messages.getMessage("cannotConnectError"), e);
169
170             if(e instanceof AxisFault)
171                 throw (AxisFault)e;
172             throw new AxisFault("cannotConnect", e);
173         }
174
175         // store these in the context for later use
176
context.setProperty(JMSConstants.CONNECTOR, connector);
177         context.setProperty(JMSConstants.VENDOR_ADAPTER, vendorAdapter);
178
179         // vendors may populate the message context
180
vendorAdapter.setupMessageContext(context, message, jmsurl);
181
182         if (log.isDebugEnabled()) {
183             log.debug("Exit: JMSTransport::setupMessageContextImpl");
184         }
185     }
186
187     /**
188      * Shuts down the connectors managed by this JMSTransport.
189      */

190     public void shutdown()
191     {
192         if (log.isDebugEnabled()) {
193             log.debug("Enter: JMSTransport::shutdown");
194         }
195
196         closeAllConnectors();
197
198         if (log.isDebugEnabled()) {
199             log.debug("Exit: JMSTransport::shutdown");
200         }
201     }
202
203     /**
204      * Closes all JMS connectors
205      */

206     public static void closeAllConnectors()
207     {
208         if (log.isDebugEnabled()) {
209             log.debug("Enter: JMSTransport::closeAllConnectors");
210         }
211
212         JMSConnectorManager.getInstance().closeAllConnectors();
213
214         if (log.isDebugEnabled()) {
215             log.debug("Exit: JMSTransport::closeAllConnectors");
216         }
217     }
218
219     /**
220      * Closes JMS connectors that match the specified endpoint address
221      *
222      * @param endpointAddr the JMS endpoint address
223      * @param username
224      * @param password
225      */

226     public static void closeMatchingJMSConnectors(String JavaDoc endpointAddr, String JavaDoc username, String JavaDoc password)
227     {
228         if (log.isDebugEnabled()) {
229             log.debug("Enter: JMSTransport::closeMatchingJMSConnectors");
230         }
231
232         try
233         {
234             JMSURLHelper jmsurl = new JMSURLHelper(new java.net.URL JavaDoc(endpointAddr));
235             String JavaDoc vendorId = jmsurl.getVendor();
236
237             JMSVendorAdapter vendorAdapter = null;
238             if (vendorId == null)
239                 vendorId = JMSConstants.JNDI_VENDOR_ID;
240             vendorAdapter = JMSVendorAdapterFactory.getJMSVendorAdapter(vendorId);
241
242             // the vendor adapter may not exist
243
if (vendorAdapter == null)
244                 return;
245
246             // determine the set of properties to be used for matching the connection
247
HashMap JavaDoc connectorProps = vendorAdapter.getJMSConnectorProperties(jmsurl);
248             HashMap JavaDoc cfProps = vendorAdapter.getJMSConnectionFactoryProperties(jmsurl);
249
250             JMSConnectorManager.getInstance().closeMatchingJMSConnectors(connectorProps, cfProps,
251                                                                          username, password,
252                                                                          vendorAdapter);
253         }
254         catch (java.net.MalformedURLException JavaDoc e)
255         {
256             log.warn(Messages.getMessage("malformedURLException00"), e);
257         }
258
259         if (log.isDebugEnabled()) {
260             log.debug("Exit: JMSTransport::closeMatchingJMSConnectors");
261         }
262     }
263 }
Popular Tags