package org.jboss.resource.connectionmanager;
import javax.management.ObjectName;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.resource.ResourceException;
import org.jboss.deployment.DeploymentException;
import org.jboss.logging.Logger;
import org.jboss.naming.NonSerializableFactory;
import org.jboss.system.ServiceMBeanSupport;
public class ConnectionFactoryBindingService extends ServiceMBeanSupport
implements ConnectionFactoryBindingServiceMBean
{
private static final Logger log = Logger.getLogger(ConnectionFactoryBindingService.class);
private boolean trace = log.isTraceEnabled();
protected ObjectName cm;
protected String jndiName;
protected String bindName;
protected boolean useJavaContext = true;
protected Object cf;
protected void startService() throws Exception
{
determineBindName();
createConnectionFactory();
bindConnectionFactory();
}
protected void stopService() throws Exception
{
unbindConnectionFactory();
}
public ObjectName getConnectionManager()
{
return cm;
}
public void setConnectionManager(ObjectName cm)
{
this.cm = cm;
}
public String getBindName()
{
return bindName;
}
public String getJndiName()
{
return jndiName;
}
public void setJndiName(String jndiName)
{
this.jndiName = jndiName;
}
public boolean isUseJavaContext()
{
return useJavaContext;
}
public void setUseJavaContext(boolean useJavaContext)
{
this.useJavaContext = useJavaContext;
}
protected void determineBindName() throws Exception
{
bindName = jndiName;
if( useJavaContext && jndiName.startsWith("java:") == false )
bindName = "java:" + jndiName;
}
protected void createConnectionFactory() throws Exception
{
try
{
BaseConnectionManager2 bcm = (BaseConnectionManager2) server.getAttribute(cm, "Instance");
BaseConnectionManager2.ConnectionManagerProxy cmProxy = new BaseConnectionManager2.ConnectionManagerProxy(bcm, cm);
cf = bcm.getPoolingStrategy().getManagedConnectionFactory().createConnectionFactory(cmProxy);
}
catch (ResourceException re)
{
throw new DeploymentException("Could not create ConnectionFactory for adapter: " + cm);
}
}
protected void bindConnectionFactory() throws Exception
{
InitialContext ctx = new InitialContext();
try
{
log.debug("Binding object '" + cf + "' into JNDI at '" + bindName + "'");
Name name = ctx.getNameParser("").parse(bindName);
NonSerializableFactory.rebind(name, cf, true);
log.info("Bound connection factory for resource adapter for ConnectionManager '" + serviceName + " to JNDI name '" + bindName + "'");
}
catch (NamingException ne)
{
throw new DeploymentException("Could not bind ConnectionFactory into jndi: " + bindName);
}
finally
{
ctx.close();
}
}
protected void unbindConnectionFactory() throws Exception
{
InitialContext ctx = new InitialContext();
try
{
ctx.unbind(bindName);
NonSerializableFactory.unbind(bindName);
log.info("Unbound connection factory for resource adapter for ConnectionManager '" + serviceName + " from JNDI name '" + bindName + "'");
}
catch (NamingException ne)
{
log.error("Could not unbind managedConnectionFactory from jndi: " + bindName, ne);
}
finally
{
ctx.close();
}
}
}