KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > components > jms > JMSVendorAdapter


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.components.jms;
18
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.jms.InvalidDestinationException JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.JMSSecurityException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.Queue JavaDoc;
28 import javax.jms.QueueConnectionFactory JavaDoc;
29 import javax.jms.QueueSession JavaDoc;
30 import javax.jms.Topic JavaDoc;
31 import javax.jms.TopicConnectionFactory JavaDoc;
32 import javax.jms.TopicSession JavaDoc;
33
34 import org.apache.axis.MessageContext;
35 import org.apache.axis.client.Call;
36 import org.apache.axis.transport.jms.JMSConstants;
37 import org.apache.axis.transport.jms.JMSURLHelper;
38
39 /**
40  * SPI Interface that all JMSVendorAdaptors must implement. Allows for
41  * ConnectionFactory creation and Destination lookup
42  *
43  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
44  * @author Ray Chun (rchun@sonicsoftware.com)
45  */

46 public abstract class JMSVendorAdapter
47 {
48     public static final int SEND_ACTION = 0;
49     public static final int CONNECT_ACTION = 1;
50     public static final int SUBSCRIBE_ACTION = 2;
51     public static final int RECEIVE_ACTION = 3;
52     public static final int ON_EXCEPTION_ACTION = 4;
53
54     public abstract QueueConnectionFactory JavaDoc getQueueConnectionFactory(HashMap JavaDoc cfProps)
55         throws Exception JavaDoc;
56     public abstract TopicConnectionFactory JavaDoc getTopicConnectionFactory(HashMap JavaDoc cfProps)
57         throws Exception JavaDoc;
58
59     // let adapters add vendor-specific properties or override standard ones
60
public abstract void addVendorConnectionFactoryProperties(JMSURLHelper jmsurl, HashMap JavaDoc cfProps);
61
62     // let adapters match connectors using vendor-specific connection factory properties
63
public abstract boolean isMatchingConnectionFactory(javax.jms.ConnectionFactory JavaDoc cf, JMSURLHelper jmsurl, HashMap JavaDoc cfProps);
64
65     // returns <adapter> in 'org.apache.axis.components.jms.<adapter>VendorAdapter'
66
public String JavaDoc getVendorId()
67     {
68         String JavaDoc name = this.getClass().getName();
69
70         // cut off the trailing 'VendorAdapter'
71
if (name.endsWith(JMSConstants.ADAPTER_POSTFIX))
72         {
73             int index = name.lastIndexOf(JMSConstants.ADAPTER_POSTFIX);
74             name = name.substring(0,index);
75         }
76
77         // cut off the leading 'org.apache.axis.components.jms.'
78
int index = name.lastIndexOf(".");
79         if (index > 0)
80             name = name.substring(index+1);
81
82         return name;
83     }
84
85     /**
86      * Creates a JMS connector property table using values supplied in
87      * the endpoint address. Properties are translated from the short form
88      * in the endpoint address to the long form (prefixed by "transport.jms.")
89      *
90      * @param jmsurl the endpoint address
91      * @return the set of properties to be used for instantiating the JMS connector
92      */

93     public HashMap JavaDoc getJMSConnectorProperties(JMSURLHelper jmsurl)
94     {
95         HashMap JavaDoc connectorProps = new HashMap JavaDoc();
96
97         // the JMS URL may be useful when matching connectors
98
connectorProps.put(JMSConstants.JMS_URL, jmsurl);
99
100         // JMSConstants.CLIENT_ID,
101
String JavaDoc clientID = jmsurl.getPropertyValue(JMSConstants._CLIENT_ID);
102         if (clientID != null)
103             connectorProps.put(JMSConstants.CLIENT_ID, clientID);
104
105         // JMSConstants.CONNECT_RETRY_INTERVAL,
106
String JavaDoc connectRetryInterval = jmsurl.getPropertyValue(JMSConstants._CONNECT_RETRY_INTERVAL);
107         if (connectRetryInterval != null)
108             connectorProps.put(JMSConstants.CONNECT_RETRY_INTERVAL, connectRetryInterval);
109
110         // JMSConstants.INTERACT_RETRY_INTERVAL,
111
String JavaDoc interactRetryInterval = jmsurl.getPropertyValue(JMSConstants._INTERACT_RETRY_INTERVAL);
112         if (interactRetryInterval != null)
113             connectorProps.put(JMSConstants.INTERACT_RETRY_INTERVAL, interactRetryInterval);
114
115         // JMSConstants.DOMAIN
116
String JavaDoc domain = jmsurl.getPropertyValue(JMSConstants._DOMAIN);
117         if (domain != null)
118             connectorProps.put(JMSConstants.DOMAIN, domain);
119
120         // JMSConstants.NUM_RETRIES
121
String JavaDoc numRetries = jmsurl.getPropertyValue(JMSConstants._NUM_RETRIES);
122         if (numRetries != null)
123             connectorProps.put(JMSConstants.NUM_RETRIES, numRetries);
124
125         // JMSConstants.NUM_SESSIONS
126
String JavaDoc numSessions = jmsurl.getPropertyValue(JMSConstants._NUM_SESSIONS);
127         if (numSessions != null)
128             connectorProps.put(JMSConstants.NUM_SESSIONS, numSessions);
129
130         // JMSConstants.TIMEOUT_TIME,
131
String JavaDoc timeoutTime = jmsurl.getPropertyValue(JMSConstants._TIMEOUT_TIME);
132         if (timeoutTime != null)
133             connectorProps.put(JMSConstants.TIMEOUT_TIME, timeoutTime);
134
135         return connectorProps;
136     }
137
138     /**
139      *
140      * Creates a connection factory property table using values supplied in
141      * the endpoint address
142      *
143      * @param jmsurl the endpoint address
144      * @return the set of properties to be used for instantiating the connection factory
145      */

146     public HashMap JavaDoc getJMSConnectionFactoryProperties(JMSURLHelper jmsurl)
147     {
148         HashMap JavaDoc cfProps = new HashMap JavaDoc();
149
150         // hold on to the original address (this will be useful when the JNDI vendor adapter
151
// matches connectors)
152
cfProps.put(JMSConstants.JMS_URL, jmsurl);
153
154         // JMSConstants.DOMAIN
155
String JavaDoc domain = jmsurl.getPropertyValue(JMSConstants._DOMAIN);
156         if (domain != null)
157             cfProps.put(JMSConstants.DOMAIN, domain);
158
159         // allow vendors to customize the cf properties table
160
addVendorConnectionFactoryProperties(jmsurl, cfProps);
161
162         return cfProps;
163     }
164
165     public Queue JavaDoc getQueue(QueueSession JavaDoc session, String JavaDoc name)
166         throws Exception JavaDoc
167     {
168         return session.createQueue(name);
169     }
170
171     public Topic JavaDoc getTopic(TopicSession JavaDoc session, String JavaDoc name)
172         throws Exception JavaDoc
173     {
174         return session.createTopic(name);
175     }
176
177     public boolean isRecoverable(Throwable JavaDoc thrown, int action)
178     {
179         if(thrown instanceof RuntimeException JavaDoc ||
180            thrown instanceof Error JavaDoc ||
181            thrown instanceof JMSSecurityException JavaDoc ||
182            thrown instanceof InvalidDestinationException JavaDoc)
183             return false;
184         if(action == ON_EXCEPTION_ACTION)
185             return false;
186         return true;
187     }
188
189     public void setProperties(Message JavaDoc message, HashMap JavaDoc props)
190         throws JMSException JavaDoc
191     {
192         Iterator JavaDoc iter = props.keySet().iterator();
193         while (iter.hasNext())
194         {
195             String JavaDoc key = (String JavaDoc)iter.next();
196             String JavaDoc value = (String JavaDoc)props.get(key);
197
198             message.setStringProperty(key, value);
199         }
200     }
201
202     /**
203      * Set JMS properties in the message context.
204      *
205      * TODO: just copy all properties that are not used for the JMS connector
206      * or connection factory
207      */

208     public void setupMessageContext(MessageContext context,
209                                     Call call,
210                                     JMSURLHelper jmsurl)
211     {
212         Object JavaDoc tmp = null;
213
214         String JavaDoc jmsurlDestination = null;
215         if (jmsurl != null)
216             jmsurlDestination = jmsurl.getDestination();
217         if (jmsurlDestination != null)
218             context.setProperty(JMSConstants.DESTINATION, jmsurlDestination);
219         else
220         {
221             tmp = call.getProperty(JMSConstants.DESTINATION);
222             if (tmp != null && tmp instanceof String JavaDoc)
223                 context.setProperty(JMSConstants.DESTINATION, tmp);
224             else
225                 context.removeProperty(JMSConstants.DESTINATION);
226         }
227
228         String JavaDoc delivMode = null;
229         if (jmsurl != null)
230             delivMode = jmsurl.getPropertyValue(JMSConstants._DELIVERY_MODE);
231         if (delivMode != null)
232         {
233             int mode = JMSConstants.DEFAULT_DELIVERY_MODE;
234             if (delivMode.equalsIgnoreCase(JMSConstants.DELIVERY_MODE_PERSISTENT))
235                 mode = javax.jms.DeliveryMode.PERSISTENT;
236             else if (delivMode.equalsIgnoreCase(JMSConstants.DELIVERY_MODE_NONPERSISTENT))
237                 mode = javax.jms.DeliveryMode.NON_PERSISTENT;
238             context.setProperty(JMSConstants.DELIVERY_MODE, new Integer JavaDoc(mode));
239         }
240         else
241         {
242             tmp = call.getProperty(JMSConstants.DELIVERY_MODE);
243             if(tmp != null && tmp instanceof Integer JavaDoc)
244                 context.setProperty(JMSConstants.DELIVERY_MODE, tmp);
245             else
246                 context.removeProperty(JMSConstants.DELIVERY_MODE);
247         }
248
249         String JavaDoc prio = null;
250         if (jmsurl != null)
251             prio = jmsurl.getPropertyValue(JMSConstants._PRIORITY);
252         if (prio != null)
253             context.setProperty(JMSConstants.PRIORITY, Integer.valueOf(prio));
254         else
255         {
256             tmp = call.getProperty(JMSConstants.PRIORITY);
257             if(tmp != null && tmp instanceof Integer JavaDoc)
258                 context.setProperty(JMSConstants.PRIORITY, tmp);
259             else
260                 context.removeProperty(JMSConstants.PRIORITY);
261         }
262
263         String JavaDoc ttl = null;
264         if (jmsurl != null)
265             ttl = jmsurl.getPropertyValue(JMSConstants._TIME_TO_LIVE);
266         if (ttl != null)
267             context.setProperty(JMSConstants.TIME_TO_LIVE, Long.valueOf(ttl));
268         else
269         {
270             tmp = call.getProperty(JMSConstants.TIME_TO_LIVE);
271             if(tmp != null && tmp instanceof Long JavaDoc)
272                 context.setProperty(JMSConstants.TIME_TO_LIVE, tmp);
273             else
274                 context.removeProperty(JMSConstants.TIME_TO_LIVE);
275         }
276
277         String JavaDoc wait = null;
278         if (jmsurl != null)
279             wait = jmsurl.getPropertyValue(JMSConstants._WAIT_FOR_RESPONSE);
280         if (wait != null)
281             context.setProperty(JMSConstants.WAIT_FOR_RESPONSE, Boolean.valueOf(wait));
282         else
283         {
284             tmp = call.getProperty(JMSConstants.WAIT_FOR_RESPONSE);
285             if(tmp != null && tmp instanceof Boolean JavaDoc)
286                 context.setProperty(JMSConstants.WAIT_FOR_RESPONSE, tmp);
287             else
288                 context.removeProperty(JMSConstants.WAIT_FOR_RESPONSE);
289         }
290         setupApplicationProperties(context, call, jmsurl);
291     }
292     
293     public void setupApplicationProperties(MessageContext context,
294                                     Call call,
295                                     JMSURLHelper jmsurl)
296     {
297         //start with application properties from the URL
298
Map JavaDoc appProps = new HashMap JavaDoc();
299         if (jmsurl != null && jmsurl.getApplicationProperties() != null) {
300             for(Iterator JavaDoc itr=jmsurl.getApplicationProperties().iterator();
301                 itr.hasNext();) {
302                 String JavaDoc name = (String JavaDoc)itr.next();
303                 appProps.put(name,jmsurl.getPropertyValue(name));
304             }
305         }
306         
307         //next add application properties from the message context
308
Map JavaDoc ctxProps =
309            (Map JavaDoc)context.getProperty(JMSConstants.JMS_APPLICATION_MSG_PROPS);
310         if (ctxProps != null) {
311             appProps.putAll(ctxProps);
312         }
313         
314         //finally add the properties from the call
315
Map JavaDoc callProps =
316             (Map JavaDoc)call.getProperty(JMSConstants.JMS_APPLICATION_MSG_PROPS);
317         if (callProps != null) {
318             appProps.putAll(callProps);
319         }
320         
321         //now tore these properties within the context
322
context.setProperty(JMSConstants.JMS_APPLICATION_MSG_PROPS,appProps);
323     }
324 }
Popular Tags