1 18 package org.apache.activemq.jndi; 19 20 import java.net.URISyntaxException ; 21 import java.util.ArrayList ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 import java.util.Properties ; 27 import java.util.StringTokenizer ; 28 29 import javax.jms.ConnectionFactory ; 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.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 ; 42 43 51 public class ActiveMQInitialContextFactory implements InitialContextFactory { 52 53 private static final String [] defaultConnectionFactoryNames = { 54 "ConnectionFactory", "QueueConnectionFactory", "TopicConnectionFactory" 55 }; 56 57 private String connectionPrefix = "connection."; 58 private String queuePrefix = "queue."; 59 private String topicPrefix = "topic."; 60 61 public Context getInitialContext(Hashtable environment) throws NamingException { 62 Map data = new ConcurrentHashMap (); 64 String [] names = getConnectionFactoryNames(environment); 65 for (int i = 0; i < names.length; i++) { 66 ActiveMQConnectionFactory factory =null; 67 String name = names[i]; 68 69 try{ 70 factory = createConnectionFactory(name, environment); 71 }catch(Exception e){ 72 throw new NamingException ("Invalid broker URL"); 73 74 } 75 84 data.put(name,factory); 85 } 86 87 createQueues(data, environment); 88 createTopics(data, environment); 89 94 data.put("dynamicQueues", new LazyCreateContext() { 95 private static final long serialVersionUID = 6503881346214855588L; 96 97 protected Object createEntry(String 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 createEntry(String name) { 105 return new ActiveMQTopic(name); 106 } 107 }); 108 109 return createContext(environment, data); 110 } 111 112 public String getTopicPrefix() { 115 return topicPrefix; 116 } 117 118 public void setTopicPrefix(String topicPrefix) { 119 this.topicPrefix = topicPrefix; 120 } 121 122 public String getQueuePrefix() { 123 return queuePrefix; 124 } 125 126 public void setQueuePrefix(String queuePrefix) { 127 this.queuePrefix = queuePrefix; 128 } 129 130 133 protected ReadOnlyContext createContext(Hashtable environment, Map data) { 134 return new ReadOnlyContext(environment, data); 135 } 136 137 protected ActiveMQConnectionFactory createConnectionFactory(String name, Hashtable environment) throws URISyntaxException { 138 Hashtable temp = new Hashtable (environment); 139 String prefix = connectionPrefix+name+"."; 140 for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) { 141 Map.Entry entry = (Map.Entry ) iter.next(); 142 String key = (String ) entry.getKey(); 143 if( key.startsWith(prefix) ) { 144 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 [] getConnectionFactoryNames(Map environment) { 154 String factoryNames = (String ) environment.get("connectionFactoryNames"); 155 if (factoryNames != null) { 156 List list = new ArrayList (); 157 for (StringTokenizer enumeration = new StringTokenizer (factoryNames, ","); enumeration.hasMoreTokens();) { 158 list.add(enumeration.nextToken().trim()); 159 } 160 int size = list.size(); 161 if (size > 0) { 162 String [] answer = new String [size]; 163 list.toArray(answer); 164 return answer; 165 } 166 } 167 return defaultConnectionFactoryNames; 168 } 169 170 protected void createQueues(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(queuePrefix)) { 175 String jndiName = key.substring(queuePrefix.length()); 176 data.put(jndiName, createQueue(entry.getValue().toString())); 177 } 178 } 179 } 180 181 protected void createTopics(Map data, Hashtable environment) { 182 for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) { 183 Map.Entry entry = (Map.Entry ) iter.next(); 184 String key = entry.getKey().toString(); 185 if (key.startsWith(topicPrefix)) { 186 String jndiName = key.substring(topicPrefix.length()); 187 data.put(jndiName, createTopic(entry.getValue().toString())); 188 } 189 } 190 } 191 192 195 protected Queue createQueue(String name) { 196 return new ActiveMQQueue(name); 197 } 198 199 202 protected Topic createTopic(String name) { 203 return new ActiveMQTopic(name); 204 } 205 206 209 protected ActiveMQConnectionFactory createConnectionFactory(Hashtable environment) throws URISyntaxException { 210 ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory(); 211 Properties properties = new Properties (); 212 properties.putAll(environment); 213 answer.setProperties(properties); 214 return answer; 215 } 216 217 public String getConnectionPrefix() { 218 return connectionPrefix; 219 } 220 221 222 public void setConnectionPrefix(String connectionPrefix) { 223 this.connectionPrefix = connectionPrefix; 224 } 225 226 } 227 | Popular Tags |