Is it possible to develop a generic client code to access arbitrary EJBs deployed in the application server. The user will deploy their own EJBs in the production environment along with a xml file which provides the complete info about EJB like JNDI name,LocalHome,LocalInterface etc.
But the Client Code should be Generic enough to narrow down any EJBHome(Client should not hardcode any specific Home Class in narrow() method). The client is a web component deployed in the same application server.
Object remoteObj = null; try { if(ctx == null) throw new ServletException("Initial context unavailable, unable to perform this transaction at this time");
// Load the home interface class and locate the object through JNDI Class requestClassHome = Class.forName(className+"Home"); Object home = ctx.lookup(name); home = PortableRemoteObject.narrow(home, requestClassHome);
// Load the remote interface class and create an instance using the home interface Class requestClass = Class.forName(className); remoteObj = create(requestClassHome, home); remoteObj = PortableRemoteObject.narrow(remoteObj, requestClass);
return remoteObj; } catch(ClassNotFoundException excnf) { throw new XMLServletException("Class not found error: " + excnf); } catch(ClassCastException excc) { throw new XMLServletException("Error performing object narrow: " + excc); } catch(XMLServletException e) { throw e; } catch(Exception e) { throw new XMLServletException("Unexpected error "+e.getMessage()+ " while creating instance of " + name); } } protected Object create(Class requestClassHome, Object home) throws XMLServletException { Object ret=null;
try { Method m = requestClassHome.getMethod("create", null); ret = m.invoke(home, null); } catch(NoSuchMethodException exmnf) { throw new XMLServletException("Unable to find create method on home interface: " + exmnf); } catch(InvocationTargetException e) { throw new XMLServletException("Unable to execute create method on home interface " + requestClassHome + " " + e.getTargetException()); } catch(Exception e) { throw new XMLServletException( "Unable to create object from Home interface : " + requestClassHome + " Exception thrown " + e ); } return ret; }