Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Server-side Java: Create forward-compatible beans in EJB, Part 2

More key strategies for developing portable EJB 1.0 beans for EJB 1.1 servers

  • Print
  • Feedback

Page 2 of 6

List 2-A. EJB 1.0: Using the EJBContext security methods
public class AccountBean implements EntityBean {
  int id;
  double balance;
  String modifiedBy;
  EntityContext ejbContext;
  PortableContext portableContext;
  public void withdraw(Double withdraw)
      throws WithdrawLimitException, AccessDeniedException {
    // only tellers can withdraw more than 10k
    if(withdraw.doubleValue() > 10000) {
      Identity tellerIdnty = new RoleIdentity("teller");
      boolean isTeller = ejbContext.isCallerInRole(tellerIdnty)
      if(!isTeller)
        throw new AccessDeniedException();
    }
    Double limit = (Double)
      portableContext.getEnvironmentEntry(
        "java:comp/env/withdraw_limit", Double.class);
    if (withdraw.doubleValue() > limit.doubleValue())
      throw new WithdrawLimitException(limit);
    else
      balance = balance - withdraw.doubleValue();
    Identity identity = ejbContext.getCallerIdentity( );
    String modifiedBy = identity.getName();
  }
  ...
}


List 2-B. EJB 1.1: Using the EJBContext security methods
public class AccountBean implements EntityBean {
  int id;
  double balance;
  String modifiedBy;
  EntityContext ejbContext;
  PortableContext portableContext;
  public void withdraw(Double withdraw)
      throws WithdrawLimitException, AccessDeniedException {
    // only tellers can withdraw more than 10k
    if(withdraw.doubleValue() > 10000) {
      boolean isTeller = ejbContext.isCallerInRole("teller")
      if (!isTeller)
        throw new AccessDeniedException( );
    }
    Double limit = (Double)
      portableContext.getEnvironmentEntry(
        "java:comp/env/withdraw_limit",Double.class);
    if (withdraw.doubleValue() > limit.doubleValue())
      throw new WithdrawLimitException(limit);
    else
      balance = balance - withdraw.doubleValue();
    Principal principal = ejbContext.getCallerPrincipal( );
    String modifiedBy = principal.getName();
  }
  ...
}


PortableContext can hide the EJB 1.0 and EJB 1.1 security models from the bean. To accomplish this, the PortableContext models its abstraction around the EJB 1.1 security model. Below, the abstract PortableContext class has been modified to include two new methods, getCallerPrincipal() and isCallerInRole(), which mimic the new security methods in the EJB 1.1 EJBContext.

List 3. PortableContext class with new abstract security methods
import javax.ejb.*;
import java.lang.reflect.Method;
import java.security.Principal;

public abstract class PortableContext { final static String SYSTEM_PROPERTY_NAME = "java.ejb.portable_context"; EJBContext ejbContext; public static PortableContext getInstance(EJBContext context) throws PortableContextException{ String className = System.getProperty(SYSTEM_PROPERTY_NAME); if(className == null) throw new PortableContextException("System property for implementation not set"); try{ Class clazz = Class.forName(className); PortableContext portableCtx = (PortableContext)clazz.newInstance(); portableCtx.setEJBContext(context); return portableCtx; }catch(Exception e){ throw new PortableContextException(e); } } public void setEJBContext(EJBContext ctx){ ejbContext = ctx; } public abstract Object lookup(String name, Class type)throws PortableContextException;
public abstract Principal getCallerPrincipal( );

public abstract boolean isCallerInRole(String roleName);
}


In the PortableContext class, the security methods are abstract, which means that the PortableContext implementations (PortableContext1_0 and PortableContext1_1) must implement these methods.

  • Print
  • Feedback

Resources

Server-side Java: Read the whole series -archived on JavaWorld