2 Replies Latest reply: Jul 20, 2012 11:18 AM by murali veligeti - oracle RSS

    injecting_jndi_datasources

    801933
      I know in standalone client cannot access java:comp/env which is used in my server application from my previous post, so I try another method as follow to test the server method
      public void getEnvironment() throws Exception{
           try{
                System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
                /*System.setProperty(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
                System.setProperty(Context.SECURITY_PRINCIPAL, "weblogic");
                System.setProperty(Context.SECURITY_CREDENTIALS, "password");*/
                InitialContext ic = new InitialContext();
                ic.createSubcontext("java:");
                ic.createSubcontext("java:/comp");
                ic.createSubcontext("java:/comp/env");
                ic.createSubcontext("java:/comp/env/jdbc");               
                // Construcvt Datasource
                OracleConnectionPoolDataSource ds = new OracleConnectionPoolDataSource();          
                ds.setURL("jdbc:oracle:thin:@cdcehr29:14985");
                ds.setUser("als_app_ro_user");
                ds.setPassword("ai1pass");
                //Connection conn = ds.getConnection();
                ic.bind("java:/comp/env/jdbc/test", ds);
                System.out.println("see?");
                Context envIc = (Context) ic.lookup("java:/comp/env");
                envIc.bind("THIS_SERVICE_CD", "");
                envIc.bind("THIS_SERVICE_VER", "");
                envIc.bind("SERVICE_DATA_SOURCE_JNDI", "jdbc/test");
                envIc.bind("ALS_PROJECT_CD", "");
                envIc.bind("ALS_SYSTEM_CD", "");
                envIc.bind("EHR_CONFIG_DATA_SOURCE_JNDI", "");
                envIc.bind("SGR_PATH", "");
                envIc.bind("AUDIT_TABLE_NAME", "");
                envIc.bind("DEBUG_TABLE_NAME", "");
                InitialContext ic2 = new InitialContext();
                Context ctx = (Context)ic2.lookup("java:/comp/env");  // error
                //Context ctx = (Context)ic2.lookup("java:comp/env");
                System.out.println("see2?");
           }catch(NamingException ex){
                ex.printStackTrace();
           }catch(Exception ex){
                ex.printStackTrace();
           }
      }
      but when the code run to
      Context ctx = (Context)ic2.lookup("java:/comp/env"); // error

      error as following is thrown
      >
      javax.naming.NameNotFoundException: remaining name: /comp/env
           at weblogic.j2eeclient.SimpleContext.resolve(SimpleContext.java:35)
           at weblogic.j2eeclient.SimpleContext.resolve(SimpleContext.java:39)
           at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:59)
           at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:64)
           at weblogic.jndi.factories.java.ReadOnlyContextWrapper.lookup(ReadOnlyContextWrapper.java:45)
           at weblogic.jndi.internal.AbstractURLContext.lookup(AbstractURLContext.java:130)
           at javax.naming.InitialContext.lookup(InitialContext.java:392)
           at jdbc.test.ConnectOracle.getEnvironment(ConnectOracle.java:84)
           at jdbc.test.ConnectOracle.main(ConnectOracle.java:178)


      what is the reason?
      I get this inspiration from a website [https://blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit|https://blogs.oracle.com/randystuph/entry/injecting_jndi_datasources_for_junit]
      which mention
      >
      Context initContext = new InitialContext();
      Context webContext = (Context)initContext.lookup("java:/comp/env");

      after create the initialcontext in @BeforeClass
        • 1. Re: injecting_jndi_datasources
          801933
          seem the problem related to
          System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
          System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
          because the following has no error when create the second InitialContext() and then called
          Context webContext = (Context) initContext.lookup("java:/comp/env");
          public void getEnvironment() throws Exception{
              try {
                      System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
                      System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");
                         /* if using these, will throw error
                          * createSubcontext not allowed in a ReadOnlyContext;remaining name ''
                          * when call initialContext.createSubcontext("java:/comp");
                       System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
                       System.setProperty(Context.URL_PKG_PREFIXES, "weblogic.jndi.factories:weblogic.corba.j2ee.naming.url");
                       */
                      InitialContext initialContext = new InitialContext();
                      initialContext.createSubcontext("java:");
                      initialContext.createSubcontext("java:/comp");
                      initialContext.createSubcontext("java:/comp/env");
                      initialContext.createSubcontext("java:/comp/env/jdbc");
                      Context initContext = new InitialContext();
                      Context webContext = (Context) initContext.lookup("java:/comp/env");
                      //DataSource dataSource = (DataSource) webContext.lookup("jdbc/mydatasourcename1");
                      System.out.println("see");
                  }
                  catch (NamingException ne) {
                      System.err.println("Problem with the jndi name.");
                      ne.printStackTrace();
                  }
          }
          But the detail reason is still unknown, anyone could help?
          • 2. Re: injecting_jndi_datasources
            murali veligeti - oracle
            Hi,

            Please try with below code when connecting to weblogic server:

            Hashtable h = new Hashtable();
            h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
            h.put(Context.PROVIDER_URL, "t3://localhost:7001");
            h.put(Context.SECURITY_PRINCIPAL, pUsername);
            h.put(Context.SECURITY_CREDENTIALS, pPassword);

            InitialContext context = new InitialContext(h);
            context.createSubcontext("java:/comp");


            The below code is not connecting to weblogic.

            System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
            System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming")


            Thanks & Regards,
            Murali.
            ============