KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001, 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55
56 package org.jboss.axis.transport.jms;
57
58 import org.jboss.axis.AxisEngine;
59 import org.jboss.axis.AxisFault;
60 import org.jboss.axis.MessageContext;
61 import org.jboss.axis.client.Call;
62 import org.jboss.axis.client.Transport;
63 import org.jboss.axis.utils.Messages;
64 import org.jboss.logging.Logger;
65
66 import javax.jms.Destination JavaDoc;
67 import java.util.HashMap JavaDoc;
68 import java.util.Iterator JavaDoc;
69
70 /**
71  * JMSTransport is the JMS-specific implemenation of org.jboss.axis.client.Transport.
72  * It implements the setupMessageContextImpl() function to set JMS-specific message
73  * context fields and transport chains. Connector and connection factory
74  * properties are passed in during instantiation and are in turn passed through
75  * when creating a connector.
76  *
77  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
78  * @author Richard Chung (rchung@sonicsoftware.com)
79  * @author Dave Chappell (chappell@sonicsoftware.com)
80  */

81 public class JMSTransport extends Transport
82 {
83    protected static Logger log = Logger.getLogger(JMSTransport.class);
84
85    private HashMap JavaDoc connectors;
86    private HashMap JavaDoc connectorProps;
87    private HashMap JavaDoc connectionFactoryProps;
88    private JMSConnector defaultConnector;
89    private HashMap JavaDoc passwords;
90    private Object JavaDoc connectorLock;
91
92    public JMSTransport(HashMap JavaDoc connectorProps,
93                        HashMap JavaDoc connectionFactoryProps)
94    {
95       transportName = "JMSTransport";
96       connectors = new HashMap JavaDoc();
97       passwords = new HashMap JavaDoc();
98       this.connectorProps = connectorProps;
99       this.connectionFactoryProps = connectionFactoryProps;
100       connectorLock = new Object JavaDoc();
101    }
102
103    /**
104     * Set up any transport-specific derived properties in the message context.
105     * @param context the context to set up
106     * @param message the client service instance
107     * @param engine the engine containing the registries
108     * @throws AxisFault if service cannot be found
109     */

110    public void setupMessageContextImpl(MessageContext context,
111                                        Call message,
112                                        AxisEngine engine)
113            throws AxisFault
114    {
115       String JavaDoc username = message.getUsername();
116       JMSConnector connector = null;
117       try
118       {
119          if (username == null)
120          {
121             initConnectorIfNecessary();
122             connector = defaultConnector;
123          }
124          else
125          {
126             String JavaDoc password = message.getPassword();
127             synchronized (connectorLock)
128             {
129                if (connectors.containsKey(username))
130                {
131                   String JavaDoc oldPassword = (String JavaDoc)passwords.get(username);
132                   if (password.equals(oldPassword))
133                      connector = (JMSConnector)connectors.get(username);
134                   else
135                      throw new AxisFault("badUserPass");
136                }
137                else
138                {
139                   connector = createConnector(username, password);
140                   connectors.put(username, connector);
141                   // I should really md5 hash these
142
passwords.put(username, password);
143                }
144             }
145          }
146       }
147       catch (Exception JavaDoc e)
148       {
149          log.error(Messages.getMessage("cannotConnectError"), e);
150
151          if (e instanceof AxisFault)
152             throw (AxisFault)e;
153          throw new AxisFault("cannotConnect", e);
154       }
155
156       context.setProperty(JMSConstants.CONNECTOR, connector);
157
158       //I would like to use the following, but that requires JMS-URL syntax
159
//which I don't have so I will rely on message properties for now
160
//String destination = message.getTargetEndpointAddress();
161

162       Object JavaDoc tmp = message.getProperty(JMSConstants.DESTINATION);
163       if (tmp != null && (tmp instanceof String JavaDoc || tmp instanceof Destination JavaDoc))
164          context.setProperty(JMSConstants.DESTINATION, tmp);
165       else
166          context.removeProperty(JMSConstants.DESTINATION);
167
168       tmp = message.getProperty(JMSConstants.WAIT_FOR_RESPONSE);
169       if (tmp != null && tmp instanceof Boolean JavaDoc)
170          context.setProperty(JMSConstants.WAIT_FOR_RESPONSE, tmp);
171       else
172          context.removeProperty(JMSConstants.WAIT_FOR_RESPONSE);
173
174       tmp = message.getProperty(JMSConstants.DELIVERY_MODE);
175       if (tmp != null && tmp instanceof Integer JavaDoc)
176          context.setProperty(JMSConstants.DELIVERY_MODE, tmp);
177       else
178          context.removeProperty(JMSConstants.DELIVERY_MODE);
179
180       tmp = message.getProperty(JMSConstants.PRIORITY);
181       if (tmp != null && tmp instanceof Integer JavaDoc)
182          context.setProperty(JMSConstants.PRIORITY, tmp);
183       else
184          context.removeProperty(JMSConstants.PRIORITY);
185
186       tmp = message.getProperty(JMSConstants.TIME_TO_LIVE);
187       if (tmp != null && tmp instanceof Long JavaDoc)
188          context.setProperty(JMSConstants.TIME_TO_LIVE, tmp);
189       else
190          context.removeProperty(JMSConstants.TIME_TO_LIVE);
191
192    }
193
194    private void initConnectorIfNecessary()
195            throws Exception JavaDoc
196    {
197       if (defaultConnector != null)
198          return;
199       synchronized (connectorLock)
200       {
201          //this is to catch a race issue when n threads do the null check
202
//before one of them actually creates the default connector
203
if (defaultConnector != null)
204             return;
205          defaultConnector = createConnector(null, null);
206       }
207    }
208
209    private JMSConnector createConnector(String JavaDoc username, String JavaDoc password)
210            throws Exception JavaDoc
211    {
212       JMSConnector connector = JMSConnectorFactory.
213               createClientConnector(connectorProps, connectionFactoryProps,
214                       username, password);
215       connector.start();
216       return connector;
217    }
218
219    /**
220     * Shuts down the connectors managed by this JMSTransport.
221     */

222    public void shutdown()
223    {
224       synchronized (connectorLock)
225       {
226          if (defaultConnector != null)
227             defaultConnector.shutdown();
228
229          Iterator JavaDoc connectorIter = connectors.values().iterator();
230          while (connectorIter.hasNext())
231          {
232             ((JMSConnector)connectorIter.next()).shutdown();
233          }
234       }
235    }
236
237 }
Popular Tags