JNDI Lookup - works standalone, doesn't work inside websphere - Websphere

This is a discussion on JNDI Lookup - works standalone, doesn't work inside websphere - Websphere ; Hi All, I am trying out a simple program using JNDI which works fine when I run it as a standalone program, but when I run the same inside websphere version 6.1, I get a NamingContextPackage not found exception. Here ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: JNDI Lookup - works standalone, doesn't work inside websphere

  1. JNDI Lookup - works standalone, doesn't work inside websphere

    Hi All,
    I am trying out a simple program using JNDI which works fine when I run it as a standalone program, but when I run the same inside websphere version 6.1, I get a NamingContextPackage not found exception.

    Here is my Standalone program:

    public class TestLDAP {
    final static String ldapServerName = "myHost:389";
    final static String rootdn = "cn=Manager,dc=abc,dc=com";
    final static String rootpass = "secret";
    final static String rootContext = "dc=abc,dc=com";


    public static void main( String[] args ) throws Exception {
    Properties env = new Properties();

    env.put( Context.INITIAL_CONTEXT_FACTORY,
    "com.sun.jndi.ldap.LdapCtxFactory" );
    env.put( Context.PROVIDER_URL, "ldap://" + ldapServerName + "/" + rootContext );
    env.put( Context.SECURITY_PRINCIPAL, rootdn );
    env.put( Context.SECURITY_CREDENTIALS, rootpass );

    try {
    // obtain initial directory context using the environment
    DirContext ctx = new InitialDirContext(env);

    OracleDataSource ods = new OracleDataSource();
    ods.setDriverType ( "thin" ); // type of driver
    ods.setServerName ( "myHost" ); // database server name
    ods.setNetworkProtocol("tcp"); // tcp is the default anyway
    ods.setDatabaseName("ORCL"); // Oracle SID
    ods.setPortNumber(1521); // listener port number
    ods.setUser("scott");
    ods.setPassword("tiger");
    //ctx.unbind("cn=myRandomInt");
    bindObjectInLDAP("cn=myRandomInt", ods, ctx);
    getObjectFromLDAP("cn=myRandomInt", ctx);

    } catch ( NameAlreadyBoundException nabe ) {
    System.err.println( "value has already been bound!" );
    } catch ( Exception e ) {
    System.err.println( e );
    }

    }

    private static void bindObjectInLDAP(String name, Object obj, DirContext ctx) throws Exception{
    System.out.println("Adding " + obj + " to directory..." );
    ctx.bind(name, obj);
    }

    private static void getObjectFromLDAP(String name, DirContext ctx) throws Exception {
    DataSource dsUsingJNDI = (DataSource) ctx.lookup(name);
    System.out.println("Retreived: " + dsUsingJNDI);
    Connection connection = dsUsingJNDI.getConnection();
    System.out.println( "SUCCESS!" + connection);

    try {
    if( connection != null )
    connection.close();
    } catch (SQLException ignore ) {}
    }
    }



    Inside websphere, when I run the same code, I can create the Inital Context, but it fails at the part where I do:

    DataSource dsUsingJNDI = (DataSource) ctx.lookup(name);

    Here is the Exception I get:

    Could not lookup:
    javax.naming.NameNotFoundException: Context: ImpactCell/nodes/ImpactNode/servers/server1, name: cn=myRandomInt: First component in name cn=myRandomInt not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDLmg.org/CosNaming/NamingContext/NotFound:1.0]
    at com.ibm.ws.naming.jndicos.CNContextImpl.processNot FoundException(CNContextImpl.java:4730)
    at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(C NContextImpl.java:1907)
    at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(C NContextImpl.java:1862)
    at com.ibm.ws.naming.jndicos.CNContextImpl.lookupExt( CNContextImpl.java:1552)
    at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNC ontextImpl.java:1354)
    at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCt x.java:172)
    at javax.naming.InitialContext.lookup(InitialContext. java:363)

    Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDLmg.org/CosNaming/NamingContext/NotFound:1.0


    Is this because of a missing jar or probably websphere doesn't like "com.sun.jndi.ldap.LdapCtxFactory" or something else?
    Both my standalone program and websphere uses IBM JVM version 5.0

    Any clues on what could be causing this?

    Thanks in advance!

  2. Re: JNDI Lookup - works standalone, doesn't work inside websphere

    Maybe it won't even work in websphere. The namespace is inside OpenLDAP server running as a separate process. I am reading the websphere docs and it mentions this:

    A federated binding to a non-WebSphere naming context has the following functional limitations:

    *JNDI operations are restricted to the use of CORBA objects. For example, EJB homes can be looked up, but non-CORBA objects such as data sources cannot.*

  3. Re: JNDI Lookup - works standalone, doesn't work inside websphere

    Can you paste the code you use inside WAS?
    The exception:
    javax.naming.NameNotFoundException: Context: ImpactCell/nodes/ImpactNode/servers/server1, name:
    cn=myRandomInt: First component in name cn=myRandomInt not found. [Root exception is
    org.omg.CosNaming.NamingContextPackage.NotFound: IDLmg.org/CosNaming/NamingContext/NotFound:1.0]

    seems to indicate that you might be using the default WAS Context factory
    i.e. ctx = new InitialContext()

    instead of com.sun.jndi.ldap.LdapCtxFactory.

    i.e.
    Properties env = "suitably initialized"
    ctx = new InitialContext(env);


    Cheers,
    Vinod


    sunjit@gmail.com wrote:
    > Hi All,
    > I am trying out a simple program using JNDI which works fine when I run it as a standalone program, but when I run the same inside websphere version 6.1, I get a NamingContextPackage not found exception.
    >
    > Here is my Standalone program:
    >
    > public class TestLDAP {
    > final static String ldapServerName = "myHost:389";
    > final static String rootdn = "cn=Manager,dc=abc,dc=com";
    > final static String rootpass = "secret";
    > final static String rootContext = "dc=abc,dc=com";
    >
    >
    > public static void main( String[] args ) throws Exception {
    > Properties env = new Properties();
    >
    > env.put( Context.INITIAL_CONTEXT_FACTORY,
    > "com.sun.jndi.ldap.LdapCtxFactory" );
    > env.put( Context.PROVIDER_URL, "ldap://" + ldapServerName + "/" + rootContext );
    > env.put( Context.SECURITY_PRINCIPAL, rootdn );
    > env.put( Context.SECURITY_CREDENTIALS, rootpass );
    >
    > try {
    > // obtain initial directory context using the environment
    > DirContext ctx = new InitialDirContext(env);
    >
    > OracleDataSource ods = new OracleDataSource();
    > ods.setDriverType ( "thin" ); // type of driver
    > ods.setServerName ( "myHost" ); // database server name
    > ods.setNetworkProtocol("tcp"); // tcp is the default anyway
    > ods.setDatabaseName("ORCL"); // Oracle SID
    > ods.setPortNumber(1521); // listener port number
    > ods.setUser("scott");
    > ods.setPassword("tiger");
    > //ctx.unbind("cn=myRandomInt");
    > bindObjectInLDAP("cn=myRandomInt", ods, ctx);
    > getObjectFromLDAP("cn=myRandomInt", ctx);
    >
    > } catch ( NameAlreadyBoundException nabe ) {
    > System.err.println( "value has already been bound!" );
    > } catch ( Exception e ) {
    > System.err.println( e );
    > }
    >
    > }
    >
    > private static void bindObjectInLDAP(String name, Object obj, DirContext ctx) throws Exception{
    > System.out.println("Adding " + obj + " to directory..." );
    > ctx.bind(name, obj);
    > }
    >
    > private static void getObjectFromLDAP(String name, DirContext ctx) throws Exception {
    > DataSource dsUsingJNDI = (DataSource) ctx.lookup(name);
    > System.out.println("Retreived: " + dsUsingJNDI);
    > Connection connection = dsUsingJNDI.getConnection();
    > System.out.println( "SUCCESS!" + connection);
    >
    > try {
    > if( connection != null )
    > connection.close();
    > } catch (SQLException ignore ) {}
    > }
    > }
    >
    >
    >
    > Inside websphere, when I run the same code, I can create the Inital Context, but it fails at the part where I do:
    >
    > DataSource dsUsingJNDI = (DataSource) ctx.lookup(name);
    >
    > Here is the Exception I get:
    >
    > Could not lookup:
    > javax.naming.NameNotFoundException: Context: ImpactCell/nodes/ImpactNode/servers/server1, name: cn=myRandomInt: First component in name cn=myRandomInt not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDLmg.org/CosNaming/NamingContext/NotFound:1.0]
    > at com.ibm.ws.naming.jndicos.CNContextImpl.processNot FoundException(CNContextImpl.java:4730)
    > at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(C NContextImpl.java:1907)
    > at com.ibm.ws.naming.jndicos.CNContextImpl.doLookup(C NContextImpl.java:1862)
    > at com.ibm.ws.naming.jndicos.CNContextImpl.lookupExt( CNContextImpl.java:1552)
    > at com.ibm.ws.naming.jndicos.CNContextImpl.lookup(CNC ontextImpl.java:1354)
    > at com.ibm.ws.naming.util.WsnInitCtx.lookup(WsnInitCt x.java:172)
    > at javax.naming.InitialContext.lookup(InitialContext. java:363)
    >
    > Caused by: org.omg.CosNaming.NamingContextPackage.NotFound: IDLmg.org/CosNaming/NamingContext/NotFound:1.0
    >
    >
    > Is this because of a missing jar or probably websphere doesn't like "com.sun.jndi.ldap.LdapCtxFactory" or something else?
    > Both my standalone program and websphere uses IBM JVM version 5.0
    >
    > Any clues on what could be causing this?
    >
    > Thanks in advance!


+ Reply to Thread