1 22 package org.jboss.varia.deployment; 23 24 import java.io.File ; 25 import java.io.FileWriter ; 26 import java.io.IOException ; 27 import java.net.URL ; 28 import java.util.Arrays ; 29 30 import javax.management.ObjectName ; 31 32 import org.jboss.deployment.DeploymentException; 33 import org.jboss.deployment.DeploymentInfo; 34 import org.jboss.deployment.SubDeployerSupport; 35 import org.jboss.mx.util.MBeanProxyExt; 36 import org.jboss.mx.util.ObjectNameConverter; 37 import org.jboss.system.ServiceControllerMBean; 38 39 51 public class BeanShellSubDeployer extends SubDeployerSupport 52 implements BeanShellSubDeployerMBean 53 { 54 56 public static final String BASE_SCRIPT_OBJECT_NAME = "jboss.scripts:type=BeanShell"; 58 59 60 private static final String [] DEFAULT_ENHANCED_SUFFIXES = new String [] { 61 "800:.bsh" 62 }; 63 64 66 protected ServiceControllerMBean serviceController; 67 68 70 72 77 public BeanShellSubDeployer() 78 { 79 setEnhancedSuffixes(DEFAULT_ENHANCED_SUFFIXES); 80 } 81 82 84 86 88 91 protected void startService() throws Exception 92 { 93 serviceController = (ServiceControllerMBean) 94 MBeanProxyExt.create(ServiceControllerMBean.class, 95 ServiceControllerMBean.OBJECT_NAME, server); 96 97 super.startService(); 99 } 100 101 103 protected void processNestedDeployments(DeploymentInfo di) throws DeploymentException 104 { 105 } 107 108 115 public boolean accepts(DeploymentInfo sdi) 116 { 117 return super.accepts(sdi); 118 } 119 120 127 public void init(DeploymentInfo di) 128 throws DeploymentException 129 { 130 super.init(di); 131 di.watch = di.url; 132 } 133 134 141 public void create(DeploymentInfo di) 142 throws DeploymentException 143 { 144 try 145 { 146 log.debug("Deploying BeanShell script, create step: url " + di.url); 148 149 String lURL = di.url.toString(); 150 int lIndex = lURL.lastIndexOf( "/" ); 151 di.shortName = lURL.substring( lIndex >= 0 ? lIndex + 1 : 0 ); 152 153 BeanShellScript script = new BeanShellScript (di); 154 ObjectName bshScriptName = script.getPreferedObjectName(); 155 ObjectName [] depends = script.getDependsServices(); 156 157 if (bshScriptName == null) 158 { 159 bshScriptName = ObjectNameConverter.convert( 160 BASE_SCRIPT_OBJECT_NAME + ",url=" + di.url); 161 } 162 163 di.deployedObject = bshScriptName; 164 try 165 { 166 server.unregisterMBean(bshScriptName); 167 } catch(Exception e) { log.info(e);} 168 server.registerMBean(script, bshScriptName); 169 170 log.debug( "Deploying: " + di.url ); 171 172 if (depends == null) 174 serviceController.create(bshScriptName); 175 else 176 serviceController.create(bshScriptName, Arrays.asList(depends)); 177 super.create(di); 178 } 179 catch (Exception e) 180 { 181 destroy(di); 182 DeploymentException de = new DeploymentException("create operation failed for script " 183 + di.url, e); 184 throw de; 185 } 186 } 187 188 public synchronized void start(DeploymentInfo di) 189 throws DeploymentException 190 { 191 try 192 { 193 log.debug( "start script, deploymentInfo: " + di + 195 ", short name: " + di.shortName + 196 ", parent short name: " + 197 (di.parent == null ? "no parent" : di.parent.shortName) ); 198 199 serviceController.start(di.deployedObject); 200 201 log.debug( "Deployed: " + di.url ); 202 super.start(di); 203 } 204 catch (Exception e) 205 { 206 throw new DeploymentException( "Could not deploy " + di.url, e ); 207 } 208 } 209 210 public void stop(DeploymentInfo di) 211 throws DeploymentException 212 { 213 try 214 { 215 serviceController.stop(di.deployedObject); 216 super.stop(di); 217 } 218 catch (Exception e) 219 { 220 throw new DeploymentException( "problem stopping ejb module: " + 221 di.url, e ); 222 } 223 } 224 225 public void destroy(DeploymentInfo di) 226 throws DeploymentException 227 { 228 try 229 { 230 serviceController.destroy( di.deployedObject ); 231 serviceController.remove( di.deployedObject ); 232 super.destroy(di); 233 } 234 catch (Exception e) 235 { 236 throw new DeploymentException( "problem destroying BSH Script: " + 237 di.url, e ); 238 } 239 } 240 241 254 public URL createScriptDeployment(String bshScript, String scriptName) 255 throws DeploymentException 256 { 257 try 258 { 259 File scriptFile = File.createTempFile(scriptName, ".bsh"); 260 FileWriter fw = new FileWriter (scriptFile); 261 fw.write(bshScript); 262 fw.close(); 263 264 URL scriptURL = scriptFile.toURL(); 265 mainDeployer.deploy(scriptURL); 266 return scriptURL; 267 } 268 catch(IOException e) 269 { 270 throw new DeploymentException("Failed to deploy: "+scriptName, e); 271 } 272 } 273 274 276 278 280 282 } 283 | Popular Tags |