/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.jndi;
import EDU.oswego.cs.dl.util.concurrent.ConcurrentHashMap;
import org.codehaus.activemq.ActiveMQConnectionFactory;
import org.codehaus.activemq.broker.Broker;
import org.codehaus.activemq.message.ActiveMQQueue;
import org.codehaus.activemq.message.ActiveMQTopic;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.Topic;
import javax.naming.CommunicationException;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
/**
* A factory of the ActiveMQ InitialContext which contains {@link ConnectionFactory}
* instances as well as a child context called <i>destinations</i> which contain all of the
* current active destinations, in child context depending on the QoS such as
* transient or durable and queue or topic.
*
* @version $Revision: 1.1 $
*/
public class ActiveMQInitialContextFactory implements InitialContextFactory {
private String queuePrefix = "queue.";
private String topicPrefix = "topic.";
public Context getInitialContext(Hashtable environment) throws NamingException {
// lets create a factory
Map data = new ConcurrentHashMap();
ActiveMQConnectionFactory factory = createConnectionFactory(environment);
data.put("ConnectionFactory", factory);
data.put("QueueConnectionFactory", factory);
data.put("TopicConnectionFactory", factory);
createQueues(data, environment);
createTopics(data, environment);
try {
Broker broker = factory.getEmbeddedBroker();
if (broker != null) {
data.put("destinations", broker.getDestinationContext(environment));
}
}
catch (JMSException e) {
CommunicationException exception = new CommunicationException("Failed to access embedded broker: " + e);
exception.setRootCause(e);
throw exception;
}
return new ReadOnlyContext(environment, data);
}
// Properties
//-------------------------------------------------------------------------
public String getTopicPrefix() {
return topicPrefix;
}
public void setTopicPrefix(String topicPrefix) {
this.topicPrefix = topicPrefix;
}
public String getQueuePrefix() {
return queuePrefix;
}
public void setQueuePrefix(String queuePrefix) {
this.queuePrefix = queuePrefix;
}
// Implementation methods
//-------------------------------------------------------------------------
protected void createQueues(Map data, Hashtable environment) {
for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String key = entry.getKey().toString();
if (key.startsWith(queuePrefix)) {
String jndiName = key.substring(queuePrefix.length());
data.put(jndiName, createQueue(entry.getValue().toString()));
}
}
}
protected void createTopics(Map data, Hashtable environment) {
for (Iterator iter = environment.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String key = entry.getKey().toString();
if (key.startsWith(topicPrefix)) {
String jndiName = key.substring(topicPrefix.length());
data.put(jndiName, createTopic(entry.getValue().toString()));
}
}
}
/**
* Factory method to create new Queue instances
*/
protected Queue createQueue(String name) {
return new ActiveMQQueue(name);
}
/**
* Factory method to create new Topic instances
*/
protected Topic createTopic(String name) {
return new ActiveMQTopic(name);
}
/**
* Factory method to create a new connection factory from the given environment
*/
protected ActiveMQConnectionFactory createConnectionFactory(Hashtable environment) {
ActiveMQConnectionFactory answer = new ActiveMQConnectionFactory();
Properties properties = new Properties();
properties.putAll(environment);
answer.setProperties(properties);
return answer;
}
}