6 Replies Latest reply on Aug 1, 2004 8:05 PM by colaheavy

    how to register a object to JNDI namespace..?

    Jay Choi Newbie

      Hi, all..

      What I am trying to is that I want my class called PageMethodStaticXMLData which reads a xml file and to be used by all process, etc SessionBeans, Servlets..

      I created PageMethodStaticXMLData class to read and parse a xml data which is just a static lookup information and then trying to registered as a resource in ejb-jar.xml as the following:

      <resource-env-ref>
      <resource-env-ref-name>staticXMLData</resource-env-ref-name>
      <resource-env-ref-type>com.etn.data.PageMethodStaticXMLData</resource-env-ref-type>
      </resource-env-ref>



      then from a session bean trying to look it up as the following:

      Context initial = new InitialContext();
      PageMethodStaticXMLData data = (PageMethodStaticXMLData)initial.lookup("java:/staticXMLData");



      during the runtime, I get this message when the above code executed:

      javax.naming.NameNotFoundException: staticXMLData not bound



      So I am wondering if it is not possible to registered a custom class as a JNDI resource to be shared by SessionBeans & servlets or not..
      If it is possible, where did I go wrong here..??

      Thanks very much..

        • 1. Re: how to register a object to JNDI namespace..?
          therion Newbie

          hi shacka,

          Make your class PageMethodStaticXMLData as a MBean service and bind it in jndi context.

          Once you build your MBean you can bind it into the context using the example below:

          ...
          ...
          InitialContext rootCtx = new InitialContext ();
          Name fullName = rootCtx.getNameParser ( "" ).parse ( jndiName );
          NonSerializableFactory.rebind ( fullName, xmlDataClassInstance, true);
          ...
          ...
          


          Hope this helps!

          Therion.


          • 2. Re: how to register a object to JNDI namespace..?
            Jay Choi Newbie

            Ye..!!! My first ever reply from Jboss forum.. :)

            Thanks, therion.. Here are the follow up questions..

            [1] how to make a class as MBean service? which xml file to put it into?
            [2] and how to bind it in jndi context..? is it done from configuration xml file?
            [3] any class can be made as MBean serviable(?) or it has to implement something..?

            I am knew to this MBean service and I need help helplessly.. shy..
            Could you or anybody kindly tell me in details..?

            Thanks..

            • 3. Re: how to register a object to JNDI namespace..?
              therion Newbie

              shacka,

              Supose you build a MBean service named XMLDataLoader, you must create an interface XMLDataLoaderMBean and an implementation of that interface called XMLDataLoader.

              Example (a simple one)

              XMLDataLoaderMBean.java

              ...
              import javax.naming.NamingException;
              
              public interface XMLDataLoaderMBean {
              
               public String getJndiName ();
               public void setJndiName ( String jndiName ) throws NamingException;
               public void start () throws Exception;
               public void stop () throws Exception;
              
              }
              



              XMLDataLoader.java

              public class XMLDataLoader implements XMLDataLoaderMBean {
              
               PageMethodStaticXMLData xmlDataInstance;
               private String jndiName;
               private boolean started;
              
               public String getJndiName() {
               return jndiName;
               }
              
               public void setJndiName ( String jndiName ) throws NamingException {
               String oldName = this.jndiName;
               this.jndiName = jndiName;
               if ( started ) {
               unbind ( oldName );
               rebind ( jndiName, xmlDataInstance );
               }
               }
              
               public void start() throws Exception {
               started = true;
               xmlDataInstance = new PageMethodStaticXMLData ();
               rebind ( jndiName, xmlDataInstance );
               }
              
               public void stop() throws Exception {
               started = false;
               unbind ( jndiName );
               }
              
               private void rebind ( String jndiName, Object object ) throws Exception {
               InitialContext rootCtx = new InitialContext ();
               Name fullName = rootCtx.getNameParser ( "" ).parse ( jndiName );
               NonSerializableFactory.rebind ( fullName, object, true);
               }
              
               private void unbind ( String jndiName ) throws Exception {
               InitialContext rootCtx = new InitialContext();
               rootCtx.unbind ( jndiName );
               NonSerializableFactory.unbind(jndiName);
               }
              
              }
              


              Then you must define a .sar and put it into the deploy dir or into your .ear.
              Let's supouse that you define a services.sar into your deploy dir, then you must create a jboss-service.xml into the services.sar directory (or file).

              The structure of the directory will be the following:

              deploy/
               /services.sar
               /META-INF
               jboss-service.xml
              


              So, in the jboss-service.xml you must put the MBean declaration.

              <?xml version="1.0" encoding="UTF-8"?>
              
              <server>
              
              <mbean code="some.package.XMLDataLoader"
               name="services:service=XMLDataLoader">
               <attribute name="JndiName">services/XMLData</attribute>
              </mbean>
              
              </server>
              


              When Jboss starts will call your services.

              Then you can invoke your XMLData class from your EJBs with the jndi name that you define.

              Therion

              • 4. Re: how to register a object to JNDI namespace..?
                Jay Choi Newbie

                Great... Therion..

                I love your step-by-step instruction..

                Thanks in Million..

                • 5. Re: how to register a object to JNDI namespace..?
                  Jay Choi Newbie

                  I just followed what Therion suggested and it all works fine and everything but there is one problem..

                  when I start jboss, I got the following (obviously) error since the mbean service starts up way before my .ear file is deployed..

                  I guess I can extract any essential files out of my .ear and jar it separately and put it into lib directory to make it work but is there better way to handle this..?

                  Thanks in million..!!!!


                  10:37:19,256 ERROR [MainDeployer] could not create deployment: file:/C:/Dev/jboss/server/default/deploy/services.sar/
                  org.jboss.deployment.DeploymentException: create operation failed for package file:/C:/Dev/jboss/server/default/deploy/services.sar/; - nested throwable: (org.jboss.deployment.DeploymentException: No ClassLoaders found for: com.etn.util.xmlloader.XML
                  DataLoader; - nested throwable: (java.lang.ClassNotFoundException: No ClassLoaders found for: com.etn.util.xmlloader.XMLDataLoader))
                   at org.jboss.deployment.SARDeployer.create(SARDeployer.java:227)
                   at org.jboss.deployment.MainDeployer.create(MainDeployer.java:790)
                   at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:644)
                   at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:608)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:324)
                   at org.jboss.mx.server.ReflectedDispatcher.dispatch(ReflectedDispatcher.java:60)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:61)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:53)
                   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
                   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:185)
                   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:473)
                   at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:176)
                   at $Proxy7.deploy(Unknown Source)
                   at org.jboss.deployment.scanner.URLDeploymentScanner.deploy(URLDeploymentScanner.java:304)
                   at org.jboss.deployment.scanner.URLDeploymentScanner.scan(URLDeploymentScanner.java:478)
                   at org.jboss.deployment.scanner.AbstractDeploymentScanner$ScannerThread.doScan(AbstractDeploymentScanner.java:201)
                   at org.jboss.deployment.scanner.AbstractDeploymentScanner.startService(AbstractDeploymentScanner.java:274)
                   at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanSupport.java:271)
                   at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMBeanSupport.java:221)
                   at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:324)
                   at org.jboss.mx.server.ReflectedDispatcher.dispatch(ReflectedDispatcher.java:60)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:61)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:53)
                   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
                   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:185)
                   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:473)
                   at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceController.java:837)
                   at $Proxy0.start(Unknown Source)
                   at org.jboss.system.ServiceController.start(ServiceController.java:367)
                   at sun.reflect.GeneratedMethodAccessor6.invoke(Unknown Source)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:324)
                   at org.jboss.mx.server.ReflectedDispatcher.dispatch(ReflectedDispatcher.java:60)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:61)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:53)
                   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
                   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:185)
                   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:473)
                   at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:176)
                   at $Proxy4.start(Unknown Source)
                   at org.jboss.deployment.SARDeployer.start(SARDeployer.java:251)
                   at org.jboss.deployment.MainDeployer.start(MainDeployer.java:836)
                   at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:645)
                   at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:608)
                   at org.jboss.deployment.MainDeployer.deploy(MainDeployer.java:592)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:324)
                   at org.jboss.mx.server.ReflectedDispatcher.dispatch(ReflectedDispatcher.java:60)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:61)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:53)
                   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
                   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:185)
                   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:473)
                   at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:176)
                   at $Proxy5.deploy(Unknown Source)
                   at org.jboss.system.server.ServerImpl.doStart(ServerImpl.java:407)
                   at org.jboss.system.server.ServerImpl.start(ServerImpl.java:311)
                   at org.jboss.Main.boot(Main.java:145)
                   at org.jboss.Main$1.run(Main.java:399)
                   at java.lang.Thread.run(Thread.java:534)
                  Caused by: org.jboss.deployment.DeploymentException: No ClassLoaders found for: com.etn.util.xmlloader.XMLDataLoader; - nested throwable: (java.lang.ClassNotFoundException: No ClassLoaders found for: com.etn.util.xmlloader.XMLDataLoader)
                   at org.jboss.system.ServiceConfigurator.install(ServiceConfigurator.java:139)
                   at org.jboss.system.ServiceController.install(ServiceController.java:198)
                   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                   at java.lang.reflect.Method.invoke(Method.java:324)
                   at org.jboss.mx.server.ReflectedDispatcher.dispatch(ReflectedDispatcher.java:60)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:61)
                   at org.jboss.mx.server.Invocation.dispatch(Invocation.java:53)
                   at org.jboss.mx.server.Invocation.invoke(Invocation.java:86)
                   at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.java:185)
                   at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:473)
                   at org.jboss.mx.util.MBeanProxyExt.invoke(MBeanProxyExt.java:176)
                   at $Proxy4.install(Unknown Source)
                   at org.jboss.deployment.SARDeployer.create(SARDeployer.java:204)
                   ... 66 more
                  Caused by: java.lang.ClassNotFoundException: No ClassLoaders found for: com.etn.util.xmlloader.XMLDataLoader
                   at org.jboss.mx.loading.LoadMgr3.beginLoadTask(LoadMgr3.java:185)
                   at org.jboss.mx.loading.UnifiedClassLoader3.loadClassImpl(UnifiedClassLoader3.java:178)
                   at org.jboss.mx.loading.UnifiedClassLoader3.loadClass(UnifiedClassLoader3.java:132)
                   at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
                   at org.jboss.mx.server.MBeanServerImpl.instantiate(MBeanServerImpl.java:843)
                   at org.jboss.mx.server.MBeanServerImpl.instantiate(MBeanServerImpl.java:290)
                   at org.jboss.mx.server.MBeanServerImpl.createMBean(MBeanServerImpl.java:317)
                   at org.jboss.system.ServiceCreator.install(ServiceCreator.java:98)
                   at org.jboss.system.ServiceConfigurator.internalInstall(ServiceConfigurator.java:149)
                   at org.jboss.system.ServiceConfigurator.install(ServiceConfigurator.java:114)
                   ... 80 more
                  


                  • 6. Re: how to register a object to JNDI namespace..?
                    colaheavy Newbie

                    shacka,

                    an .ear should contain everything an j2ee enterprise application needs. Due to the class loader hierarchy used by jboss in the ear deployer there is something different as if you would deploy ajr's, sar's, etc. separetly. Due to j2ee spec concerning ear packaging you should put the .sar into the .ear and modify the application.xml accordingly. Also put any jboss specific deploy descriptors in the META-INF directory in each archive contained in the .ear.