Best Practices for Coding Standard Interfaces


Many new EJB developers are confused by the relationship between the remote interface and the bean class. This contrast is necessary for the container to intercept all method calls to the EJB. One confusing aspect is that the EJB class implements methods defined in the remote interface, but the EJB class does not implement the remote interface itself. In fact, the EJB class should never implement the remote interface. Although the EJB specification does allow this practice, it can cause serious bugs . The problem with having the EJB class implementing the remote interface is that the EJB class can be passed as a parameter to any method that expects the remote interface as a parameter.

Remember that the remote interface is needed so that the container can intercept method calls to provide necessary services, such as transaction or security. If the bean class is used, the method calls arrive directly on the bean object, creating a dangerous situation in which the container cannot intercept method calls or intervene in case of error. If the EJB class does not implement the remote interface, this problem becomes apparent at compile time. The Java compiler will reject the attempt to pass the bean class as a parameter of the remote interface's type.

The only advantage of implementing the remote interface in the bean class is that the Java compiler catches any method that is defined in the remote interface but not implemented in the bean class. However, this also means that the bean class has to provide dummy implementations of the remote interface's super class to satisfy the Java compiler.

Clearly, implementing the remote interface in the bean class is not a good practice, but catching the bean writer's errors as early as possible is desirable. WebLogic Server comes with a command-line utility called the EJB Compiler (EJBC), which performs a compliance check to catch as many violations of the EJB specification as possible. The weblogic.ejbc utility analyzes an EJB and flags any specification violations with the related section in the EJB specification. This utility can be performed as an attribute of the Java Complier (javac) on the command line or by using the graphical deployment tools. It catches any methods defined in the remote interface but not implemented in the EJB class.

Another method for catching this kind of error is using a pattern known as business interface . In this pattern, all the business methods are defined in a separate interface. The remote interface extends the business interface and the javax.ejb.EJBObject interface. The bean class can then implement the business interface, as shown here:

 
 public interface MyBusinessInterface { public void businessMethod() throws RemoteException; } public interface MyRemoteInterface extends MyBusinessInterface, EJBObject {} public class MyBean implements SessionBean, MyBusinessInterface { public void businessMethod() throws RemoteException{} } 

By using the business interface, the Java Compiler ensures that the bean class implements the methods in the business interface. The pattern is also safe because the bean class cannot be passed into a method expecting the remote interface. Although both classes can be assigned from the business interface, the remote interface is still a distinct type.



BEA WebLogic Platform 7
BEA WebLogic Platform 7
ISBN: 0789727129
EAN: 2147483647
Year: 2003
Pages: 360

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net