package org.jboss.resource.deployment;
import java.util.Properties;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import org.jboss.deployment.DeploymentException;
import org.jboss.naming.Util;
import org.jboss.resource.metadata.AdminObjectMetaData;
import org.jboss.resource.metadata.ConnectorMetaData;
import org.jboss.system.ServiceMBeanSupport;
public class AdminObject extends ServiceMBeanSupport implements AdminObjectMBean
{
protected ObjectName rarName;
protected String type;
protected Properties properties;
protected String jndiName;
public String getJNDIName()
{
return jndiName;
}
public void setJNDIName(String jndiName)
{
this.jndiName = jndiName;
}
public Properties getProperties()
{
return properties;
}
public void setProperties(Properties properties)
{
this.properties = properties;
}
public ObjectName getRARName()
{
return rarName;
}
public void setRARName(ObjectName rarName)
{
this.rarName = rarName;
}
public String getType()
{
return type;
}
public void setType(String type)
{
this.type = type;
}
protected void startService() throws Exception
{
AdminObjectMetaData aomd = retrieveAdminObjectMetaData();
if (aomd == null)
throw new DeploymentException("No admin object metadata type=" + type + " ra=" + rarName);
Object adminObject = createAdminObject(aomd);
bind(adminObject);
}
protected void stopService() throws Exception
{
unbind();
}
protected AdminObjectMetaData retrieveAdminObjectMetaData() throws DeploymentException
{
try
{
ConnectorMetaData cmd = (ConnectorMetaData) server.getAttribute(rarName, "MetaData");
return cmd.getAdminObject(type);
}
catch (Throwable t)
{
DeploymentException.rethrowAsDeploymentException("Error retrieving admin object metadata type=" + type + " ra=" + rarName, t);
return null; }
}
protected Object createAdminObject(AdminObjectMetaData aomd) throws DeploymentException
{
try
{
return AdminObjectFactory.createAdminObject(jndiName, rarName, aomd, properties);
}
catch (Throwable t)
{
DeploymentException.rethrowAsDeploymentException("Error creating admin object metadata type=" + type + " ra=" + rarName, t);
return null; }
}
protected void bind(Object object) throws Exception
{
InitialContext ctx = new InitialContext();
try
{
Util.bind(ctx, jndiName, object);
log.info("Bound admin object '" + object.getClass().getName() + "' at '" + jndiName + "'");
}
finally
{
ctx.close();
}
}
protected void unbind() throws Exception
{
InitialContext ctx = new InitialContext();
try
{
Util.unbind(ctx, jndiName);
log.info("Unbound admin object at '" + jndiName + "'");
}
finally
{
ctx.close();
}
}
}