FileDocCategorySizeDatePackage
EJB3StandaloneDeployer.javaAPI DocJBoss 4.2.117361Fri Jul 13 20:53:50 BST 2007org.jboss.ejb3.embedded

EJB3StandaloneDeployer

public class EJB3StandaloneDeployer extends Object
When initialized properly, this class will search for annotated classes and archives in your classpath and try to create EJB containers and EntityManagers automatically.

All classes and jars must already be in your classpath for this deployer to work.

author
Bill Burke
version
$Revision: 60233 $

Fields Summary
protected static final Logger
log
protected Set
archives
protected Set
deployDirs
protected Set
archivesByResource
protected Set
deployDirsByResource
protected ClassLoader
classLoader
private Map
defaultPersistenceProperties
private Hashtable
jndiProperties
private List
deployments
private org.jboss.kernel.Kernel
kernel
private MBeanServer
mbeanServer
Constructors Summary
public EJB3StandaloneDeployer()



    
   
      classLoader = Thread.currentThread().getContextClassLoader();
   
Methods Summary
public voidcreate()

      try
      {
         for (String resource : deployDirsByResource)
         {
            Enumeration<URL> urls = classLoader.getResources(resource);
            while (urls.hasMoreElements())
            {
               URL url = urls.nextElement();
               URL deployUrl = getDeployDirFromResource(url, resource);
               deployDirs.add(deployUrl);
            }
         }

         for (URL url : deployDirs)
         {
            File dir = new File(url.toURI());
            for (File fp : dir.listFiles())
            {
               if (fp.isDirectory())
               {
                  archives.add(fp.toURL());
                  continue;
               }
               try
               {
                  ZipFile zip = new ZipFile(fp);
                  zip.entries();
                  zip.close();
                  archives.add(fp.toURL());
               }
               catch (IOException e)
               {
               }
            }
         }

         for (String resource : archivesByResource)
         {
            Enumeration<URL> urls = classLoader.getResources(resource);
            while (urls.hasMoreElements())
            {
               URL url = urls.nextElement();
               URL archiveUrl = getContainingUrlFromResource(url, resource);
               archives.add(archiveUrl);
            }
         }

         if (defaultPersistenceProperties == null)
         {
            InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("default.persistence.properties");
            if (is == null) throw new RuntimeException("cannot find default.persistence.properties");
            Properties defaults = new Properties();
            defaults.load(is);
            defaultPersistenceProperties = defaults;
         }

         for (URL archive : archives)
         {
            DeployerUnit du = new DeployerUnit(classLoader, archive, defaultPersistenceProperties, jndiProperties);
            EJB3StandaloneDeployment deployment = new EJB3StandaloneDeployment(du, kernel, mbeanServer);
            deployments.add(deployment);
            deployment.create();
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
         throw e;
      }
   
public voiddestroy()

      for (EJB3StandaloneDeployment deployment : deployments)
      {
         deployment.destroy();
      }
   
public java.util.SetgetArchives()
A set of URLs of jar archives to search for annotated EJB classes

return
this will not return null.

      return archives;
   
public java.util.SetgetArchivesByResource()
All strings in this set will be used with ClassLoader.getResources() All URLs returned will be used to search for annotated classes.

return

      return archivesByResource;
   
public java.lang.ClassLoadergetClassLoader()

      return classLoader;
   
public static java.net.URLgetContainingUrlFromResource(java.net.URL url, java.lang.String resource)

      if (url.getProtocol().equals("jar"))
      {
         URL jarURL = url;
         URLConnection urlConn = jarURL.openConnection();
         JarURLConnection jarConn = (JarURLConnection) urlConn;
         // Extract the archive to dest/jarName-contents/archive
         String parentArchiveName = jarConn.getJarFile().getName();
         File fp = new File(parentArchiveName);
         return fp.toURL();
      }

      // its a file
      String base = url.toString();
      int idx = base.lastIndexOf(resource);
      base = base.substring(0, idx);
      return new URL(base);
   
public java.util.MapgetDefaultPersistenceProperties()

      return defaultPersistenceProperties;
   
public static java.net.URLgetDeployDirFromResource(java.net.URL url, java.lang.String resource)

      if (url.getProtocol().equals("jar"))
      {
         URL jarURL = url;
         URLConnection urlConn = jarURL.openConnection();
         JarURLConnection jarConn = (JarURLConnection) urlConn;
         // Extract the archive to dest/jarName-contents/archive
         String parentArchiveName = jarConn.getJarFile().getName();
         File fp = new File(parentArchiveName);
         return fp.getParentFile().toURL();
      }

      // its a file
      String base = url.toString();
      int idx = base.lastIndexOf(resource);
      base = base.substring(0, idx);
      File fp = new File(base);
      return fp.getParentFile().toURL();
   
public java.util.SetgetDeployDirs()
Set of directories where there are jar files and directories The deployer will search through all archives and directories for annotated classes

return
this will not return null.

      return deployDirs;
   
public java.util.SetgetDeployDirsByResource()
All strings in this set will be used with ClassLoader.getResources(). All URLs returned will be create a set of deploy directories that will be used to find archives and directories that will be searched for annotated classes

return

      return deployDirsByResource;
   
public java.util.ListgetDeployments()
Returns a list of deployments found in this

return

      return deployments;
   
public java.util.HashtablegetJndiProperties()

      return jndiProperties;
   
public org.jboss.kernel.KernelgetKernel()

      return kernel;
   
public javax.management.MBeanServergetMbeanServer()
This is used by deployer for @Service beans that have @Management interfaces

return

      return mbeanServer;
   
private voidloadMbeanServer()

      if (mbeanServer == null)
      {
         ControllerContext context = kernel.getController().getInstalledContext("MBeanServer");

         if (context != null)
            mbeanServer = (MBeanServer) context.getTarget();
         else
         {
            ArrayList servers = MBeanServerFactory.findMBeanServer(null);
            if (servers.size() == 0)
               mbeanServer = MBeanServerFactory.createMBeanServer();
            else
               mbeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
         }
      }
   
private voidlookup(java.lang.String name)

      System.out.println("lookup " + name);
      try {
         InitialContext jndiContext = new InitialContext();
         NamingEnumeration names = jndiContext.list(name);
         if (names != null){
            while (names.hasMore()){
               System.out.println("  " + names.next());
            }
         }
      } catch (Exception e){
      }
   
public voidsetArchives(java.util.Set archives)

      new Exception().printStackTrace();
      this.archives = archives;
   
public voidsetArchivesByResource(java.util.Set archivesByResource)

      this.archivesByResource = archivesByResource;
   
public voidsetClassLoader(java.lang.ClassLoader classLoader)
You can set the classloader that will be used

param
classLoader

      this.classLoader = classLoader;
   
public voidsetDefaultPersistenceProperties(java.util.Map defaultPersistenceProperties)
If you do not specifiy the default persistence properties, the resource "default.persistence.properties" will be search for in your classpath

param
defaultPersistenceProperties

      this.defaultPersistenceProperties = defaultPersistenceProperties;
   
public voidsetDeployDirs(java.util.Set deployDirs)

      this.deployDirs = deployDirs;
   
public voidsetDeployDirsByResource(java.util.Set deployDirsByResource)

      this.deployDirsByResource = deployDirsByResource;
   
public voidsetDeployments(java.util.List deployments)

      this.deployments = deployments;
   
public voidsetJndiProperties(java.util.Hashtable jndiProperties)

      this.jndiProperties = jndiProperties;
   
public voidsetKernel(org.jboss.kernel.Kernel kernel)

      this.kernel = kernel;
   
public voidsetMbeanServer(javax.management.MBeanServer mbeanServer)

      this.mbeanServer = mbeanServer;
   
public voidstart()

      try
      {
         loadMbeanServer();

         for (EJB3StandaloneDeployment deployment : deployments)
         {
            if (deployment.getMbeanServer() == null)
            {
               deployment.setMbeanServer(mbeanServer);
            }

            deployment.start();
            lookup("");
         }
      }
      catch (Exception e)
      {
         e.printStackTrace();
         throw e;
      }
   
public voidstop()

      for (EJB3StandaloneDeployment deployment : deployments)
      {
         deployment.stop();
      }