KickJava   Java API By Example, From Geeks To Geeks.

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


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.Hashtable JavaDoc;
21
22 import javax.jms.ConnectionFactory JavaDoc;
23 import javax.jms.Queue JavaDoc;
24 import javax.jms.QueueConnectionFactory JavaDoc;
25 import javax.jms.QueueSession JavaDoc;
26 import javax.jms.Topic JavaDoc;
27 import javax.jms.TopicConnectionFactory JavaDoc;
28 import javax.jms.TopicSession JavaDoc;
29 import javax.naming.Context JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 import org.apache.axis.transport.jms.JMSConstants;
33 import org.apache.axis.transport.jms.JMSURLHelper;
34
35 /**
36  * Uses JNDI to locate ConnectionFactory and Destinations
37  *
38  * @author Jaime Meritt (jmeritt@sonicsoftware.com)
39  * @author Ray Chun (rchun@sonicsoftware.com)
40  */

41 public class JNDIVendorAdapter extends JMSVendorAdapter
42 {
43     public final static String JavaDoc CONTEXT_FACTORY = "java.naming.factory.initial";
44     public final static String JavaDoc PROVIDER_URL = "java.naming.provider.url";
45
46     public final static String JavaDoc _CONNECTION_FACTORY_JNDI_NAME = "ConnectionFactoryJNDIName";
47     public final static String JavaDoc CONNECTION_FACTORY_JNDI_NAME = JMSConstants.JMS_PROPERTY_PREFIX +
48                                                                     _CONNECTION_FACTORY_JNDI_NAME;
49
50     private Context JavaDoc context;
51
52     public QueueConnectionFactory JavaDoc getQueueConnectionFactory(HashMap JavaDoc cfConfig)
53         throws Exception JavaDoc
54     {
55         return (QueueConnectionFactory JavaDoc)getConnectionFactory(cfConfig);
56     }
57
58     public TopicConnectionFactory JavaDoc getTopicConnectionFactory(HashMap JavaDoc cfConfig)
59         throws Exception JavaDoc
60     {
61         return (TopicConnectionFactory JavaDoc)getConnectionFactory(cfConfig);
62     }
63
64     private ConnectionFactory JavaDoc getConnectionFactory(HashMap JavaDoc cfProps)
65         throws Exception JavaDoc
66     {
67         if(cfProps == null)
68                 throw new IllegalArgumentException JavaDoc("noCFProps");
69         String JavaDoc jndiName = (String JavaDoc)cfProps.get(CONNECTION_FACTORY_JNDI_NAME);
70         if(jndiName == null || jndiName.trim().length() == 0)
71             throw new IllegalArgumentException JavaDoc("noCFName");
72
73         Hashtable JavaDoc environment = new Hashtable JavaDoc(cfProps);
74
75         // set the context factory if provided in the JMS URL
76
String JavaDoc ctxFactory = (String JavaDoc)cfProps.get(CONTEXT_FACTORY);
77         if (ctxFactory != null)
78             environment.put(CONTEXT_FACTORY, ctxFactory);
79
80         // set the provider url if provided in the JMS URL
81
String JavaDoc providerURL = (String JavaDoc)cfProps.get(PROVIDER_URL);
82         if (providerURL != null)
83             environment.put(PROVIDER_URL, providerURL);
84
85         context = new InitialContext JavaDoc(environment);
86
87         return (ConnectionFactory JavaDoc)context.lookup(jndiName);
88     }
89
90     /**
91      * Populates the connection factory config table with properties from
92      * the JMS URL query string
93      *
94      * @param jmsurl The target endpoint address of the Axis call
95      * @param cfConfig The set of properties necessary to create/configure the connection factory
96      */

97     public void addVendorConnectionFactoryProperties(JMSURLHelper jmsurl,
98                                                      HashMap JavaDoc cfConfig)
99     {
100         // add the connection factory jndi name
101
String JavaDoc cfJNDIName = jmsurl.getPropertyValue(_CONNECTION_FACTORY_JNDI_NAME);
102         if (cfJNDIName != null)
103             cfConfig.put(CONNECTION_FACTORY_JNDI_NAME, cfJNDIName);
104
105         // add the initial ctx factory
106
String JavaDoc ctxFactory = jmsurl.getPropertyValue(CONTEXT_FACTORY);
107         if (ctxFactory != null)
108             cfConfig.put(CONTEXT_FACTORY, ctxFactory);
109
110         // add the provider url
111
String JavaDoc providerURL = jmsurl.getPropertyValue(PROVIDER_URL);
112         if (providerURL != null)
113             cfConfig.put(PROVIDER_URL, providerURL);
114     }
115
116     /**
117      * Check that the attributes of the candidate connection factory match the
118      * requested connection factory properties.
119      *
120      * @param cf the candidate connection factory
121      * @param originalJMSURL the URL which was used to create the connection factory
122      * @param cfProps the set of properties that should be used to determine the match
123      * @return true or false to indicate whether a match has been found
124      */

125     public boolean isMatchingConnectionFactory(ConnectionFactory JavaDoc cf,
126                                                JMSURLHelper originalJMSURL,
127                                                HashMap JavaDoc cfProps)
128     {
129         JMSURLHelper jmsurl = (JMSURLHelper)cfProps.get(JMSConstants.JMS_URL);
130
131         // just check the connection factory jndi name
132
String JavaDoc cfJndiName = jmsurl.getPropertyValue(_CONNECTION_FACTORY_JNDI_NAME);
133         String JavaDoc originalCfJndiName = originalJMSURL.getPropertyValue(_CONNECTION_FACTORY_JNDI_NAME);
134
135         if (cfJndiName.equalsIgnoreCase(originalCfJndiName))
136             return true;
137
138         return false;
139     }
140
141     public Queue JavaDoc getQueue(QueueSession JavaDoc session, String JavaDoc name)
142         throws Exception JavaDoc
143     {
144         return (Queue JavaDoc)context.lookup(name);
145     }
146
147     public Topic JavaDoc getTopic(TopicSession JavaDoc session, String JavaDoc name)
148         throws Exception JavaDoc
149     {
150         return (Topic JavaDoc)context.lookup(name);
151     }
152 }
Popular Tags