3 Replies Latest reply on Jun 25, 2003 8:36 PM by adrian.brock

    Binding to JNDI

    scottpy

      Hello everyone!

      I have a fairly simple question. I am writting an n-tier application for a client. The client wants to have access to a configuration file that resides outside of the .ear. I have found a good place to put the properties file, but now, I want to load that file into JNDI so that my beans can use it. My solution has been to create a servlet that is essentially an init object. I want that servlet to create a Properties object and bind it in JNDI.

      I have tried the following, but this does not work. It does not bind my properties object. What am I doing wrong here?

      Thanks,
      -Scott

      [...]

      Properties jprops = new Properties();
      jprops.setProperty( "java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
      jprops.setProperty( "java.naming.provider.url","localhost:1099");
      jprops.setProperty( "java.naming.factory.url.pkgs","org.jboss.naming:org.jnp.interfaces");

      Properties props = new Properties();
      try {
      URL uconf = new URL(CONF_DIR+"bqconf.properties");
      props.load(uconf.openStream());
      }
      catch(MalformedURLException e) {
      System.out.println("Malformed URL!");
      e.printStackTrace();
      }
      catch(IOException e){
      System.out.println("No properties file present!");
      System.out.println("FILE: "+CONF_DIR+"bqconf.properties");
      }

      try {
      InitialContext context = new InitialContext(jprops);
      context.bind("java:mailproperties",props);
      }
      catch(NamingException e){
      e.printStackTrace();
      }

      try {
      InitialContext context = new InitialContext(jprops);
      context.lookup("java:mailerproperties");
      System.out.println("Properties Bound!");
      }
      catch(NamingException e){
      System.out.println("Naming Exception: "+e.getMessage());
      }

      [...]

        • 1. Re: Binding to JNDI

          The java: namespace is not available when
          you use "java.naming.provider.url"

          Anything you put there with this attribute set will
          be lost.

          Use the global namespace.

          Regards,
          Adrian

          • 2. Re: Binding to JNDI
            scottpy

            Ah! I understand. I have tried to the same piece of code without "java:" in the namespace, and I am still unable to bind this value to JNDI.

            Any more thoughts?

            Thanks!
            -Scott

            • 3. Re: Binding to JNDI

              This simple test works for me:

              InitialContext ctx = new InitialContext();
              ctx.bind("blah", new String("hello"));
              ctx = new InitialContext();
              System.out.println(ctx.lookup("blah"));

              My jndi.properties file contains:
              java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
              java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
              java.naming.provider.url=localhost

              Regards,
              Adrian