KickJava   Java API By Example, From Geeks To Geeks.

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


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.components.jms.JMSVendorAdapter;
20 import org.apache.axis.components.logger.LogFactory;
21 import org.apache.commons.logging.Log;
22
23 import java.util.HashMap JavaDoc;
24
25 /**
26  * JMSConnectorFactory is a factory class for creating JMSConnectors. It can
27  * create both client connectors and server connectors. A server connector
28  * is configured to allow asynchronous message receipt, while a client
29  * connector is not.
30  *
31  * JMSConnectorFactory can also be used to select an appropriately configured
32  * JMSConnector from an existing pool of connectors.
33  *
34  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
35  * @author Richard Chung (rchung@sonicsoftware.com)
36  * @author Dave Chappell (chappell@sonicsoftware.com)
37  * @author Ray Chun (rchun@sonicsoftware.com)
38  */

39 public abstract class JMSConnectorFactory
40 {
41     protected static Log log =
42             LogFactory.getLog(JMSConnectorFactory.class.getName());
43
44     /**
45      * Performs an initial check on the connector properties, and then defers
46      * to the vendor adapter for matching on the vendor-specific connection factory.
47      *
48      * @param connectors the list of potential matches
49      * @param connectorProps the set of properties to be used for matching the connector
50      * @param cfProps the set of properties to be used for matching the connection factory
51      * @param username the user requesting the connector
52      * @param password the password associated with the requesting user
53      * @param adapter the vendor adapter specified in the JMS URL
54      * @return a JMSConnector that matches the specified properties
55      */

56      public static JMSConnector matchConnector(java.util.Set JavaDoc connectors,
57                                                HashMap JavaDoc connectorProps,
58                                                HashMap JavaDoc cfProps,
59                                                String JavaDoc username,
60                                                String JavaDoc password,
61                                                JMSVendorAdapter adapter)
62     {
63         java.util.Iterator JavaDoc iter = connectors.iterator();
64         while (iter.hasNext())
65         {
66             JMSConnector conn = (JMSConnector) iter.next();
67
68             // username
69
String JavaDoc connectorUsername = conn.getUsername();
70             if (!( ((connectorUsername == null) && (username == null)) ||
71                    ((connectorUsername != null) && (username != null) && (connectorUsername.equals(username))) ))
72                 continue;
73
74             // password
75
String JavaDoc connectorPassword = conn.getPassword();
76             if (!( ((connectorPassword == null) && (password == null)) ||
77                    ((connectorPassword != null) && (password != null) && (connectorPassword.equals(password))) ))
78                 continue;
79
80             // num retries
81
int connectorNumRetries = conn.getNumRetries();
82             String JavaDoc propertyNumRetries = (String JavaDoc)connectorProps.get(JMSConstants.NUM_RETRIES);
83             int numRetries = JMSConstants.DEFAULT_NUM_RETRIES;
84             if (propertyNumRetries != null)
85                 numRetries = Integer.parseInt(propertyNumRetries);
86             if (connectorNumRetries != numRetries)
87                 continue;
88
89             // client id
90
String JavaDoc connectorClientID = conn.getClientID();
91             String JavaDoc clientID = (String JavaDoc)connectorProps.get(JMSConstants.CLIENT_ID);
92             if (!( ((connectorClientID == null) && (clientID == null))
93                    ||
94                    ((connectorClientID != null) && (clientID != null) && connectorClientID.equals(clientID)) ))
95                 continue;
96
97             // domain
98
String JavaDoc connectorDomain = (conn instanceof QueueConnector) ? JMSConstants.DOMAIN_QUEUE : JMSConstants.DOMAIN_TOPIC;
99             String JavaDoc propertyDomain = (String JavaDoc)connectorProps.get(JMSConstants.DOMAIN);
100             String JavaDoc domain = JMSConstants.DOMAIN_DEFAULT;
101             if (propertyDomain != null)
102                 domain = propertyDomain;
103             if (!( ((connectorDomain == null) && (domain == null))
104                    ||
105                    ((connectorDomain != null) && (domain != null) && connectorDomain.equalsIgnoreCase(domain)) ))
106                 continue;
107
108             // the connection factory must also match for the connector to be reused
109
JMSURLHelper jmsurl = conn.getJMSURL();
110             if (adapter.isMatchingConnectionFactory(conn.getConnectionFactory(), jmsurl, cfProps))
111             {
112                 // attempt to reserve the connector
113
try
114                 {
115                     JMSConnectorManager.getInstance().reserve(conn);
116
117                     if (log.isDebugEnabled()) {
118                         log.debug("JMSConnectorFactory: Found matching connector");
119                     }
120                 }
121                 catch (Exception JavaDoc e)
122                 {
123                     // ignore. the connector may be in the process of shutting down, so try the next element
124
continue;
125                 }
126
127                 return conn;
128             }
129         }
130
131         if (log.isDebugEnabled()) {
132             log.debug("JMSConnectorFactory: No matching connectors found");
133         }
134
135         return null;
136     }
137
138     /**
139      * Static method to create a server connector. Server connectors can
140      * accept incoming requests.
141      *
142      * @param connectorConfig
143      * @param cfConfig
144      * @param username
145      * @param password
146      * @return
147      * @throws Exception
148      */

149     public static JMSConnector createServerConnector(HashMap JavaDoc connectorConfig,
150                                               HashMap JavaDoc cfConfig,
151                                               String JavaDoc username,
152                                               String JavaDoc password,
153                                               JMSVendorAdapter adapter)
154         throws Exception JavaDoc
155     {
156         return createConnector(connectorConfig, cfConfig, true,
157                                username, password, adapter);
158     }
159
160     /**
161      * Static method to create a client connector. Client connectors cannot
162      * accept incoming requests.
163      *
164      * @param connectorConfig
165      * @param cfConfig
166      * @param username
167      * @param password
168      * @return
169      * @throws Exception
170      */

171     public static JMSConnector createClientConnector(HashMap JavaDoc connectorConfig,
172                                               HashMap JavaDoc cfConfig,
173                                               String JavaDoc username,
174                                               String JavaDoc password,
175                                               JMSVendorAdapter adapter)
176         throws Exception JavaDoc
177     {
178         return createConnector(connectorConfig, cfConfig, false,
179                                username, password, adapter);
180     }
181
182     private static JMSConnector createConnector(HashMap JavaDoc connectorConfig,
183                                                 HashMap JavaDoc cfConfig,
184                                                 boolean allowReceive,
185                                                 String JavaDoc username,
186                                                 String JavaDoc password,
187                                                 JMSVendorAdapter adapter)
188         throws Exception JavaDoc
189     {
190         if(connectorConfig != null)
191             connectorConfig = (HashMap JavaDoc)connectorConfig.clone();
192         int numRetries = MapUtils.removeIntProperty(connectorConfig,
193                                     JMSConstants.NUM_RETRIES,
194                                     JMSConstants.DEFAULT_NUM_RETRIES);
195
196         int numSessions = MapUtils.removeIntProperty(connectorConfig,
197                                     JMSConstants.NUM_SESSIONS,
198                                     JMSConstants.DEFAULT_NUM_SESSIONS);
199
200         long connectRetryInterval = MapUtils.removeLongProperty(connectorConfig,
201                                     JMSConstants.CONNECT_RETRY_INTERVAL,
202                                     JMSConstants.DEFAULT_CONNECT_RETRY_INTERVAL);
203
204         long interactRetryInterval = MapUtils.removeLongProperty(connectorConfig,
205                                     JMSConstants.INTERACT_RETRY_INTERVAL,
206                                     JMSConstants.DEFAULT_INTERACT_RETRY_INTERVAL);
207
208         long timeoutTime = MapUtils.removeLongProperty(connectorConfig,
209                                     JMSConstants.TIMEOUT_TIME,
210                                     JMSConstants.DEFAULT_TIMEOUT_TIME);
211
212         String JavaDoc clientID = MapUtils.removeStringProperty(connectorConfig,
213                                     JMSConstants.CLIENT_ID,
214                                     null);
215         String JavaDoc domain = MapUtils.removeStringProperty(connectorConfig,
216                                     JMSConstants.DOMAIN,
217                                     JMSConstants.DOMAIN_DEFAULT);
218
219         // this will be set if the target endpoint address was set on the Axis call
220
JMSURLHelper jmsurl = (JMSURLHelper)connectorConfig.get(JMSConstants.JMS_URL);
221
222         if(cfConfig == null)
223             throw new IllegalArgumentException JavaDoc("noCfConfig");
224
225         if(domain.equals(JMSConstants.DOMAIN_QUEUE))
226         {
227             return new QueueConnector(adapter.getQueueConnectionFactory(cfConfig),
228                                       numRetries, numSessions, connectRetryInterval,
229                                       interactRetryInterval, timeoutTime,
230                                       allowReceive, clientID, username, password,
231                                       adapter, jmsurl);
232         }
233         else // domain is Topic
234
{
235             return new TopicConnector(adapter.getTopicConnectionFactory(cfConfig),
236                                       numRetries, numSessions, connectRetryInterval,
237                                       interactRetryInterval, timeoutTime,
238                                       allowReceive, clientID, username, password,
239                                       adapter, jmsurl);
240         }
241     }
242 }
Popular Tags