1 22 package org.jboss.deployment; 23 24 import java.io.File ; 25 import java.net.URL ; 26 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.ObjectName ; 29 30 import org.jboss.mx.loading.RepositoryClassLoader; 31 import org.jboss.mx.util.MBeanProxyExt; 32 import org.jboss.system.ServiceControllerMBean; 33 34 41 public abstract class SimpleSubDeployerSupport extends SubDeployerSupport 42 { 43 44 private ServiceControllerMBean serviceController; 45 46 47 private boolean registeredClassLoader = false; 48 49 54 public abstract String getExtension(); 55 56 61 public abstract String getMetaDataURL(); 62 63 69 public abstract String getObjectName(DeploymentInfo di) throws DeploymentException; 70 71 76 public abstract String getDeploymentClass(); 77 78 public boolean accepts(DeploymentInfo di) 79 { 80 String urlStr = di.url.toString(); 81 String extension = getExtension(); 82 return urlStr.endsWith(extension) || urlStr.endsWith(extension + '/'); 83 } 84 85 public void init(DeploymentInfo di) throws DeploymentException 86 { 87 URL url = getMetaDataResource(di); 88 parseMetaData(di, url); 89 resolveWatch(di, url); 90 super.init(di); 91 } 92 93 public void create(DeploymentInfo di) throws DeploymentException 94 { 95 determineObjectName(di); 96 ObjectName uclName = registerClassLoader(di); 97 try 98 { 99 registerDeployment(di, uclName); 100 try 101 { 102 createService(di); 103 try 104 { 105 super.create(di); 106 } 107 catch (Throwable t) 108 { 109 destroyService(di); 110 } 111 } 112 catch (Throwable t) 113 { 114 unregisterDeployment(di); 115 throw t; 116 } 117 } 118 catch (Throwable t) 119 { 120 unregisterClassLoader(di); 121 DeploymentException.rethrowAsDeploymentException("Error creating deployment " + di.url, t); 122 } 123 } 124 125 public void start(DeploymentInfo di) throws DeploymentException 126 { 127 startService(di); 128 try 129 { 130 super.start(di); 131 } 132 catch (Throwable t) 133 { 134 stopService(di); 135 DeploymentException.rethrowAsDeploymentException("Error in start for deployment " + di.url, t); 136 } 137 } 138 139 public void stop(DeploymentInfo di) throws DeploymentException 140 { 141 stopService(di); 142 super.stop(di); 143 } 144 145 public void destroy(DeploymentInfo di) throws DeploymentException 146 { 147 try 148 { 149 destroyService(di); 150 super.destroy(di); 151 } 152 finally 153 { 154 unregisterDeployment(di); 155 unregisterClassLoader(di); 156 } 157 } 158 159 public void postRegister(Boolean done) 160 { 161 super.postRegister(done); 162 163 if (done.booleanValue()) 164 { 165 serviceController = (ServiceControllerMBean) 166 MBeanProxyExt.create(ServiceControllerMBean.class, 167 ServiceControllerMBean.OBJECT_NAME, 168 server); 169 } 170 } 171 172 179 protected URL getMetaDataResource(DeploymentInfo di) throws DeploymentException 180 { 181 URL url = di.localCl.findResource(getMetaDataURL()); 182 if (url == null) 183 throw new DeploymentException("Could not find meta data " + getMetaDataURL() + " for deployment " + di.url); 184 return url; 185 } 186 187 194 abstract protected void parseMetaData(DeploymentInfo di, URL url) throws DeploymentException; 195 196 203 protected void resolveWatch(DeploymentInfo di, URL url) throws DeploymentException 204 { 205 di.watch = di.url; 207 208 if (di.url.getProtocol().equals("file")) 210 { 211 File file = new File (di.url.getFile()); 212 if (file.isDirectory()) 213 di.watch = url; 214 } 215 } 216 217 223 protected void determineObjectName(DeploymentInfo di) throws DeploymentException 224 { 225 String objectName = getObjectName(di); 226 try 227 { 228 di.deployedObject = new ObjectName (objectName); 229 } 230 catch (MalformedObjectNameException e) 231 { 232 throw new DeploymentException("INTERNAL ERROR: Bad object name. " + objectName); 233 } 234 } 235 236 243 protected ObjectName registerClassLoader(DeploymentInfo di) throws DeploymentException 244 { 245 ObjectName uclName = null; 246 try 247 { 248 RepositoryClassLoader ucl = di.ucl; 249 uclName = ucl.getObjectName(); 250 if (server.isRegistered(uclName) == false) 251 { 252 server.registerMBean(di.ucl, uclName); 253 registeredClassLoader = true; 254 } 255 } 256 catch (Throwable t) 257 { 258 DeploymentException.rethrowAsDeploymentException("Error registering classloader " + uclName, t); 259 } 260 return uclName; 261 } 262 263 268 protected void unregisterClassLoader(DeploymentInfo di) 269 { 270 ObjectName uclName = null; 271 try 272 { 273 RepositoryClassLoader ucl = di.ucl; 274 if (ucl != null) 275 { 276 uclName = ucl.getObjectName(); 277 if (registeredClassLoader && server.isRegistered(uclName)) 278 { 279 server.unregisterMBean(uclName); 280 registeredClassLoader = false; 281 } 282 } 283 } 284 catch (Throwable t) 285 { 286 log.warn("Error unregistering classloader " + uclName, t); 287 } 288 } 289 290 297 protected void registerDeployment(DeploymentInfo di, ObjectName uclName) throws DeploymentException 298 { 299 try 300 { 301 String deploymentClass = getDeploymentClass(); 302 server.createMBean(deploymentClass, di.deployedObject, uclName, new Object [] { di }, new String [] { DeploymentInfo.class.getName() }); 303 } 304 catch (Throwable t) 305 { 306 DeploymentException.rethrowAsDeploymentException("Error registering deployment " + di.url, t); 307 } 308 } 309 310 315 protected void unregisterDeployment(DeploymentInfo di) 316 { 317 try 318 { 319 if (server.isRegistered(di.deployedObject)) 320 server.unregisterMBean(di.deployedObject); 321 } 322 catch (Throwable t) 323 { 324 log.warn("Error unregistering deployment " + di.deployedObject, t); 325 } 326 } 327 328 334 protected void createService(DeploymentInfo di) throws DeploymentException 335 { 336 try 337 { 338 serviceController.create(di.deployedObject); 339 } 340 catch (Throwable t) 341 { 342 DeploymentException.rethrowAsDeploymentException("Error in create for deployment " + di.url, t); 343 } 344 } 345 346 352 protected void startService(DeploymentInfo di) throws DeploymentException 353 { 354 try 355 { 356 serviceController.start(di.deployedObject); 357 } 358 catch (Throwable t) 359 { 360 DeploymentException.rethrowAsDeploymentException("Error in start for deployment " + di.url, t); 361 } 362 } 363 364 369 protected void stopService(DeploymentInfo di) 370 { 371 try 372 { 373 if (di.deployedObject != null) 374 serviceController.stop(di.deployedObject); 375 } 376 catch (Throwable t) 377 { 378 log.warn("Error in stop for deployment " + di.url, t); 379 } 380 } 381 382 387 protected void destroyService(DeploymentInfo di) throws DeploymentException 388 { 389 try 390 { 391 if (di.deployedObject != null) 392 serviceController.destroy(di.deployedObject); 393 } 394 catch (Throwable t) 395 { 396 log.warn("Error in destroy for deployment " + di.url, t); 397 } 398 try 399 { 400 if (di.deployedObject != null) 401 serviceController.remove(di.deployedObject); 402 } 403 catch (Throwable t) 404 { 405 log.warn("Error in remove for deployment " + di.url, t); 406 } 407 } 408 } 409 | Popular Tags |