This discussion is archived
5 Replies Latest reply: Jun 25, 2012 2:10 AM by RenévanWijk RSS

Setting Context Using WebLogic JNDI Environment Object

644604 Newbie
Currently Being Moderated
I was assigned a task to takeover and setup an application (EJB) running on Weblogic 10.3.3 with limited documentation. I need to create a few context values using JNDI environment. There are some context values which are already created and can be seen from Weblogic server JNDI tree using the Weblogic Server Admin Console. However, I have no idea how and where the context attributes and values are created. I did a thorough search through the source code but failed to find the context attributes/values that I see from the JNDI tree.

Any clue or hint will be much appreciated. Thank you.

Edited by: user641601 on Jun 25, 2012 1:08 PM
  • 1. Re: Setting Context Using WebLogic JNDI Environment Object
    RenévanWijk Oracle ACE
    Currently Being Moderated
    The JNDI resources in the JNDI are created when you actually create the resource. For example, when you create
    a data source you add the JNDI name as well. When the created resource is assigned to a specific server
    the JNDI tree will show the resource.

    WebLogic also creates its own resources that it uses, such as, for example ejb.mgmt.MEJB and javax.transaction.UserTransaction
    and a bunch of resources starting with weblogic.

    Usually, when you deploy an application for which you did not define a proper JNDI resource that it needs, an exception will
    be thrown telling you it cannot find the used JNDI name in the application.

    To find out which JNDI names the application it uses, you can check the persistence.xml (which contains information about the
    data source JNDI name). This file is located in the META-INF directory of the EJB jar file. Depending on the how additional
    resource are bound to the application you can either check ejb-jar.xml (also located in the META-INF directory) or you
    have to scan through the Java files and check if they used resource injection by using @Resource, there could be something like
    @Resource(name = "jms/ConnectionFactory")
    private ConnectionFactory connectionFactory;
    @Resource(name = "jms/CompanyQueue")
    private Queue destination;
    Of course once you find the JNDI names to be used, you have to figure out what resource should be coupled to them. This can
    be done based on the class types to which it is injected.

    When you have further questions do not be affraid to ask.
  • 2. Re: Setting Context Using WebLogic JNDI Environment Object
    644604 Newbie
    Currently Being Moderated
    Thanks for your reply. However, I have tried to search through all source files (java, xml, etc...) and jar files but I couldn't find the JNDI names that I see at the JNDI tree. Is it possible that the JNDI names/resources are allocated remotely? If yes, how can that be done?

    Here's some of the Java codes involved:

    String databaseHost = "" + context.lookup("DATABASE_HOST_NAME");
    String databasePort = "" + context.lookup("DATABASE_PORT");
    String dbVersion = "" + context.lookup("DB_VERSION");

    Here's the exception I got:

    [Exception Occurred]
    Naming Exception - javax.naming.NameNotFoundException: Unable to resolve 'DB_VERSION'.

    I've check the JNDI tree, the entry for "DATABASE_HOST_NAME" and "DATABASE_PORT" are there. "DB_VERSION" is not found in the JNDI tree. I did a thorough search in the source files for "DATABASE_HOST_NAME" and "DATABASE_PORT" to find out how and where are these JNDI values allocated so that I can do the same for "DB_VERSION". However, no match was found.

    Edited by: user641601 on Jun 25, 2012 3:20 PM
  • 3. Re: Setting Context Using WebLogic JNDI Environment Object
    RenévanWijk Oracle ACE
    Currently Being Moderated
    How are the resources for DATABASE_HOST_NAME defined in WebLogic?

    Or is this done in the Java program it self? Is there something like the following in the code?
        static {
            Hashtable environment = new Hashtable();
            environment.put(Context.INITIAL_CONTEXT_FACTORY, "");
            environment.put(Context.PROVIDER_URL, "");
            try {
                context = new InitialContext(environment);
                registerSomeObject();
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
    
        private static void registerSomeObject() {
            String databaseHostName = "my-database-host-name";
            try {
                context.rebind("DATABASE_HOST_NAME", databaseHostName);
            } catch (NamingException e) {
                e.printStackTrace();
            }
        }
    in other place the look-up
        public String getSomeObject() {
            String data = null;
            try {
                data = (String) getContext().lookup("DATABASE_HOST_NAME");
            } catch (NamingException e) {
                e.printStackTrace();
            }
            return data;
        }
  • 4. Re: Setting Context Using WebLogic JNDI Environment Object
    644604 Newbie
    Currently Being Moderated
    I have search through the Java codes for DATABASE_HOST_NAME but I only found the lookup, not the binding. I have no idea how is it defined in WebLogic. I was given the source codes and some dependencies (jar). I compiled, packaged and deployed the application to a newly installed WebLogic. I have even extracted the jar files and searched for DATABASE_HOST_NAME but still, no match found.

    Any clue? I've been stuck at this point for almost a week.
  • 5. Re: Setting Context Using WebLogic JNDI Environment Object
    RenévanWijk Oracle ACE
    Currently Being Moderated
    What I find strange is that the JNDI resource such as DATABASE_HOST_NAME show up in the WebLogic Server JNDI tree.
    You have to do something for this. Did you run a certain script in order to configure a WebLogic domain?

    What you also can try is comment out the line that is giving the error. It looks like it only sets the database version
    (String dbVersion = "" + context.lookup("DB_VERSION");) or even better set the version by hand and compile it again.

    I think I am running out of options as of what can be happening in the application.

    It is also very strange that the database resource is set-up as:
    String databaseHost = "" + context.lookup("DATABASE_HOST_NAME");
    String databasePort = "" + context.lookup("DATABASE_PORT");
    String dbVersion = "" + context.lookup("DB_VERSION");

    Normally you would configure a data source in WebLogic (http://docs.oracle.com/cd/E21764_01/apirefs.1111/e13952/taskhelp/jdbc/ConfigureDataSources.html)
    and use its JNDI name in the application. Looks like they are now managing the connections themselves.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points