1 22 package org.jboss.system.server.profileservice; 23 24 import java.net.URL ; 25 import java.util.Collection ; 26 import java.util.Enumeration ; 27 28 import org.jboss.beans.metadata.plugins.AbstractBeanMetaData; 29 import org.jboss.dependency.spi.ControllerContext; 30 import org.jboss.deployers.spi.DeploymentException; 31 import org.jboss.deployers.spi.IncompleteDeploymentException; 32 import org.jboss.deployers.spi.IncompleteDeployments; 33 import org.jboss.deployers.spi.IncompleteDeploymentsBuilder; 34 import org.jboss.deployers.spi.deployment.MainDeployer; 35 import org.jboss.deployers.spi.structure.DeploymentContext; 36 import org.jboss.kernel.Kernel; 37 import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap; 38 import org.jboss.kernel.plugins.deployment.xml.BasicXMLDeployer; 39 import org.jboss.kernel.spi.dependency.KernelController; 40 import org.jboss.profileservice.spi.Profile; 41 import org.jboss.profileservice.spi.ProfileKey; 42 import org.jboss.profileservice.spi.ProfileService; 43 import org.jboss.system.server.Server; 44 45 53 public class ProfileServiceBootstrap extends BasicBootstrap 54 { 55 56 public static String BOOTSTRAP_XML_NAME = "bootstrap-beans.xml"; 57 58 59 protected String profileName; 60 61 62 protected Server server; 63 64 65 protected BasicXMLDeployer kernelDeployer; 66 67 68 protected KernelController controller; 69 70 71 protected MainDeployer mainDeployer; 72 73 74 protected ProfileService profileService; 75 76 77 protected String deployerBeansPrefix; 78 79 80 protected URL bootstrapURL; 81 82 85 public ProfileServiceBootstrap() 86 { 87 this("default", null); 88 } 89 90 96 public ProfileServiceBootstrap(String name, Server server) 97 { 98 this.profileName = name; 99 this.server = server; 100 } 101 102 public Server getServer() 103 { 104 return server; 105 } 106 107 112 public MainDeployer getMainDeployer() 113 { 114 return mainDeployer; 115 } 116 117 122 public ProfileService getProfileService() 123 { 124 return profileService; 125 } 126 127 132 public BasicXMLDeployer getKernelDeployer() 133 { 134 return kernelDeployer; 135 } 136 137 142 public String getDeployerBeansPrefix() 143 { 144 return deployerBeansPrefix; 145 } 146 147 154 public void setDeployerBeansPrefix(String prefix) 155 { 156 if (prefix != null && prefix.length() == 0) 157 prefix = null; 158 this.deployerBeansPrefix = prefix; 159 } 160 161 166 public URL getBootstrapURL() 167 { 168 return bootstrapURL; 169 } 170 171 177 public void setBootstrapURL(URL bootstrapURL) 178 { 179 this.bootstrapURL = bootstrapURL; 180 } 181 182 @Override 183 public void bootstrap() throws Throwable 184 { 185 super.bootstrap(); 187 188 kernelDeployer = new BasicXMLDeployer(getKernel()); 189 190 if (bootstrapURL == null) 191 { 192 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 193 194 String bootstrapResName; 195 if (deployerBeansPrefix == null) 196 bootstrapResName = profileName + "/" + BOOTSTRAP_XML_NAME; 197 else 198 bootstrapResName = deployerBeansPrefix + BOOTSTRAP_XML_NAME; 199 200 log.debug("Scanning for bootstrap resources: " + bootstrapResName); 201 Enumeration <URL > descriptors = cl.getResources(bootstrapResName); 202 while (descriptors.hasMoreElements()) 203 { 204 URL url = descriptors.nextElement(); 205 log.debug("Found: "+url); 206 deploy(url); 207 } 208 209 String metaInfBootstrapResName = "META-INF/" + bootstrapResName; 210 log.debug("Scanning for bootstrap resources: " + metaInfBootstrapResName); 211 descriptors = cl.getResources(metaInfBootstrapResName); 212 while (descriptors.hasMoreElements()) 213 { 214 URL url = descriptors.nextElement(); 215 log.debug("Found: "+url); 216 deploy(url); 217 } 218 } 219 else 220 { 221 deploy(bootstrapURL); 222 } 223 224 Kernel kernel = getKernel(); 225 controller = kernel.getController(); 226 227 if (server != null) 229 { 230 AbstractBeanMetaData metaData = new AbstractBeanMetaData("JBossServer", server.getClass().getName()); 231 controller.install(metaData, server); 232 } 233 234 checkIncomplete(); 236 237 profileService = getBean(controller, "ProfileService", ProfileService.class); 239 log.debug("Using ProfileService: " + profileService); 240 mainDeployer = getBean(controller, "MainDeployer", MainDeployer.class); 241 log.debug("Using MainDeployer: " + mainDeployer); 242 243 try 245 { 246 loadProfile(profileName); 247 } 248 catch (IncompleteDeploymentException e) 249 { 250 log.error("Failed to load profile: " + e.getMessage()); 251 } 252 catch (Exception e) 253 { 254 log.error("Failed to load profile: ", e); 255 } 256 } 257 258 268 protected <T> T getBean(KernelController controller, Object name, Class <T> expectedType) 269 { 270 ControllerContext context = controller.getInstalledContext(name); 271 if (context == null) 272 throw new IllegalStateException ("Context not installed: " + name); 273 Object result = context.getTarget(); 274 if (result == null) 275 throw new IllegalStateException ("No target for " + name); 276 if (expectedType.isInstance(result) == false) 277 throw new IllegalStateException (name + " expected " + expectedType.getName() + " was " + result.getClass().getName()); 278 return expectedType.cast(result); 279 } 280 281 287 protected void deploy(URL url) throws Throwable 288 { 289 log.debug("Deploying bootstrap config: " + url); 290 kernelDeployer.deploy(url); 291 } 292 293 298 protected void undeploy(URL url) 299 { 300 try 301 { 302 log.debug("Undeploying bootstrap config: " + url); 303 kernelDeployer.undeploy(url); 304 } 305 catch (Throwable t) 306 { 307 log.warn("Error during undeployment: " + url, t); 308 } 309 } 310 311 320 protected void loadProfile(String name) throws Throwable 321 { 322 MainDeployer deployer = getMainDeployer(); 323 if (deployer == null) 324 throw new NullPointerException ("MainDeployer has not been set"); 325 ProfileService ps = getProfileService(); 326 if (ps == null) 327 throw new NullPointerException ("ProfileService has not been set"); 328 329 ProfileKey key = new ProfileKey(name); 331 Profile profile = ps.getProfile(key); 332 333 DeploymentContext first = null; 335 336 Collection <DeploymentContext> boostraps = profile.getBootstraps(); 338 for (DeploymentContext d : boostraps) 339 { 340 deployer.addDeploymentContext(d); 341 if (first == null) 342 first = d; 343 } 344 deployer.process(); 345 checkIncomplete(); 346 347 Thread thread = Thread.currentThread(); 348 ClassLoader old = thread.getContextClassLoader(); 349 ClassLoader cl = first != null ? first.getClassLoader() : null; 350 if (cl != null) 351 thread.setContextClassLoader(cl); 352 try 353 { 354 355 Collection <DeploymentContext> profileDeployers = profile.getDeployers(); 357 for (DeploymentContext d : profileDeployers) 358 deployer.addDeploymentContext(d); 359 deployer.process(); 360 checkIncomplete(); 361 362 Collection <DeploymentContext> profileDeployments = profile.getDeployments(); 364 for (DeploymentContext d : profileDeployments) 365 deployer.addDeploymentContext(d); 366 deployer.process(); 367 checkIncomplete(); 368 } 369 finally 370 { 371 thread.setContextClassLoader(old); 372 } 373 } 374 375 380 protected void checkIncomplete() throws DeploymentException 381 { 382 IncompleteDeployments incomplete = IncompleteDeploymentsBuilder.build(mainDeployer, controller); 383 if (incomplete.isIncomplete()) 384 throw new IncompleteDeploymentException(incomplete); 385 } 386 } 387 | Popular Tags |