0

I'm trying to look up the PublicRepository class from an EJB on a Weblogic 10 server. This is the piece of code:

/**
     * RMI/IIOP clients should use this narrow function
     */
private static Object narrow(Object ref, Class c) {
    return PortableRemoteObject.narrow(ref, c);
}

/**
 * Lookup the EJBs home in the JNDI tree
 */
private static PublicRepository lookupHome() throws NamingException {
    // Lookup the beans home using JNDI
    Context ctx = getInitialContext();

    try {

        Object home = ctx.lookup("cea");
        return (PublicRepository) narrow(home, PublicRepository.class);

    } catch(NamingException ne) {
        System.out.println("The client was unable to lookup the EJBHome.  Please make sure ");
        System.out.println("that you have deployed the ejb with the JNDI name "
        + "cea" + " on the WebLogic server at " + "iiop://localhost:7001");
        throw ne;
    }
}


private static Context getInitialContext() throws NamingException {

    try {
        // Get an InitialContext
        Properties h = new Properties();
        h.put(Context.INITIAL_CONTEXT_FACTORY,
        "weblogic.jndi.WLInitialContextFactory");
        h.put(Context.PROVIDER_URL, "iiop://localhost:7001");
        return new InitialContext(h);

    } catch(NamingException ne) {
        System.out.println("We were unable to get a connection to the WebLogic server at " + "iiop://localhost:7001");
        System.out.println("Please make sure that the server is running.");
        throw ne;
    }
}

I'm however getting Cast Exception:

Exception in thread "main" java.lang.ClassCastException
    at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(Unknown Source)
    at javax.rmi.PortableRemoteObject.narrow(Unknown Source)
    at vrd.narrow(vrd.java:67)
    at vrd.lookupHome(vrd.java:80)
    at vrd.main(vrd.java:34)
Caused by: java.lang.ClassCastException: weblogic.corba.j2ee.naming.ContextImpl
    ... 5 more

Am I correct when I'm using the above code to retrive a certain class to be used in my client application? How could I get rid of the cast exception?

L4zl0w
  • 1,069
  • 4
  • 15
  • 42
  • Can you provide the full code for the class, along with the CLASSPATH of your client application? The package referenced in the exception looks suspicious. – kevinpowe Apr 14 '11 at 04:46
  • Wich version of EJB are you trying to lookup? Is it an EJB 3? EJB2? Also using weblogic 10.x you may consider using annotations to get references to EJBs. Could you please elaborate a bit more on your problem? – groo Apr 14 '11 at 20:11

3 Answers3

0

The simple thing to do would be to store the result of 'narrow' in a java.lang.Object and then see what type it is...

Jeff West
  • 1,563
  • 9
  • 11
  • Hmm I tried it and I still get cast exception!? Btw PublicRepository extends EJBObject type. – L4zl0w Apr 13 '11 at 18:55
  • I'm quite surprised you are getting a ClassCastException when you are trying to assign the result of a method to an 'Object'... – Jeff West Apr 14 '11 at 22:36
  • You should pass Object.class to narrow(), instead of PublicRepository.class. The actual ClassCastException is generated much inside narrow(), not when the method returns. But first, why don't you just print home.getClass()? – Blaisorblade Apr 16 '11 at 16:05
0

The error means you've looked up a Context rather than a bound object. In other words, you looked up "cea" instead of something like "cea/Bean". It's the analogous to using a FileInputStream on a directory.

Brett Kail
  • 33,593
  • 2
  • 85
  • 90
0

I was using the wrong JNDI name, hence it couldn't retrieve the object. Thanks everyone for looking.

L4zl0w
  • 1,069
  • 4
  • 15
  • 42