KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > jndi > ActiveMQInitialContextFactory


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

18 package org.apache.activemq.jndi;
19
20 import java.net.URISyntaxException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Hashtable JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.jms.ConnectionFactory JavaDoc;
30 import javax.jms.Queue JavaDoc;
31 import javax.jms.Topic JavaDoc;
32 import javax.naming.Context JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.spi.InitialContextFactory JavaDoc;
35
36 import org.apache.activemq.ActiveMQConnectionFactory;
37 import org.apache.activemq.broker.Broker;
38 import org.apache.activemq.command.ActiveMQQueue;
39 import org.apache.activemq.command.ActiveMQTopic;
40
41 import java.util.concurrent.ConcurrentHashMap JavaDoc;
42
43 /**
44  * A factory of the ActiveMQ InitialContext which contains {@link ConnectionFactory}
45  * instances as well as a child context called <i>destinations</i> which contain all of the
46  * current active destinations, in child context depending on the QoS such as
47  * transient or durable and queue or topic.
48  *
49  * @version $Revision: 1.2 $
50  */

51 public class ActiveMQInitialContextFactory implements InitialContextFactory JavaDoc {
52
53     private static final String JavaDoc[] defaultConnectionFactoryNames = {
54         "ConnectionFactory", "QueueConnectionFactory", "TopicConnectionFactory"
55     };
56
57     private String JavaDoc connectionPrefix = "connection.";
58     private String JavaDoc queuePrefix = "queue.";
59     private String JavaDoc topicPrefix = "topic.";
60
61     public Context JavaDoc getInitialContext(Hashtable JavaDoc environment) throws NamingException JavaDoc {
62         // lets create a factory
63
Map JavaDoc data = new ConcurrentHashMap JavaDoc();
64         String JavaDoc[] names = getConnectionFactoryNames(environment);
65         for (int i = 0; i < names.length; i++) {
66             ActiveMQConnectionFactory factory =null;
67             String JavaDoc name = names[i];
68
69             try{
70              factory = createConnectionFactory(name, environment);
71             }catch(Exception JavaDoc e){
72                 throw new NamingException JavaDoc("Invalid broker URL");
73
74             }
75        /* if( broker==null ) {
76                 try {
77                     broker = factory.getEmbeddedBroker();
78                 }
79                 catch (JMSException e) {
80                     log.warn("Failed to get embedded broker", e);
81                 }
82             }
83        */

84             data.put(name,factory);
85         }
86
87         createQueues(data, environment);
88         createTopics(data, environment);
89         /*
90         if (broker != null) {
91             data.put("destinations", broker.getDestinationContext(environment));
92         }
93         */

94         data.put("dynamicQueues", new LazyCreateContext() {
95             private static final long serialVersionUID = 6503881346214855588L;
96
97             protected Object JavaDoc createEntry(String JavaDoc name) {
98                 return new ActiveMQQueue(name);
99             }
100         });
101         data.put("dynamicTopics", new LazyCreateContext() {
102             private static final long serialVersionUID = 2019166796234979615L;
103
104             protected Object JavaDoc createEntry(String JavaDoc name) {
105                 return new ActiveMQTopic(name);
106             }
107         });
108
109         return createContext(environment, data);
110     }
111
112     // Properties
113
//-------------------------------------------------------------------------
114
public String JavaDoc getTopicPrefix() {
115         return topicPrefix;
116     }
117
118     public void setTopicPrefix(String JavaDoc topicPrefix) {
119         this.topicPrefix = topicPrefix;
120     }
121
122     public String JavaDoc getQueuePrefix() {
123         return queuePrefix;
124     }
125
126     public void setQueuePrefix(String JavaDoc queuePrefix) {
127         this.queuePrefix = queuePrefix;
128     }
129
130     // Implementation methods
131
//-------------------------------------------------------------------------
132

133     protected ReadOnlyContext createContext(Hashtable JavaDoc environment, Map JavaDoc data) {
134         return new ReadOnlyContext(environment, data);
135     }
136
137     protected ActiveMQConnectionFactory createConnectionFactory(String JavaDoc name, Hashtable JavaDoc environment) throws URISyntaxException JavaDoc {
138         Hashtable JavaDoc temp = new Hashtable JavaDoc(environment);
139         String JavaDoc prefix = connectionPrefix+name+".";
140         for (Iterator JavaDoc iter = environment.entrySet().iterator(); iter.hasNext();) {
141             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
142             String JavaDoc key = (String JavaDoc) entry.getKey();
143             if( key.startsWith(prefix) ) {
144                 // Rename the key...
145
temp.remove(key);
146                 key = key.substring(prefix.length());
147                 temp.put(key, entry.getValue());
148             }
149         }
150         return createConnectionFactory(temp);
151     }
152
153     protected String JavaDoc[] getConnectionFactoryNames(Map JavaDoc environment) {
154         String JavaDoc factoryNames = (String JavaDoc) environment.get("connectionFactoryNames");
155         if (factoryNames != null) {
156             List JavaDoc list = new ArrayList JavaDoc();
157             for (StringTokenizer JavaDoc enumeration = new StringTokenizer JavaDoc(factoryNames, ","); enumeration.hasMoreTokens();) {
158                 list.add(enumeration.nextToken().trim());
159             }
160             int size = list.size();
161             if (size > 0) {
162                 String JavaDoc[] answer = new String JavaDoc[size];
163                 list.toArray(answer);
164                 return answer;
165             }
166         }
167         return defaultConnectionFactoryNames;
168     }
169
170     protected void createQueues(Map JavaDoc data, Hashtable JavaDoc environment) {
171         for (Iterator JavaDoc iter = environment.entrySet().iterator(); iter.hasNext();) {
172             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
173             String JavaDoc key = entry.getKey().toString();
174             if (key.startsWith(queuePrefix)) {
175                 String JavaDoc jndiName = key.substring(queuePrefix.length());
176                 data.put(jndiName, createQueue(entry.getValue().toString()));
177             }
178         }
179     }
180
181     protected void createTopics(Map JavaDoc data, Hashtable JavaDoc environment) {
182         for (Iterator JavaDoc iter = environment.entrySet().iterator(); iter.hasNext();) {
183             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
184             String JavaDoc key = entry.getKey().toString();
185             if (key.startsWith(topicPrefix)) {
186                 String JavaDoc jndiName = key.substring(topicPrefix.length());
187                 data.put(jndiName, createTopic(entry.getValue().toString()));
188             }
189         }
190     }
191
192     /**
193      * Factory method to create new Queue instances
194      */

195     protected Queue JavaDoc createQueue(String JavaDoc name) {
196         return new ActiveMQQueue(name);
197     }
198
199     /**
200      * Factory method to create new Topic instances
201      */

202     protected Topic JavaDoc createTopic(String JavaDoc name) {
203         return new ActiveMQTopic(name);
204     }
205     
206     /**
207      * Factory method to create a new connection factory from the given environment
208      */

209     protected ActiveMQConnectionFactory createConnectionFactory(Hashtable JavaDoc environment) throws URISyntaxException JavaDoc {
210         ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
211         Properties JavaDoc properties = new Properties JavaDoc();
212         properties.putAll(environment);
213         answer.setProperties(properties);
214         return answer;
215     }
216
217     public String JavaDoc getConnectionPrefix() {
218         return connectionPrefix;
219     }
220     
221
222     public void setConnectionPrefix(String JavaDoc connectionPrefix) {
223         this.connectionPrefix = connectionPrefix;
224     }
225     
226 }
227
Popular Tags