package org.jboss.jms.jndi;
import java.util.Properties;
import javax.management.ObjectName;
import javax.management.MBeanServer;
import javax.management.MalformedObjectNameException;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.NameNotFoundException;
import org.jboss.deployment.DeploymentException;
import org.jboss.system.ServiceMBeanSupport;
public class JMSProviderLoader
extends ServiceMBeanSupport
implements JMSProviderLoaderMBean
{
protected JMSProviderAdapter providerAdapter;
protected Properties properties;
protected String providerName;
protected String providerAdapterClass;
protected String factoryRef;
protected String queueFactoryRef;
protected String topicFactoryRef;
protected String jndiName;
public void setProviderName(String name)
{
this.providerName = name;
}
public String getProviderName()
{
return providerName;
}
public void setProviderAdapterClass(String clazz)
{
providerAdapterClass = clazz;
}
public String getProviderAdapterClass()
{
return providerAdapterClass;
}
public void setProperties(final Properties properties)
{
this.properties = properties;
}
public Properties getProperties()
{
return properties;
}
public void setAdapterJNDIName(final String name)
{
this.jndiName = name;
}
public String getAdapterJNDIName()
{
return jndiName;
}
public void setFactoryRef(final String newFactoryRef) {
factoryRef = newFactoryRef;
}
public void setQueueFactoryRef(final String newQueueFactoryRef) {
queueFactoryRef = newQueueFactoryRef;
}
public void setTopicFactoryRef(final String newTopicFactoryRef) {
topicFactoryRef = newTopicFactoryRef;
}
public String getFactoryRef() {
return factoryRef;
}
public String getQueueFactoryRef() {
return queueFactoryRef;
}
public String getTopicFactoryRef() {
return topicFactoryRef;
}
public String getName()
{
return providerName;
}
protected void startService() throws Exception
{
if (queueFactoryRef == null)
throw new DeploymentException
("missing required attribute: QueueFactoryRef");
if (topicFactoryRef == null)
throw new DeploymentException
("missing required attribute: TopicFactoryRef");
Class cls = Thread.currentThread().getContextClassLoader().loadClass(providerAdapterClass);
providerAdapter = (JMSProviderAdapter)cls.newInstance();
providerAdapter.setName(providerName);
providerAdapter.setProperties(properties);
providerAdapter.setFactoryRef(factoryRef);
providerAdapter.setQueueFactoryRef(queueFactoryRef);
providerAdapter.setTopicFactoryRef(topicFactoryRef);
InitialContext context = new InitialContext();
try {
if (jndiName == null) {
String name = providerAdapter.getName();
jndiName = "java:/" + name;
}
bind(context, jndiName, providerAdapter);
log.debug("Bound adapter to " + jndiName);
}
finally {
context.close();
}
}
protected void stopService() throws Exception
{
InitialContext context = new InitialContext();
try {
String name = providerAdapter.getName();
String jndiname = "java:/" + name;
context.unbind(jndiname);
log.debug("unbound adapter " + name + " from " + jndiname);
}
finally {
context.close();
}
}
private void bind(Context ctx, String name, Object val)
throws NamingException
{
if (log.isDebugEnabled())
log.debug("attempting to bind " + val + " to " + name);
Name n = ctx.getNameParser("").parse(name);
while (n.size() > 1)
{
String ctxName = n.get(0);
try
{
ctx = (Context)ctx.lookup(ctxName);
} catch (NameNotFoundException e)
{
ctx = ctx.createSubcontext(ctxName);
}
n = n.getSuffix(1);
}
ctx.bind(n.get(0), val);
}
}