6 Replies Latest reply on Nov 22, 2009 8:00 AM by juergen.zimmermann

    How to get DeploymentUnit?

    juergen.zimmermann

      I want to integrate Jersey (aka reference impl for JAX-RS) and JBoss. Therefore, I have to provide an implementation for the annotation @EJB by myself.

      At http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4235907#4235907 I found these 3 steps by Ales Justin:

      (1) Get CL for the class
      (2) Get the Module for CL
      (3) Search all DUs for matching Module
      --> the one that has that Module as an attachment is your DU

      (1) is no problem. I get the classloader object of type org.jboss.classloader.spi.base.BaseClassLoader. Invoking BaseClassLoader.getName() I get the associated url, e.g. "vfsfile:/.../deploy/hska.ear/hskaREST.war"

      (2) is a problem, because I don't know which method should be invoked on BaseClassLoader. Any hint is appreciated!

      (3) will probabely the next problem. How can I get a list of all available DeploymentUnits?

        • 1. Re: How to get DeploymentUnit?
          alesj

          Actually that's not correct (hence my "off the top of my head"),
          as Module/ClassLoader can be created from different/multiple (sub)deployments.
          e.g. ear + jars

          Why exactly do you need DeploymentUnit?
          What is your environment - where do you want to access DU from?
          Looks like class's classloader is your starting point. Why?

          Give more details and we'll find the solution together.

          • 2. Re: How to get DeploymentUnit?
            juergen.zimmermann

            I've to implement a class as follows:

            import java.lang.reflect.Type;
            import javax.ejb.EJB;
            import javax.naming.Context;
            import javax.naming.InitialContext;
            import javax.naming.NamingException;
            import javax.ws.rs.ext.Provider;
            import com.sun.jersey.core.spi.component.ComponentContext;
            import com.sun.jersey.spi.inject.Injectable;
            import com.sun.jersey.spi.inject.InjectableProvider;
            
            @Provider
            public class EJBProvider implements InjectableProvider<EJB, Type> {
             public Injectable<Object> getInjectable(ComponentContext componentCtx,
             EJB ejbAnnotation,
             Type type) {
             if (!(type instanceof Class<?>))
             return null;
            
             Class<?> clazz = (Class<?>) type;
            
             String beanInterface = ejbAnnotation.beanInterface().getName();
             if (Object.class.getName().equals(beanInterface)) {
             beanInterface = clazz.getName();
             }
            
             String jndiName = ???; // get jndi name via beanInterface
            
             Context ctx = null;
             try {
             ctx = new InitialContext();
             Object obj = ctx.lookup(jndiName);
            
             return new Injectable<Object>() {
             public Object getValue() {
             return obj;
             }
             };
             }
             catch (NamingException e) { /* error handling */ }
             finally { /* close ctx */ }
             }
            }


            So my 1st problem is: how to get the jndi name when the interface of an EJB is available?

            I was trying the following where I only would need the DeploymentUnit object. Perhaps there are alternatives. Any hint is highly appreciated!
            //import org.jboss.deployers.structure.spi.DeploymentUnit;
            //import org.jboss.ejb3.common.resolvers.plugins.FirstMatchEjbReferenceResolver;
            //import org.jboss.ejb3.common.resolvers.spi.EjbReference;
            //import org.jboss.ejb3.common.resolvers.spi.EjbReferenceResolver;
            
            EjbReference ref = new EjbReference(ejbAnnotation.beanName(),
             beanInterface,
             ejbAnnotation.mappedName());
            EjbReferenceResolver resolver = new FirstMatchEjbReferenceResolver();
            
            DeploymentUnit du = ???; // How to get DeploymentUnit? Via ClassLoader?
            
            String jndiName = resolver.resolveEjb(du, ref);
            


            • 3. Re: How to get DeploymentUnit?
              juergen.zimmermann

              BTW, the whole deployment is in an EAR, the class EJBProvider (see above) is in a web module, and the referenced EJB of course in an EJB module.

              • 4. Re: How to get DeploymentUnit?
                alesj

                 

                "Juergen.Zimmermann" wrote:

                So my 1st problem is: how to get the jndi name when the interface of an EJB is available?

                Dunno, this is EJB question not MC. ;-)
                Either ask on their forum or check Weld-int
                where, afair, something similar had to be implemented.

                "Juergen.Zimmermann" wrote:

                DeploymentUnit du = ???; // How to get DeploymentUnit? Via ClassLoader?
                


                ClassLoader cl = clazz.getClassLoader();
                Module module = ClassLoading.getModuleForClassLoader(cl);
                if (module instanceof AbstractDeploymentClassLoaderPolicyModule)
                {
                 AbstractDeploymentClassLoaderPolicyModule deploymentModule = (AbstractDeploymentClassLoaderPolicyModule)module;
                 DeploymentUnit unit = deploymentModule.getDeploymentUnit();
                

                This way you get the deployment unit owning the classloader/module,
                but to actually get the right deployment unit you would need to check the whole hierarchy of this unit.
                e.g.
                get class's source location, and check it against unit's and sub-units classpath roots.

                • 5. Re: How to get DeploymentUnit?
                  alesj

                   

                  "Juergen.Zimmermann" wrote:
                  the class EJBProvider (see above) is in a web module

                  You can use MC-int to get DeploymentUnit from ServletContext.
                  * http://anonsvn.jboss.org/repos/jbossas/projects/mc-int/trunk/servlet/

                  • 6. Re: How to get DeploymentUnit?
                    juergen.zimmermann

                    Thank you very much for your help!