package org.jboss.varia.deployment;
import java.net.URL;
import java.util.Arrays;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
import javax.management.ObjectName;
import org.jboss.deployment.DeploymentException;
import org.jboss.deployment.DeploymentInfo;
import org.jboss.deployment.SubDeployerSupport;
import org.jboss.mx.util.MBeanProxyExt;
import org.jboss.mx.util.ObjectNameConverter;
import org.jboss.system.ServiceControllerMBean;
public class BeanShellSubDeployer
extends SubDeployerSupport
implements BeanShellSubDeployerMBean
{
public static final String BEANSHELL_EXTENSION = ".bsh";
public static final String BASE_SCRIPT_OBJECT_NAME = "jboss.scripts:type=BeanShell";
protected ServiceControllerMBean serviceController;
public BeanShellSubDeployer()
{
setSuffixes(new String[] { BEANSHELL_EXTENSION });
setRelativeOrder(RELATIVE_ORDER_800);
}
protected void startService() throws Exception
{
serviceController = (ServiceControllerMBean)
MBeanProxyExt.create(ServiceControllerMBean.class,
ServiceControllerMBean.OBJECT_NAME, server);
super.startService();
}
protected void processNestedDeployments(DeploymentInfo di) throws DeploymentException
{
}
public boolean accepts(DeploymentInfo sdi)
{
String urlStr = sdi.url.toString();
return urlStr.toLowerCase().endsWith(BEANSHELL_EXTENSION);
}
public void init(DeploymentInfo di)
throws DeploymentException
{
super.init(di);
di.watch = di.url;
}
public void create(DeploymentInfo di)
throws DeploymentException
{
try
{
log.debug("Deploying BeanShell script, create step: url " + di.url);
String lURL = di.url.toString();
int lIndex = lURL.lastIndexOf( "/" );
di.shortName = lURL.substring( lIndex >= 0 ? lIndex + 1 : 0 );
BeanShellScript script = new BeanShellScript (di);
ObjectName bshScriptName = script.getPreferedObjectName();
ObjectName[] depends = script.getDependsServices();
if (bshScriptName == null)
{
bshScriptName = ObjectNameConverter.convert(
BASE_SCRIPT_OBJECT_NAME + ",url=" + di.url);
}
di.deployedObject = bshScriptName;
try
{
server.unregisterMBean(bshScriptName);
} catch(Exception e) { log.info(e);}
server.registerMBean(script, bshScriptName);
log.debug( "Deploying: " + di.url );
if (depends == null)
serviceController.create(bshScriptName);
else
serviceController.create(bshScriptName, Arrays.asList(depends));
super.create(di);
}
catch (Exception e)
{
destroy(di);
throw new DeploymentException("create operation failed for script "
+ di.url, e);
}
}
public synchronized void start(DeploymentInfo di)
throws DeploymentException
{
try
{
log.debug( "start script, deploymentInfo: " + di +
", short name: " + di.shortName +
", parent short name: " +
(di.parent == null ? "no parent" : di.parent.shortName) );
serviceController.start(di.deployedObject);
log.debug( "Deployed: " + di.url );
super.start(di);
}
catch (Exception e)
{
throw new DeploymentException( "Could not deploy " + di.url, e );
}
}
public void stop(DeploymentInfo di)
throws DeploymentException
{
try
{
serviceController.stop(di.deployedObject);
super.stop(di);
}
catch (Exception e)
{
throw new DeploymentException( "problem stopping ejb module: " +
di.url, e );
}
}
public void destroy(DeploymentInfo di)
throws DeploymentException
{
try
{
serviceController.destroy( di.deployedObject );
serviceController.remove( di.deployedObject );
super.destroy(di);
}
catch (Exception e)
{
throw new DeploymentException( "problem destroying BSH Script: " +
di.url, e );
}
}
public URL createScriptDeployment(String bshScript, String scriptName)
throws DeploymentException
{
URL scriptURL = null;
try
{
File scriptFile = File.createTempFile(scriptName, ".bsh");
FileWriter fw = new FileWriter(scriptFile);
fw.write(bshScript);
fw.close();
scriptURL = scriptFile.toURL();
mainDeployer.deploy(scriptURL);
}
catch(IOException e)
{
throw new DeploymentException("Failed to deploy: "+scriptName, e);
}
return scriptURL;
}
}