dcsimg
Runtime ejb retrieval
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mark_Gargan
Posted On:   Friday, May 10, 2002 04:57 AM

Hi there, I've a runtime problem. I don't know until runtime which ejb my client program wishes to lookup. Is there anyway of creating/retrieving the instance of a bean at runtime without having to use the instanceOf method? Basically for the code for retrieving a bean is there anyway that if you know the names of all the classes involved i.e. the bean class, home and remote interfaces, can you create/retrieve the bean. < String MyEjbBindName = args[0]; String MyEjbHomeName = args[1]; String MyEjbRemoteName = args[2]; Object ref = initialContext.lookup(MyEjbBindName); MyBeanHome home = (MyBeanHome)PortableREmoteObject.narrow(ref,MyBeanHome.class); MyBeanRemote remote = home   More>>

Hi there,
I've a runtime problem. I don't know until runtime which ejb my client program wishes to lookup. Is there anyway of creating/retrieving the instance of a bean at runtime without having to use the

instanceOf

method? Basically for the code for retrieving a bean is there anyway that if you know the names of all the classes involved i.e. the bean class, home and remote interfaces, can you create/retrieve the bean.

<
			
String MyEjbBindName = args[0];
String MyEjbHomeName = args[1];
String MyEjbRemoteName = args[2];

Object ref = initialContext.lookup(MyEjbBindName);
MyBeanHome home = (MyBeanHome)PortableREmoteObject.narrow(ref,MyBeanHome.class);

MyBeanRemote remote = home.create();


This would be doable I think if there's some way of runtime casting e.g.

			
Object obj = new Integer(2);
Class castClass = Class.forName("java.lang.Integer");
Integer x = (castClass)obj;
System.out.println(x.intValue());


however I don't think this is possible.
Any ideas would be greatly appreciated.


Thanks,
Mark.    <<Less

Re: Runtime ejb retrieval

Posted By:   Bozidar_Dangubic  
Posted On:   Friday, May 10, 2002 05:50 AM

well, not really. you really cannot do it. if you know the name of the bean, its remote and home interface it won't help you to do it at runtime because you need to narrow() and not cast. so you code needs to know in advance what bean you are looking up because when you narrow you cannot specify a name of the remote interface in the parenthesis as a String object. I mean, this won't work

String remoteInterfaceClass = "com.company.MyRemoteInterface";
String beanName = "MyRemoteInterface)";
Object ref = ctx.lookup(beanName);
remoteInterfaceClass myRemIFace = (remoteInterfaceClass)PortableRemoteObject.narrow(ref,Class.forName("remoteInterfaceClass");
....

I don't know if you see the problem. the problem is not really the runtime nature of your lookups but the fact that you need to know ahead of time what bean are you looking up. the only solution would be to try to have all you beans share the same remote interface and same home interface and so something like a factory pattern but I am yet to see a decent implementation of this that I think is useful. certainly won't hurt to try.
About | Sitemap | Contact