Source code: org/activemq/jndi/ActiveMQInitialContextFactory.java
1 /**
2 *
3 * Copyright 2004 Protique Ltd
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq.jndi;
19
20 import java.util.ArrayList;
21 import java.util.Hashtable;
22 import java.util.Iterator;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Properties;
26 import java.util.StringTokenizer;
27
28 import javax.jms.ConnectionFactory;
29 import javax.jms.JMSException;
30 import javax.jms.Queue;
31 import javax.jms.Topic;
32 import javax.naming.Context;
33 import javax.naming.NamingException;
34 import javax.naming.spi.InitialContextFactory;
35
36 import org.activemq.ActiveMQConnectionFactory;
37 import org.activemq.broker.Broker;
38 import org.activemq.message.ActiveMQQueue;
39 import org.activemq.message.ActiveMQTopic;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
44
45 /**
46 * A factory of the ActiveMQ InitialContext which contains {@link ConnectionFactory}
47 * instances as well as a child context called <i>destinations</i> which contain all of the
48 * current active destinations, in child context depending on the QoS such as
49 * transient or durable and queue or topic.
50 *
51 * @version $Revision: 1.1.1.1 $
52 */
53 public class ActiveMQInitialContextFactory implements InitialContextFactory {
54 private static final transient Log log = LogFactory.getLog(ActiveMQInitialContextFactory.class);
55
56 protected static final String[] defaultConnectionFactoryNames = {
57 "ConnectionFactory", "QueueConnectionFactory", "TopicConnectionFactory"
58 };
59
60 private String connectionPrefix = "connection.";
61 private String queuePrefix = "queue.";
62 private String topicPrefix = "topic.";
63
64 public Context getInitialContext(Hashtable environment) throws NamingException {
65 // lets create a factory
66 Map data = new ConcurrentHashMap();
67 Broker broker = null;
68
69 String[] names = getConnectionFactoryNames(environment);
70 for (int i = 0; i < names.length; i++) {
71
72 String name = names[i];
73 ActiveMQConnectionFactory factory = createConnectionFactory(name, environment);
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 data.put(name,factory);
84 }
85
86 createQueues(data, environment);
87 createTopics(data, environment);
88 if (broker != null) {
89 data.put("destinations", broker.getDestinationContext(environment));
90 }
91
92 data.put("dynamicQueues", new LazyCreateContext() {
93 protected Object createEntry(String name) {
94 return new ActiveMQQueue(name);
95 }
96 });
97 data.put("dynamicTopics", new LazyCreateContext() {
98 protected Object createEntry(String name) {
99 return new ActiveMQTopic(name);
100 }
101 });
102
103 return new ReadOnlyContext(environment, data);
104 }
105
106 private ActiveMQConnectionFactory createConnectionFactory(String name, Hashtable environment) {
107 Hashtable temp = new Hashtable(environment);
108 String prefix = connectionPrefix+name+".";
109 for (Iterator iter = environment.keySet().iterator(); iter.hasNext();) {
110 String key = (String) iter.next();
111 if( key.startsWith(prefix) ) {
112 Object value = environment.get(key);
113 // Rename the key...
114 temp.remove(key);
115 key = key.substring(prefix.length());
116 temp.put(key, value);
117 }
118 }
119 return createConnectionFactory(temp);
120 }
121
122 // Properties
123 //-------------------------------------------------------------------------
124 public String getTopicPrefix() {
125 return topicPrefix;
126 }
127
128 public void setTopicPrefix(String topicPrefix) {
129 this.topicPrefix = topicPrefix;
130 }
131
132 public String getQueuePrefix() {
133 return queuePrefix;
134 }
135
136 public void setQueuePrefix(String queuePrefix) {
137 this.queuePrefix = queuePrefix;
138 }
139
140 // Implementation methods
141 //-------------------------------------------------------------------------
142 protected String[] getConnectionFactoryNames(Map environment) {
143 String factoryNames = (String) environment.get("connectionFactoryNames");
144 if (factoryNames != null) {
145 List list = new ArrayList();
146 for (StringTokenizer enumeration = new StringTokenizer(factoryNames, ","); enumeration.hasMoreTokens();) {
147 list.add(enumeration.nextToken().trim());
148 }
149 int size = list.size();
150 if (size > 0) {
151 String[] answer = new String[size];
152 list.toArray(answer);
153 return answer;
154 }
155 }
156 return defaultConnectionFactoryNames;
157 }
158
159 protected void createQueues(Map data, Hashtable environment) {
160 for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
161 Map.Entry entry = (Map.Entry) iter.next();
162 String key = entry.getKey().toString();
163 if (key.startsWith(queuePrefix)) {
164 String jndiName = key.substring(queuePrefix.length());
165 data.put(jndiName, createQueue(entry.getValue().toString()));
166 }
167 }
168 }
169
170 protected void createTopics(Map data, Hashtable environment) {
171 for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
172 Map.Entry entry = (Map.Entry) iter.next();
173 String key = entry.getKey().toString();
174 if (key.startsWith(topicPrefix)) {
175 String jndiName = key.substring(topicPrefix.length());
176 data.put(jndiName, createTopic(entry.getValue().toString()));
177 }
178 }
179 }
180
181 /**
182 * Factory method to create new Queue instances
183 */
184 protected Queue createQueue(String name) {
185 return new ActiveMQQueue(name);
186 }
187
188 /**
189 * Factory method to create new Topic instances
190 */
191 protected Topic createTopic(String name) {
192 return new ActiveMQTopic(name);
193 }
194
195 /**
196 * Factory method to create a new connection factory from the given environment
197 */
198 protected ActiveMQConnectionFactory createConnectionFactory(Hashtable environment) {
199 ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
200 Properties properties = new Properties();
201 properties.putAll(environment);
202 answer.setProperties(properties);
203 return answer;
204 }
205
206 public String getConnectionPrefix() {
207 return connectionPrefix;
208 }
209
210
211 public void setConnectionPrefix(String connectionPrefix) {
212 this.connectionPrefix = connectionPrefix;
213 }
214
215 }