Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * JBoss, Home of Professional Open Source
  * Copyright 2006, JBoss Inc., and individual contributors as indicated
  * by the @authors tag. See the copyright.txt in the distribution for a
  * full listing of individual contributors.
  *
  * This is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as
  * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 package org.jboss.embedded;
 
 import java.net.URL;
 import java.util.List;
 
Basic bootstrap class for embeddable JBoss

Author(s):
Bill Burke
adrian@jboss.org
Version:
$Revision: 1.1 $
 
 public class Bootstrap
 {
    private static final Logger log = Logger.getLogger(Bootstrap.class);
 
    public static final String BOOTSTRAP_RESOURCE_PATH="jboss.embedded.bootstrap.resource.path";
    public static final String BOOTSTRAP_RESOURCE_FILE="conf/bootstrap-beans.xml";
 
    protected Kernel kernel;
    protected ClassLoader loader = Thread.currentThread().getContextClassLoader();
    protected MainDeployer mainDeployer;
    protected boolean started;
    protected boolean ignoreShutdownErrors;
 
    private static Bootstrap instance;

   
For those applications that need a singelton Bootstrap instance

Returns:
the bootstrap
 
    public static synchronized Bootstrap getInstance()
    {
       if ( == null)
           = new Bootstrap(createKernel());
 
       return ;
    }
 
    public Bootstrap()
    {
       this. = createKernel();
    }
 
    public Bootstrap(Kernel kernel)
    {
       this. = kernel;
    }
 
    public boolean isIgnoreShutdownErrors()
    {
       return ;
    }
 
    public void setIgnoreShutdownErrors(boolean ignoreShutdownErrors)
    {
       this. = ignoreShutdownErrors;
    }
 
    public boolean isStarted()
    {
       return ;
    }
 
   public Kernel getKernel()
   {
      return ;
   }
   public void setKernel(Kernel kernel)
   {
      this. = kernel;
   }
   public ClassLoader getLoader()
   {
      return ;
   }
   public void setLoader(ClassLoader loader)
   {
      this. = loader;
   }
   protected static Kernel createKernel()
   {
      BasicBootstrap bootstrap1 = new BasicBootstrap();
      bootstrap1.run();
      return bootstrap1.getKernel();
   }
   protected void deployBaseBootstrapUrl(URL urlthrows Throwable
   {
      BeanXMLDeployer deployer = new BeanXMLDeployer();
      deployer.deploy(url);
   }
   protected void bootstrapURL(URL urlthrows DeploymentException
   {
      try
      {
         // ServerConfig has to be created and installed before boostrap as we may want to use
         // system properties set up in ServerConfig
         ServerConfig config = new ServerConfig();
         AbstractBeanMetaData bmd = new AbstractBeanMetaData("ServerConfig"ServerConfig.class.getName());
         .getController().install(bmdconfig);
         deployBaseBootstrapUrl(url);
          = (MainDeployer).getRegistry().getEntry("MainDeployer").getTarget();
      }
      catch (Throwable throwable)
      {
         throw new RuntimeException("Unable to bootstrap: "throwable);
      }
       = true;
   }

   
Specify top classpath resource directory where base JBoss Embedded directory structure is. The Embedded JBoss directory structure is determined by extrapolating a directory from a base classpath resource. The absolute directory will be determined by doing classloader.getResource(bootstrapResourcePath + "conf/bootstrap-beans.xml")

Parameters:
bootstrapResourcePath
Throws:
org.jboss.deployers.spi.DeploymentException
   public void bootstrap(String bootstrapResourcePaththrows DeploymentException
   {
      if (bootstrapResourcePath == null)
      {
         bootstrapResourcePath = "";
      }
      else if (!bootstrapResourcePath.equals("") && !bootstrapResourcePath.endsWith("/"))
      {
         bootstrapResourcePath += "/";
      }
      System.setProperty(bootstrapResourcePath);
      bootstrapResourcePath += ;
      URL url = .getResource(bootstrapResourcePath);
      if (url == null)
         throw new DeploymentException("Unable to find bootstrap file: " + bootstrapResourcePath + " in classpath");
      bootstrapURL(url);
   }

   
Will obtain resource path from jboss.embedded.bootstrap.resource.path System Property. Otherwise it just invoked bootstrap(String bootstrapResourcePath) with ""

   public void bootstrap() throws DeploymentException
   {
      String path = System.getProperty("");
      bootstrap(path);
   }

   
Shutdown the kernel and all deployments
   public void shutdown()
   {
      try
      {
         .shutdown();
      }
      catch (Exception e)
      {
         if (!)
            throw new RuntimeException(e);
         else
            .error("Failed to shutdown Bootstrap"e);
      }
   }

   
Look in java.class.path for any .jar or class directories whose base file/dir match any base file/dir names in the comma delimited path parameter If classpath is: /home/wburke/jars/foo.jar and path is: "foo.jar" This will be a match and that .jar file will be deployed

Parameters:
path can be comma delimited
Throws:
org.jboss.deployers.spi.DeploymentException
   public void scanClasspath(String paththrows DeploymentException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.addClasspath(path);
      group.process();
   }

   
Undeploy something deployed via scanSclasspath()

   public void undeployClasspath(String paththrows DeploymentException
   {
      List<URLpaths = DeploymentGroup.getClassPaths(path);
      for (URL url : paths)
      {
         undeploy(url);
      }
   }

   
Deploy the classpath directories or .jar files a classloader resource is located in. ClassLoader.getResources() is used to find the base resources. i.e. classpath is "/home/wburke/lib/tutorial.jar:/home/wburke/lib/pu.jar" tutorial.jar and pu.jar has "META-INF/persistence.xml" resource within it. addResourceBases("META-INF/persistence.xml") will try and deploy tutorial.jar and pu.jar because the both have the META-INF/persistence.xml resource within them.

Parameters:
baseResource
Throws:
org.jboss.deployers.spi.DeploymentException
   public void deployResourceBase(String baseResourcethrows DeploymentException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.addResourceBase(baseResource);
      group.process();
   }

   

Parameters:
baseResource
Throws:
org.jboss.deployers.spi.DeploymentException
   public void deployResourceBases(String baseResourcethrows DeploymentException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.addResourceBases(baseResource);
      group.process();
   }

   
Find the .class resource of the given class Deploy a URL pointing to the classpath the resource is located in. i.e. classpath is "/home/wburke/lib/tutorial.jar" tutorial.jar has "META-INF/persistence.xml" resource within it. addResourceBase("META-INF/persistence.xml") will try and deploy tutorial.jar classloader.getResource("META-INF/persistence.xml") is used to determine the base location

Parameters:
baseResource
Throws:
org.jboss.deployers.spi.DeploymentException
   public void deployResourceBase(Class baseResourcethrows DeploymentException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.addResourceBase(baseResource);
      group.process();
   }
   public void deploy(URL urlthrows DeploymentException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.add(url);
      group.process();
   }
   public void deploy(VirtualFile filethrows DeploymentException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.add(file);
      group.process();
   }

   
Deploy a resource found by getResource() on the kernel's classloader

   public void deployResource(String resourcethrows DeploymentException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.addResource(resource);
      group.process();
   }

   
Define a deploy directory and deploy all files within it. The recurse parameter tells whether to recurse into sub directories for deployments

   public void deployDirectory(URL urlboolean recursethrows DeploymentExceptionIOException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.addDirectory(urlrecurse);
      group.process();
   }

   
Find a deploy directory from a base resource

   public void deployDirectoryFromResource(String resourceboolean recursethrows DeploymentExceptionIOException
   {
      DeploymentGroup group = createDeploymentGroup();
      group.addDirectoryByResource(resourcerecurse);
      group.process();
   }

   
opposite of deployResourceBase()

Parameters:
baseResource
Throws:
org.jboss.deployers.spi.DeploymentException
   public void undeployResourceBase(String baseResourcethrows DeploymentException
   {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      if ( != null)
         classLoader = ;
      URL url = classLoader.getResource(baseResource);
      if (url == null)
         throw new RuntimeException("Could not find baseResource: " + baseResource);
      undeployResourceBase(urlbaseResource);
   }
   private void undeployResourceBase(URL urlString baseResource)
           throws DeploymentException
   {
      String urlString = url.toString();
      int idx = urlString.lastIndexOf(baseResource);
      urlString = urlString.substring(0, idx);
      URL deployUrl;
      try
      {
         deployUrl = new URL(urlString);
      }
      catch (MalformedURLException e)
      {
         throw new RuntimeException(e);
      }
      undeploy(deployUrl);
   }
   public void undeployResourceBases(String baseResourcethrows DeploymentException
   {
      try
      {
         Enumeration<URLurls = .getResources(baseResource);
         while (urls.hasMoreElements())
         {
            URL url = urls.nextElement();
            undeployResourceBase(urlbaseResource);
         }
      }
      catch (IOException e)
      {
         throw new RuntimeException(e);
      }
   }

   
opposite of deployResourceBase()

Parameters:
baseResource
Throws:
org.jboss.deployers.spi.DeploymentException
   public void undeployResourceBase(Class baseResourcethrows DeploymentException
   {
      String resource = baseResource.getName().replace('.''/') + ".class";
      undeployResourceBase(resource);
   }
   
opposite of deployResource

   public void undeployResource(String resourcethrows DeploymentException
   {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      if ( != null)
         classLoader = ;
      URL url = classLoader.getResource(resource);
      if (url == null)
         throw new NullPointerException("Resource was null: " + resource);
      undeploy(url);
   }
   public void undeploy(URL urlthrows DeploymentException
   {
      VirtualFile vf = DeploymentGroup.getVirtualFile(url);
      undeploy(vf);
   }
   public void undeploy(VirtualFile vf)
           throws DeploymentException
   {
      .process();
   }
   public void undeployDirectory(URL urlboolean recursethrows DeploymentExceptionIOException
   {
      List<VirtualFilefiles = DeploymentGroup.getDeployerDirUrls(nullurlrecurse);
      for (VirtualFile vf : files)
         .removeDeployment(vf.getName());
      .process();
   }
   public void undeployDirectoryFromResource(String resourceboolean recursethrows DeploymentExceptionIOException
   {
      ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
      if ( != nullclassLoader = ;
      List<VirtualFilefiles = DeploymentGroup.getDeployerDirUrlsFromResource(nullclassLoaderresourcerecurse);
      for (VirtualFile vf : files)
         .removeDeployment(vf.getName());
      .process();
   }
   {
      DeploymentGroup group = new DeploymentGroup();
      group.setClassLoader();
      group.setMainDeployer();
      group.setKernel();
      return group;
   }
   public static void main(String[] argsthrows Exception
   {
      getInstance().bootstrap();
      for (String arg : args)
      {
         getInstance().scanClasspath(arg);
      }
      ..println("Running...");
      Thread t = new Thread();
      t.setDaemon(false);
      t.start();
   }
 }
New to GrepCode? Check out our FAQ X