public class LibraryDelegate implements ILibrary { PRivate ILibrary library; private Map availableMethods;
public LibraryDelegate() { init(); }
public void init() { // Look up and oBTain our session bean try { LibraryHome libraryHome = (LibraryHome)EJBHomeFactory.getInstance().lookup( "java:comp/env/ejb/LibraryHome", LibraryHome.class); library = libraryHome.create();
// Get the methods available for use in proxying availableMethods = new HashMap(); Method[] methods = ILibrary.class.getMethods(); for (int i=0; i<methods.length; i++) { availableMethods.put(methods[i].getName(), methods[i]); } } catch (NamingException e) { throw new RuntimeException(e); } catch (CreateException e) { throw new RuntimeException(e); } catch (RemoteException e) { throw new RuntimeException(e); } }
// All the hard-coded methods are removed public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
try { // See if this is init() or destroy() if (method.getName().equals("init")) { init(); return null; } else if (method.getName().equals("destroy")) { destroy(); return null; } else { Method method = (Method)availableMethods.get(method.getName());
// See if we found anything if (method != null) { return method.invoke(library, args); } else { throw new NoSUChMethodException("The Library does not " + "support the " + method.getName() +" method."); } } } catch (InvocationTargetException e) { // We don´t support throwing RuntimeExceptions from EJBs // directly if (e.getTargetException() instanceof RemoteException) { throw new RuntimeException(e); } else { throw e.getTargetException(); } } }
public void destroy() { // In this case, do nothing } }