12 Replies Latest reply on Sep 23, 2003 4:39 PM by Adrian Brock

    EJBDeployer Classloader

    Joao Machado Newbie

      Hi all,

      I need to load a class inside a JAR file with the same classloader that loaded my EJB classes because this class is an implementation of an interface class that already has been loaded. So I thought in two possible solutions :

      1 - Get the EJB Class Loader and load my JAR
      2 - Change the EJB Classloader to be one that I could write

      I don't know exactly how to do anyone of them, anyone has sugestions/ideas on how I could do this ?

        • 1. Re: EJBDeployer Classloader
          Stephane Nicoll Master

          This question is not related to JMS, is it?

          otherwise put your EJB Classe and your other class inside an EAR file and it will use the same classloader.

          Regards,

          Stephane

          • 2. Re: EJBDeployer Classloader
            Joao Machado Newbie

            The problem is that the class could not exists at the moment of my deployment.

            Let me explain better the problem. Look at this snippet of code below:

            String className = properties.getClassName();
            ISomething something = (ISomething) Class.forName(className).newInstance();
            something.doSomething();

            The className variable is dynamic and could be changed in runtime and points even to a class that is defined outside my EAR or JAR.

            This class in another JAR was loaded implementing the ISomething interface that was not loaded with the same classloader of my EAR or JAR that runs inside JBoss, so I have ClassCastException.

            I have to use the same classloader for both ocasions...

            • 3. Re: EJBDeployer Classloader
              Adrian Brock Master

              Just deploy ISomething in one place, e.g. put it in
              a jar in server/default/lib

              Regards,
              Adrian

              • 4. Re: EJBDeployer Classloader
                Joao Machado Newbie

                Hi Adrian,

                I had done that. The problem is that the classes that implements the ISometinhg aren't in the classpath initially, so I need to load the JAR (containng that classes) prior to execute a Class.forName().

                I used the URLClassLoader to do this, and (of course) when I try to cast the class I got Class Cast Exception.

                I need to load my JAR by the way of EJBDeployer classloader right ? My questiuon is , how can I do this ?

                Other way is to create my own classloader and assign it to be the EJBdeployer classloader... What is better ?

                • 5. Re: EJBDeployer Classloader
                  Adrian Brock Master

                  You can use the ClassPathExtension MBean to deploy a URL.
                  There is an example in the testsuite.

                  Regards,
                  Adrian

                  • 6. Re: EJBDeployer Classloader
                    Joao Machado Newbie

                    Adrian,

                    I think that it will solve my problem, but I didn't found the MBean that you told (I'm using Jboss 3.04, but I tryed 3.2.1 too). Where is it ?

                    I haven't found the JBoss test that uses it too, if you point me to it I will be very grateful.

                    Thanks in advance !

                    • 7. Re: EJBDeployer Classloader
                      Stephane Nicoll Master

                      Check the CVS on sourceforge for testsuite

                      http://sf.net/projects/jboss

                      Regards,

                      Stephane

                      • 8. Re: EJBDeployer Classloader
                        Adrian Brock Master



                        {url to some.jar}


                        Regards,
                        adrian

                        • 9. Re: EJBDeployer Classloader
                          Joao Machado Newbie

                          Is there anyway to access it by RMIAdaptor ?

                          If I put the XML that you sent, I will be extending the classpath for a specified JAR, and I need to do this at runtime by code.

                          • 10. Re: EJBDeployer Classloader
                            Adrian Brock Master

                            The example in the testsuite does exactly that
                            getServer() returns an RMIAdaptor, an MBeanServer
                            will work just well (if you are not remote):

                            ObjectName extension;

                            public ClasspathExtensionUnitTestCase(String name)
                            {
                            super(name);

                            try
                            {
                            extension = new ObjectName("jboss.test:test=ClasspathTest,mbean=extension");
                            }
                            catch (Exception ignored)
                            {
                            }
                            }

                            public void testClasspathExtension() throws Exception
                            {
                            assertTrue("Shouldn't find the resource before deployment", findResource() == false);
                            getServer().createMBean("org.jboss.deployment.ClasspathExtension", extension);
                            try
                            {
                            getServer().setAttribute(extension, new Attribute("MetadataURL", getDeployURL("")));
                            getServer().invoke(extension, "create", new Object[0], new String[0]);
                            getServer().invoke(extension, "start", new Object[0], new String[0]);
                            assertTrue("Should find the resource after deployment", findResource());
                            }
                            finally
                            {
                            getServer().invoke(extension, "stop", new Object[0], new String[0]);
                            getServer().invoke(extension, "destroy", new Object[0], new String[0]);
                            getServer().unregisterMBean(extension);
                            }
                            assertTrue("Shouldn't find the resource after undeployment", findResource() == false);
                            }

                            Regards,
                            Adrian

                            • 11. Re: EJBDeployer Classloader
                              Joao Machado Newbie

                              Adrian,

                              If I call the MainDeployer MBEAN passing the JAR URL as the parameter it will be in the classpath right ?

                              So what's the diference between do this or call the ClassPathExtension MBEAN ?

                              Thanks again,

                              Joao

                              • 12. Re: EJBDeployer Classloader
                                Adrian Brock Master

                                None if the jar does not contain any xml files in META-INF
                                or there are subdeployments.

                                Both add a URL to the classpath.

                                The MainDeployer first checks for deployment descriptors
                                and also looks for other deployments inside the jar/directory.

                                Regards,
                                Adrian