spacer

 

Friday, September 01, 2006

Refactoring the EJBHomeFactory in JDK 1.5


Here's nifty way of refactory the EJBHomeFactory. What I really wanted to do if it's possible is to do to make a call to the factory without having to pass the EJBHome class. I have that implemented here but with the expense of a JNDI Name to EJBHome class mapping. This is a code snippet of what I wish Java Templating could do. That's too bad that Java Templating doesn't support the T.class static property.

public <T extends EJBHome> T getRemoteHome(String jndiHomeName)
        throws RemoteException {
    T home = null;
    try {
        InitialContext ctx = new InitialContext().lookup(jndiHomeName);
        home = (T) PortableRemoteObject.narrow(objRef, T.class);
    } catch (NamingException e) {
        throw (new RemoteException("Failed lookup on jndi [" + jndiHomeName
                + "]", e));
    }
    return home;
}

The old way
try {
    AccountManagerHome remoteHome = (AccountManagerHome) EJBHomeFactory
            .getInstance().getRemoteHomeTheOldWay("ejb/AccountManager");
    AccountManager mgr = remoteHome.create();
    mgr.doSomething();
} catch (RemoteException e) {
    e.printStackTrace();
} catch (CreateException e) {
    e.printStackTrace();
}

Using JDK 1.5 Typesafety
The only difference here is that the typcasting is ommitted.


// Using JDK 1.5 Typesafety
try {
    AccountManagerHome remoteHome = EJBHomeFactory.getInstance()
            .getRemoteHome("ejb/AccountManager");
    AccountManager mgr = remoteHome.create();
    mgr.doSomething();
} catch (RemoteException e) {
    e.printStackTrace();
} catch (CreateException e) {
    e.printStackTrace();
}
To View the source code click: 
EJBHomeFactory.java
or simply just browse the
package
contents
.


No comments:

Popular Posts

Featured Post

Encrypting Properties File Values with Jasypt

Encrypting Properties File Values with Jasypt What's the fuzz all about? Property files are text resources in your standard web applic...