users@glassfish.java.net

GF not generating stubs on demand for EJB 3.0, ok for EJB 2.x

From: <glassfish_at_javadesktop.org>
Date: Wed, 19 May 2010 10:02:19 PDT

Hello,

i'm trying to access an EJB 3.0 deployed on SJSAS 9.1_02 (equivalent to GF 2) from a standalone java main class using the CosNaming initial context provider (CNCtxFactory), so not GF's initial context.

I suppose i need the stubs for the EJB 3.0 , as we do in the case of EJB 2.x using CosNaming initial context.

But GF does not seem to generate the stubs for the EJB 3 as it does for EJb 2 with neither of these commands:

1. asadmin deploy --generatermistubs --retrieve /home/user1/clientstubdir fooapp.ear (as indicated here: https://glassfish.dev.java.net/javaee5/ejb/EJB_FAQ.html#CosNaming)

2. asadmin get-client-stubs ...etc. (as indicated in 819-3672 Developper's Guide, page 214)

I also discoverd that for EJB 3 you must use a modified global JNDI name of the form:

context.lookup("Foo__3_x_Internal_RemoteBusinessHome__")

where Foo is the global JNDI name of the deployed EJB 3.

I discoverd this by inspecting the root naming context with code like:
****************************
                NamingEnumeration<Binding> bindings = context.listBindings("");
                while(bindings.hasMore()){
                        Binding b = bindings.next();
                        System.out.println(b.getName() + " : " + b.getClassName());
                }
****************************

which outputs things like this:


**********************
com.sun\.tutorial\.javaee\.dukesbank\.request\.AccountController__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
second__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
ejb : com.sun.jndi.cosnaming.CNCtx
roster.request\.Request__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
com.sun\.tutorial\.javaee\.dukesbank\.request\.CustomerController__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
SerialContextProvider : com.sun.corba.se.impl.corba.CORBAObjectImpl
com.sun\.tutorial\.javaee\.dukesbank\.request\.TxController__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
some : com.hello.website.dbaccess.ejbs.deprecated._ISomeBeanHome_Stub
order.request\.Request__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
**********************


So while with GF's intial context you can use:

context.lookup("Foo"),

with CosNaming you must use

context.lookup("Foo__3_x_Internal_RemoteBusinessHome__")



Thanks for your help on this.



Here's my main class:





package com.hello.standalone.access;

import java.rmi.RemoteException;
import java.util.Hashtable;

import javax.naming.Binding;
import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.hello.website.dbaccess.ejbs.ISecondRemote;
import com.hello.website.dbaccess.ejbs.deprecated.ISomeBeanHome;
import com.hello.website.dbaccess.ejbs.deprecated.ISomeBeanRemote;

public class RemoteCaller {

        /**
         * @param args
         * @throws NamingException
         * @throws RemoteException
         */
        public static void main(String[] args) throws NamingException, RemoteException {
                
                Hashtable<String, String> props=new Hashtable<String, String>();
                
                props.put(javax.naming.InitialContext.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
                //props.put(javax.naming.InitialContext.URL_PKG_PREFIXES, "com.sun.jndi.cosnaming");
                props.put(javax.naming.InitialContext.PROVIDER_URL, "iiop://192.168.0.10:3700");
                
                InitialContext context = new InitialContext(props);
                
                System.out.println(context.getEnvironment());
                NamingEnumeration<Binding> bindings = context.listBindings("");
                while(bindings.hasMore()){
                        Binding b = bindings.next();
                        System.out.println(b.getName() + " : " + b.getClassName());
                }

                //SomeBean is an EJB 2.x, with "some" as the global JNDI name, i have the stubs in the classpath, works fine!
                ISomeBeanHome someBeanHome = (ISomeBeanHome)PortableRemoteObject.narrow(context.lookup("some"), ISomeBeanHome.class);
                ISomeBeanRemote someBean = someBeanHome.create();
                
                System.out.println(someBean.hello());
                
                //Second is an EJB 3.0, with "second" as the global JNDI name, i don't have the stubs, i'm getting java.lang.ClassCastException
                //as i see in debug context.lookup("second__3_x_Internal_RemoteBusinessHome__") is SUCCESSFUL
                
                ISecondRemote secondBean =
                        //(ISecondRemote)context.lookup("com.hello.website.dbaccess.ejbs.ISecondRemote");
                        (ISecondRemote)PortableRemoteObject.narrow(context.lookup("second__3_x_Internal_RemoteBusinessHome__"), ISecondRemote.class);
                        //(ISecondRemote)PortableRemoteObject.narrow(context.lookup("com.hello.website.dbaccess.ejbs.ISecondRemote"), ISecondRemote.class);
                
                System.out.println(secondBean.hello());
        }

}



AND THE OUTPUT





{java.naming.provider.url=iiop://192.168.0.10:3700, java.naming.factory.initial=com.sun.jndi.cosnaming.CNCtxFactory}
com.sun\.tutorial\.javaee\.dukesbank\.request\.AccountController__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
second__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
ejb : com.sun.jndi.cosnaming.CNCtx
roster.request\.Request__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
com.sun\.tutorial\.javaee\.dukesbank\.request\.CustomerController__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
SerialContextProvider : com.sun.corba.se.impl.corba.CORBAObjectImpl
com.sun\.tutorial\.javaee\.dukesbank\.request\.TxController__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
some : com.hello.website.dbaccess.ejbs.deprecated._ISomeBeanHome_Stub
order.request\.Request__3_x_Internal_RemoteBusinessHome__ : com.sun.corba.se.impl.corba.CORBAObjectImpl
Hello World
Exception in thread "main" java.lang.ClassCastException
        at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:229)
        at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137)
        at com.hello.standalone.access.RemoteCaller.main(RemoteCaller.java:48)
Caused by: java.lang.ClassCastException: Object is not of remote type com.hello.website.dbaccess.ejbs.ISecondRemote
        at com.sun.corba.se.impl.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:221)
        ... 2 more
[Message sent by forum member 'vladbalan']

http://forums.java.net/jive/thread.jspa?messageID=470511