5 Replies Latest reply on Mar 6, 2003 9:49 AM by peterdnight

    How to use jboss jndi from a java application

    peterdnight

      Hi,

      I defined a mysql-service.xml file, and can access it from within the jboss servlet container via a JSP. Same code placed in an application program (classpath set to pick up jnet.jar and jbossall-client.jar) yields a naming exception every time. The pertinent code is shown below.

      Does ANYBODY know why I can get a JNDI reference inside of the JBOSS JVM, but not outside....I am very suspicious of the login-config code... what the heck is the principal attribute? I guessed that it was DB name, but I am thinking it has to do with security....

      Thanks,
      Peter

      ---> login-config.xml snip...
      <application-policy name = "MySqlDbRealm">

      <login-module code = "org.jboss.resource.security.ConfiguredIdentityLoginModule" flag = "required">
      <module-option name = "principal">SmalleB</module-option>
      <module-option name = "userName">myUser</module-option>
      <module-option name = "password">myPass</module-option>
      <module-option name = "managedConnectionFactoryName">jboss.jca:service=LocalTxCM,name=PeterSqlDS</module-option>
      </login-module>

      </application-policy>

      ====> Lookup code works in JSP, fails in APP...
      InitialContext ic = new InitialContext();
      System.out.println("Got an InitialContext");

      DataSource ds = (DataSource) ic.lookup("java:/jdbc/smallb") ;
      System.out.println("Got an datasource");

        • 1. Re: How to use jboss jndi from a java application
          joelvogt

          if you are outside JBoss' JVM then ic = new InitialContext() will not do the job. Set a hashtable of properties, in particular the jndi provider when you construct your context. You can also provide a jndi.properties to your app. There are several examples throughout the forums or faqs.

          • 2. Re: How to use jboss jndi from a java application
            tbfmicke


            >
            > DataSource ds = (DataSource)
            > ic.lookup("java:/jdbc/smallb") ;
            > System.out.println("Got an datasource");

            I not looked on the other parts, but regardless of
            how well they work the above will not work outside of the JBoss Server. Anything below the java: namespace is anly acceccible inside JBoss. (In particular, this means that you cannot access DataSources from outside since they always will be in the java: namespace, which differs from for example weblogic).

            If you are only testing JNDI access then you should try to look up an EJB or someting. If you want a DataSource I guess you are out of luck :-)

            Regards.
            /Mikael

            • 3. Re: How to use jboss jndi from a java application
              peterdnight

              Many thanks for the suggestion, I apologize for not mentioning that i do have a jndi.properties (Shown below)

              It is being read successfully because if I change the port to 1098, I get the expected exception thrown..

              Other guesses;) I did grab the 3.0.6 source last night and took a look at the test code in the jndi package..I copied the showTree code. I confirmed that NONE of the java: context is visible, including my datasource... the output is below the properties...
              Help;)
              Thanks,
              Peter

              java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
              java.naming.provider.url=jnp://localhost/1099
              java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
              # The jnp protocol socket factory class
              jnp.socketFactory=org.jnp.interfaces.TimedSocketFactory
              # The TimedSocketFactory connection timeout in milliseconds(0 == blocking)
              jnp.timeout=0
              # The TimedSocketFactory read timeout in milliseconds(0 == blocking)
              jnp.sotimeout=0

              =====> myTree dump showing nothing from the java: context
              ----------------------------
              /XAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
              /RMIXAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
              /UserTransactionSessionFactory: org.jboss.tm.usertx.server.UserTransactionSessionFactoryImpl
              /topic: org.jnp.interfaces.NamingContext
              /topic/testDurableTopic: org.jboss.mq.SpyTopic
              /topic/testTopic: org.jboss.mq.SpyTopic
              /topic/securedTopic: org.jboss.mq.SpyTopic
              /queue: org.jnp.interfaces.NamingContext
              /queue/A: org.jboss.mq.SpyQueue
              /queue/testQueue: org.jboss.mq.SpyQueue
              /queue/ex: org.jboss.mq.SpyQueue
              /queue/DLQ: org.jboss.mq.SpyQueue
              /queue/D: org.jboss.mq.SpyQueue
              /queue/C: org.jboss.mq.SpyQueue
              /queue/B: org.jboss.mq.SpyQueue
              /ConnectionFactory: org.jboss.mq.SpyConnectionFactory
              /RMIConnectionFactory: org.jboss.mq.SpyConnectionFactory
              /UserTransaction: org.jboss.tm.usertx.client.ClientUserTransaction
              /ejb: org.jnp.interfaces.NamingContext
              /ejb/mgmt: org.jnp.interfaces.NamingContext
              /ejb/mgmt/MEJB: $Proxy15
              /ejb/jmx: org.jnp.interfaces.NamingContext
              /ejb/jmx/ejb: org.jnp.interfaces.NamingContext
              /ejb/jmx/ejb/Adaptor: $Proxy19
              /invokers: org.jnp.interfaces.NamingContext
              /invokers/amd2100: org.jnp.interfaces.NamingContext
              /invokers/amd2100/pooled: org.jboss.invocation.pooled.interfaces.PooledInvokerProxy
              /invokers/amd2100/jrmp: org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy
              /invokers/amd2100/http: org.jboss.invocation.http.interfaces.HttpInvokerProxy
              /jmx: org.jnp.interfaces.NamingContext
              /jmx/rmi: org.jnp.interfaces.NamingContext
              /jmx/rmi/RMIAdaptor: org.jboss.jmx.adaptor.rmi.RMIAdaptorImpl
              /UILXAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
              /jmx:amd2100:rmi: org.jboss.jmx.adaptor.rmi.RMIAdaptorImpl
              /UILConnectionFactory: org.jboss.mq.SpyConnectionFactory
              ----------------------------
              Got an InitialContext: org.jnp.interfaces.NamingContext@364435
              test created:org.jnp.interfaces.NamingContext@5212bc
              Hello1
              Hello2
              test/server bound
              test/server2 bound
              test/server2 lookup:Hello2
              jnp://localhost/test/server2 lookup:Hello2
              Got a NamingException: javax.naming.NameNotFoundException: jdbc not bound

              • 4. Re: How to use jboss jndi from a java application
                peterdnight

                Many thanks for the suggestion, I apologize for not mentioning that i do have a jndi.properties (Shown below)

                It is being read successfully because if I change the port to 1098, I get the expected exception thrown..

                Other guesses;) I did grab the 3.0.6 source last night and took a look at the test code in the jndi package..I copied the showTree code. I confirmed that NONE of the java: context is visible, including my datasource... the output is below the properties...
                Help;)
                Thanks,
                Peter

                java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                java.naming.provider.url=jnp://localhost/1099
                java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
                # The jnp protocol socket factory class
                jnp.socketFactory=org.jnp.interfaces.TimedSocketFactory
                # The TimedSocketFactory connection timeout in milliseconds(0 == blocking)
                jnp.timeout=0
                # The TimedSocketFactory read timeout in milliseconds(0 == blocking)
                jnp.sotimeout=0

                =====> myTree dump showing nothing from the java: context
                ----------------------------
                /XAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
                /RMIXAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
                /UserTransactionSessionFactory: org.jboss.tm.usertx.server.UserTransactionSessionFactoryImpl
                /topic: org.jnp.interfaces.NamingContext
                /topic/testDurableTopic: org.jboss.mq.SpyTopic
                /topic/testTopic: org.jboss.mq.SpyTopic
                /topic/securedTopic: org.jboss.mq.SpyTopic
                /queue: org.jnp.interfaces.NamingContext
                /queue/A: org.jboss.mq.SpyQueue
                /queue/testQueue: org.jboss.mq.SpyQueue
                /queue/ex: org.jboss.mq.SpyQueue
                /queue/DLQ: org.jboss.mq.SpyQueue
                /queue/D: org.jboss.mq.SpyQueue
                /queue/C: org.jboss.mq.SpyQueue
                /queue/B: org.jboss.mq.SpyQueue
                /ConnectionFactory: org.jboss.mq.SpyConnectionFactory
                /RMIConnectionFactory: org.jboss.mq.SpyConnectionFactory
                /UserTransaction: org.jboss.tm.usertx.client.ClientUserTransaction
                /ejb: org.jnp.interfaces.NamingContext
                /ejb/mgmt: org.jnp.interfaces.NamingContext
                /ejb/mgmt/MEJB: $Proxy15
                /ejb/jmx: org.jnp.interfaces.NamingContext
                /ejb/jmx/ejb: org.jnp.interfaces.NamingContext
                /ejb/jmx/ejb/Adaptor: $Proxy19
                /invokers: org.jnp.interfaces.NamingContext
                /invokers/amd2100: org.jnp.interfaces.NamingContext
                /invokers/amd2100/pooled: org.jboss.invocation.pooled.interfaces.PooledInvokerProxy
                /invokers/amd2100/jrmp: org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy
                /invokers/amd2100/http: org.jboss.invocation.http.interfaces.HttpInvokerProxy
                /jmx: org.jnp.interfaces.NamingContext
                /jmx/rmi: org.jnp.interfaces.NamingContext
                /jmx/rmi/RMIAdaptor: org.jboss.jmx.adaptor.rmi.RMIAdaptorImpl
                /UILXAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
                /jmx:amd2100:rmi: org.jboss.jmx.adaptor.rmi.RMIAdaptorImpl
                /UILConnectionFactory: org.jboss.mq.SpyConnectionFactory
                ----------------------------
                Got an InitialContext: org.jnp.interfaces.NamingContext@364435
                test created:org.jnp.interfaces.NamingContext@5212bc
                Hello1
                Hello2
                test/server bound
                test/server2 bound
                test/server2 lookup:Hello2
                jnp://localhost/test/server2 lookup:Hello2
                Got a NamingException: javax.naming.NameNotFoundException: jdbc not bound

                • 5. Re: How to use jboss jndi from a java application
                  peterdnight

                  Many thanks for the suggestion, I apologize for not mentioning that i do have a jndi.properties (Shown below)

                  It is being read successfully because if I change the port to 1098, I get the expected exception thrown..

                  Other guesses;) I did grab the 3.0.6 source last night and took a look at the test code in the jndi package..I copied the showTree code. I confirmed that NONE of the java: context is visible, including my datasource.. or even DefaultDS... the output is below the properties...
                  Help;)
                  Thanks,
                  Peter

                  java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
                  java.naming.provider.url=jnp://localhost/1099
                  java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
                  # The jnp protocol socket factory class
                  jnp.socketFactory=org.jnp.interfaces.TimedSocketFactory
                  # The TimedSocketFactory connection timeout in milliseconds(0 == blocking)
                  jnp.timeout=0
                  # The TimedSocketFactory read timeout in milliseconds(0 == blocking)
                  jnp.sotimeout=0

                  =====> myTree dump showing nothing from the java: context
                  ----------------------------
                  /XAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
                  /RMIXAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
                  /UserTransactionSessionFactory: org.jboss.tm.usertx.server.UserTransactionSessionFactoryImpl
                  /topic: org.jnp.interfaces.NamingContext
                  /topic/testDurableTopic: org.jboss.mq.SpyTopic
                  /topic/testTopic: org.jboss.mq.SpyTopic
                  /topic/securedTopic: org.jboss.mq.SpyTopic
                  /queue: org.jnp.interfaces.NamingContext
                  /queue/A: org.jboss.mq.SpyQueue
                  /queue/testQueue: org.jboss.mq.SpyQueue
                  /queue/ex: org.jboss.mq.SpyQueue
                  /queue/DLQ: org.jboss.mq.SpyQueue
                  /queue/D: org.jboss.mq.SpyQueue
                  /queue/C: org.jboss.mq.SpyQueue
                  /queue/B: org.jboss.mq.SpyQueue
                  /ConnectionFactory: org.jboss.mq.SpyConnectionFactory
                  /RMIConnectionFactory: org.jboss.mq.SpyConnectionFactory
                  /UserTransaction: org.jboss.tm.usertx.client.ClientUserTransaction
                  /ejb: org.jnp.interfaces.NamingContext
                  /ejb/mgmt: org.jnp.interfaces.NamingContext
                  /ejb/mgmt/MEJB: $Proxy15
                  /ejb/jmx: org.jnp.interfaces.NamingContext
                  /ejb/jmx/ejb: org.jnp.interfaces.NamingContext
                  /ejb/jmx/ejb/Adaptor: $Proxy19
                  /invokers: org.jnp.interfaces.NamingContext
                  /invokers/amd2100: org.jnp.interfaces.NamingContext
                  /invokers/amd2100/pooled: org.jboss.invocation.pooled.interfaces.PooledInvokerProxy
                  /invokers/amd2100/jrmp: org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy
                  /invokers/amd2100/http: org.jboss.invocation.http.interfaces.HttpInvokerProxy
                  /jmx: org.jnp.interfaces.NamingContext
                  /jmx/rmi: org.jnp.interfaces.NamingContext
                  /jmx/rmi/RMIAdaptor: org.jboss.jmx.adaptor.rmi.RMIAdaptorImpl
                  /UILXAConnectionFactory: org.jboss.mq.SpyXAConnectionFactory
                  /jmx:amd2100:rmi: org.jboss.jmx.adaptor.rmi.RMIAdaptorImpl
                  /UILConnectionFactory: org.jboss.mq.SpyConnectionFactory
                  ----------------------------
                  Got an InitialContext: org.jnp.interfaces.NamingContext@364435
                  test created:org.jnp.interfaces.NamingContext@5212bc
                  Hello1
                  Hello2
                  test/server bound
                  test/server2 bound
                  test/server2 lookup:Hello2
                  jnp://localhost/test/server2 lookup:Hello2
                  Got a NamingException: javax.naming.NameNotFoundException: jdbc not bound