1 7 package org.jboss.hibernate.har; 8 9 import java.io.File ; 10 import java.net.URL ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import javax.management.MBeanServer ; 15 import javax.management.MalformedObjectNameException ; 16 import javax.management.ObjectName ; 17 import javax.management.Attribute ; 18 import javax.management.MBeanInfo ; 19 20 import org.jboss.deployment.DeploymentInfo; 21 import org.jboss.deployment.SubDeployerSupport; 22 import org.jboss.deployment.DeploymentException; 23 import org.jboss.system.ServiceControllerMBean; 24 import org.jboss.mx.util.MBeanProxyExt; 25 import org.jboss.mx.util.ObjectNameFactory; 26 import org.jboss.hibernate.jmx.Hibernate; 27 28 45 public class HARDeployer extends SubDeployerSupport 46 implements HARDeployerMBean 47 { 48 private static final ObjectName OBJECT_NAME = ObjectNameFactory.create("jboss.har:service=HARDeployer"); 49 private static final String DESCRIPTOR_NAME = "hibernate-service.xml"; 50 private static final String RELATIVE_DESCRIPTOR_URL = "META-INF/" + DESCRIPTOR_NAME; 51 52 private ServiceControllerMBean serviceController; 53 54 55 private HashMap deployments = new HashMap (); 56 57 58 private boolean validateDTDs; 59 60 65 public HARDeployer() 66 { 67 setSuffixes( new String [] { ".har" } ); 68 setRelativeOrder( RELATIVE_ORDER_400 ); 69 } 70 71 76 public Iterator getDeployedApplications() 77 { 78 return deployments.values().iterator(); 79 } 80 81 protected ObjectName getObjectName(MBeanServer server, ObjectName name) 82 throws MalformedObjectNameException 83 { 84 return name == null ? OBJECT_NAME : name; 85 } 86 87 90 protected void startService() throws Exception 91 { 92 serviceController = ( ServiceControllerMBean ) MBeanProxyExt.create( 93 ServiceControllerMBean.class, 94 ServiceControllerMBean.OBJECT_NAME, server 95 ); 96 97 super.startService(); 99 } 100 101 105 protected void stopService() throws Exception 106 { 107 Iterator modules = deployments.values().iterator(); 108 while ( modules.hasNext() ) 109 { 110 DeploymentInfo di = (DeploymentInfo)modules.next(); 111 stop(di); 112 } 113 114 modules = new ArrayList ( deployments.values() ).iterator(); 116 while ( modules.hasNext() ) 117 { 118 DeploymentInfo di = (DeploymentInfo)modules.next(); 119 destroy(di); 120 } 121 deployments.clear(); 122 123 super.stopService(); 125 126 serviceController = null; 127 } 128 129 136 public boolean getValidateDTDs() 137 { 138 return validateDTDs; 139 } 140 141 148 public void setValidateDTDs(boolean validate) 149 { 150 this.validateDTDs = validate; 151 } 152 153 protected void processNestedDeployments(DeploymentInfo di) throws DeploymentException 166 { 167 super.processNestedDeployments( di ); 168 } 169 170 protected void deployUrl(DeploymentInfo di, URL url, String name) throws DeploymentException 171 { 172 super.deployUrl( di, url, name ); 173 } 174 175 public boolean isDeployable(String name, URL url) 176 { 177 log.debug( "Checking deployability of [name=" + name + ", url=" + url.getFile() + "]" ); 182 return name.endsWith( ".jar" ) || name.endsWith( RELATIVE_DESCRIPTOR_URL ); 183 } 184 186 196 public boolean accepts(DeploymentInfo di) 197 { 198 String urlStr = di.url.getFile(); 200 if ( !urlStr.endsWith( ".har" ) && !urlStr.endsWith( ".har/" ) ) 201 { 202 return false; 203 } 204 205 boolean accept = false; 207 try 208 { 209 URL dd = di.localCl.findResource( RELATIVE_DESCRIPTOR_URL ); 210 if ( dd != null ) 211 { 212 if ( di.localUrl != null ) 216 { 217 urlStr = di.localUrl.toString(); 218 } 219 220 String ddStr = dd.toString(); 221 if( ddStr.indexOf( urlStr ) >= 0 ) 222 { 223 accept = true; 224 } 225 } 226 } 227 catch( Exception ignore ) 228 { 229 } 230 231 log.debug( "accept> url=" + di.url + ", accepted=" + accept ); 232 233 return accept; 234 } 235 236 242 public void init(DeploymentInfo di) throws DeploymentException 243 { 244 log.debug( "Deploying HAR; init; " + di ); 245 try 246 { 247 if( "file".equalsIgnoreCase( di.url.getProtocol() ) ) 248 { 249 File file = new File ( di.url.getFile() ); 250 251 if ( !file.isDirectory() ) 252 { 253 di.watch = di.url; 255 } 256 else 257 { 258 di.watch = new URL ( di.url, DESCRIPTOR_NAME ); 260 } 261 } 262 else 263 { 264 di.watch = di.url; 266 } 267 } 268 catch( Exception e ) 269 { 270 if ( e instanceof DeploymentException ) 271 { 272 throw ( DeploymentException ) e; 273 } 274 throw new DeploymentException( "failed to initialize", e ); 275 } 276 277 super.init( di ); 279 } 280 281 public synchronized void create(DeploymentInfo di) throws DeploymentException 282 { 283 log.debug( "Deploying HAR; create; " + di ); 284 super.create( di ); 285 286 Iterator subdeployments = di.subDeployments.iterator(); 293 while ( subdeployments.hasNext() ) 294 { 295 DeploymentInfo nested = ( DeploymentInfo ) subdeployments.next(); 296 log.debug( "Checking sub-deployment [" + nested.url + "] for descriptor-name" ); 297 298 if ( nested.url.getFile().endsWith( DESCRIPTOR_NAME ) ) 301 { 302 log.debug( "Attempting to locate HibernateMBean in sub-deployment [" + nested.url + "]" ); 303 Iterator mbeans = nested.mbeans.iterator(); 304 305 while ( mbeans.hasNext() ) 309 { 310 ObjectName service = ( ObjectName ) mbeans.next(); 311 log.debug( "Testing [" + service + "] as HibernateMBean" ); 312 if ( isHibernateMBean( service ) ) 313 { 314 log.debug( "Located HibernateMBean" ); 315 Attribute attr = new Attribute ( "HarUrl", di.url ); 316 try 317 { 318 server.setAttribute( service, attr ); 319 } 320 catch( Exception e ) 321 { 322 throw new DeploymentException( "Failed to set HarUrl attribute: " + e.getMessage(), e ); 323 } 324 } 325 } 326 break; 327 } 328 } 329 } 330 331 public synchronized void start(DeploymentInfo di) throws DeploymentException 332 { 333 log.debug( "Deploying HAR; start; " + di ); 334 super.start( di ); 335 } 336 337 public void stop(DeploymentInfo di) throws DeploymentException 338 { 339 log.debug( "Undeploying HAR; stop; " + di ); 340 try 341 { 342 serviceController.stop(di.deployedObject); 343 } 344 catch(Exception e) 345 { 346 throw new DeploymentException("problem stopping har module: " + di.url, e); 347 } 348 super.stop(di); 349 } 350 351 public void destroy(DeploymentInfo di) throws DeploymentException 352 { 353 log.debug( "Undeploying HAR; destroy; " + di ); 354 deployments.remove(di.url); 356 357 try 358 { 359 serviceController.destroy(di.deployedObject); 360 serviceController.remove(di.deployedObject); 361 } 362 catch(Exception e) 363 { 364 throw new DeploymentException("problem destroying har module: " + di.url, e); 365 } 366 super.destroy( di ); 367 } 368 369 private boolean isHibernateMBean(ObjectName service) 370 { 371 try 372 { 373 MBeanInfo serviceInfo = server.getMBeanInfo( service ); 374 return Hibernate.class.getName().equals( serviceInfo.getClassName() ); 375 } 376 catch( Throwable t ) 377 { 378 log.warn( "Unable to determine whether MBean [" + service + "] is Hibernate MBean" ); 379 return false; 380 } 381 } 382 } 383 | Popular Tags |