Here is some code from a servlet we wrote that accepts
the bean name, a method name and an array of objects
as parameters and invokes against the EJB. It was used
to access functionality in EJBs through HTTP in lieu
of IIOP, and I have to say, it was much faster !
public void doPost(HttpServletRequest request,
HttpServletResponse response) {
dumpAttributes(request);
ObjectOutputStream out = null;
try {
out = new
ObjectOutputStream(response.getOutputStream());
response.setHeader("Content-Type", "no-cache");
response.setHeader("Pragma", "no-cache");
response.setHeader("Expires", "Tue, 1 Jan 1980
10:00:00 GMT");
LOG.debug("--------------- EJB Router Request
v" + version + "--------------");
response.setContentType("application/java");
LOG.debug("Opening Input Stream");
ObjectInputStream ois = new
ObjectInputStream(request.getInputStream());
LOG.debug("Requesting Object From Input
Stream");
Object obj = ois.readObject();
LOG.debug("Extracted Object From Input Stream:"
+ obj.getClass().getName());
LOG.debug("Casting To EJBRouterRequest...");
EJBRouterRequest ejbRequest =
(EJBRouterRequest)obj;
LOG.debug("Cast.");
LOG.debug("Looking Up Home:" +
ejbRequest.getBeanName());
EJBObject remote =
(EJBObject)remotes.get(ejbRequest.getBeanName());
Class[] classMethodParams = null;
Method method = null;
if(remote==null) {
obj = ctx.lookup(ejbRequest.getBeanName());
LOG.debug("Home Looked Up:" +
obj.getClass().getName());
LOG.debug("Narrowing to EJBHome....");
EJBHome home =
(EJBHome)PortableRemoteObject.narrow(obj,
EJBHome.class);
LOG.debug("EJBHome Narrowed.");
classMethodParams = new Class[0];
LOG.debug("Creating create() method...");
method = home.getClass().getMethod("create",
classMethodParams);
LOG.debug("create() method created.
Invoking...");
remote = (EJBObject) method.invoke(home,
classMethodParams);
remotes.put(ejbRequest.getBeanName(), remote);
LOG.debug("Remote Created.");
} else {
LOG.info("Remote Found In Cache:" +
remote.getClass().getName());
}
classMethodParams = new
Class[ejbRequest.getArgs().length];
for(int i=0; i < classMethodParams.length; i++)
{
classMethodParams[i] =
ejbRequest.getArgs()[i].getClass();
}
LOG.debug("Getting Requested Method.....");
method =
remote.getClass().getMethod(ejbRequest.getMethodName(),
classMethodParams);
LOG.debug("Got Requested Method:" + method + ".
Invoking....");
obj = method.invoke(remote,
ejbRequest.getArgs());
LOG.debug("Invoked. Object Is:" +
obj.getClass().getName());
LOG.debug("Returning Result...");
LOG.debug("ObjectValue:" + obj.toString());
out.writeObject(obj);
LOG.debug("--------------- END Router Request
--------------");
} catch (Throwable erx) {
if(erx instanceof InvocationTargetException) {
Throwable thr =
((InvocationTargetException)erx).getTargetException();
LOG.error("EJBRouter Service Invocation
Exception:" + thr);
Exception newEx = new Exception("EJBRouter
Invocation Exception:" + thr.getClass().getName() +
":" + thr.getMessage());
try {
out.writeObject(newEx);
} catch (Throwable ioex) {
LOG.error("EJBRouter Could Not Write
Exception Object To Output Stream:" + ioex);
}
//throw new ServletException(thr);
} else {
LOG.error("EJBRouter Service Unknown
Exception:" + erx);
Exception newEx = new Exception("EJBRouter
Invocation Exception:" + erx.getClass().getName() +
":" + erx.getMessage());
try {
out.writeObject(newEx);
} catch (Throwable ioex) {
LOG.error("EJBRouter Could Not Write
Exception Object To Output Stream:" + ioex);
}
//throw new ServletException(erx);
}
} finally {
try { out.flush(); } catch (Exception e) {};
try { out.close(); } catch (Exception e) {};
}
}
//Nicholas
--- Gene Chuang <[EMAIL PROTECTED]> wrote:
> Hi Anthony,
>
> You cannot "dynamically cast" in Java (doesn't make
> sense!), but you CAN dynamically
> evoke methods using reflection.
>
> So, just reflectively evoke "create" on the Object
> retreived from JNDI to get the
> EJBObject, and once again reflectively evoke the
> business method of your choosing on the
> resulting Object to achieve your goal.
>
> Gene
> --- Catalfano Anthony
> <[EMAIL PROTECTED]> wrote:
> > I'm writing a diagnostic utility servlet that will
> allow our operations
> > people to enter 3 parameters: PROVIDER_URL,
> JNDIHomeName, and BeanName. The
> > servlet needs to dynamically look up the home,
> dynamically cast it to the
> > JNDIHomeName param, call create() on the Home,
> dynamically cast the bean the
> > the BeanName. Wondering if anyone has code that
> will do this.
> >
> > Thanks
> >
> > Anthony Catalfano
> > Information Technology Analyst
> > Deere & Company
> > 309-748-5201
> >
> >
>
===========================================================================
> > To unsubscribe, send email to
> [EMAIL PROTECTED] and include in the body
> > of the message "signoff EJB-INTEREST". For
> general help, send email to
> > [EMAIL PROTECTED] and include in the body of
> the message "help".
> >
>
>
> __________________________________________________
> Do You Yahoo!?
> Make a great connection at Yahoo! Personals.
> http://personals.yahoo.com
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED]
> and include in the body
> of the message "signoff EJB-INTEREST". For general
> help, send email to
> [EMAIL PROTECTED] and include in the body of the
> message "help".
>
=====
Nicholas Whitehead
Home: (973) 377 9335
Cell: (973) 615 9646
Work(@ JP Morgan): (212) 235 5783
[EMAIL PROTECTED]
__________________________________________________
Do You Yahoo!?
Make a great connection at Yahoo! Personals.
http://personals.yahoo.com
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".