Discussions

EJB design: Using frameworks in ejbs(reply to using single session bean)

  1. hello it seems to be good idea....Our thinkings are really making j2ee much robust and modular

    We can also think of a single session bean have Different Action classes according to parameters...
    so that sesssion bean contains different types of execute methods..
    we can make different types of abstract base classes if there are more functions which can have the same parameters..

    one disadvantage in EJB LAYER when compared to servlet(web layer)

    in doGet or doPost we get request and response objects and from the servlet u can get Config and Context variables..

    But when a method is invoke on an ejb..we have to pass the parameters explicitly..





    i am pasting a simple examp

    my ejb action 1
    ---------------
    public abstract class MyEJBAction{

    public abstract ArrayList execute(
    TypeA a,
    TypeB b)
    throws Exception;

    }

    my ejb action2
    -------------

    public abstract class MyEJBDispatchAction{

    protected Class clazz = this.getClass();

    private static String methodNamePrefix = "get";

    protected Class types[] = { TypeA.class };

    public ArrayList execute(
    TypeA a,
    String methodName)
    throws Exception {

    return dispatchMethod(a, methodName);
    }

    protected ArrayList dispatchMethod(
    TypeA a,
    String methodName)
    throws Exception {

    ArrayList result = null;

    Object object = null;

    try {

    if (methodName == null || methodName.equals("")) {
    //INVALID METHOD
    } else {

    methodName = methodNamePrefix + methodName;
    Method method = clazz.getMethod(methodName, types);

    Object args[] = {a};
    result = (ArrayList) method.invoke(this, args);
    return result;
    }
    } catch (ClassCastException cce) {

    System.out.println("cce::"+cce);

    return (null);
    } catch (IllegalAccessException iae) {

    System.out.println("cce::"+iae);
    return (null);
    } catch (InvocationTargetException ite) {

    System.out.println("cce::"+ite);

    Throwable t = ite.getTargetException();
    if (t instanceof Exception) {
    throw ((Exception) t);
    } else {

    return (null);
    }
    }

    return null;
    }

    }

    my factory
    ----------
    public class MyEJBActionFactory {

    private static final String actionPackagePrefix =
    "myPackage.";


    public static String getActionClassName(String methodName) {

    return actionPackagePrefix + methodName + "Action";
    }

    public static MyEJBAction getEJBActionInstance(String actionName)
    throws Exception {
    ArrayList result = null;

    Class klass = null;
    Object object = null;

    if (actionName == null || actionName.equals("")) {
    //INVALID METHOD
    } else {

    String className = getActionClassName(actionName);
    klass = Class.forName(className);
    object = klass.newInstance();


    }

    return (MyEJBAction) object;

    }

    public static MyEJBDispatchAction getEJBDispatchActionInstance(String actionName)
    throws Exception {
    ArrayList result = null;

    Class klass = null;
    Object object = null;

    if (actionName == null || actionName.equals("")) {
    //INVALID METHOD

    } else {

    String className = getActionClassName(actionName);
    klass = Class.forName(className);
    object = klass.newInstance();
    }

    return (MyEJBDispatchAction) object;

    }

    }

    my business interace
    --------------------
    public interface MyBusinessInterface{


    public abstract ArrayList execute(
    TypeA a,
    TypeB b,
    String actionName)
    throws Exception;


    public abstract ArrayList execute(
    TypeA a,
    TypeB b,
    String actionName)
    throws Exception;
    }


    my remote
    --------
    public interface MyRemote extends MyBusinessInterface, EJBObject {

    }

    my home
    -------
    public interface MyHome extends EJBHome {

    MyRemote create() throws RemoteException, CreateException;
    }

    my bean
    -------

    public class MySLBean implements SessionBean, MyBusinessInterface {

    private SessionContext sc;
    private DataSource dataSource;

    public ArrayList execute(
    TypeA a,
    TypeB b,
    String actionName)
    throws Exception {

    ArrayList result = null;
    try {
    MyEJBAction myEJBAction =
    MyEJBActionFactory.getEJBActionInstance(actionName);

    result =
    myEJBAction.execute(a, b);
    return result;

    } catch (Exception e) {

    }
    return null;

    }

    public ArrayList execute(
    Type a,
    String actionName,
    String methodName)
    throws Exception {

    ArrayList result = null;

    try {
    MyEJBDispatchAction myEJBDispatchAction =
    MyEJBActionFactory.getEJBDispatchActionInstance(actionName);

    result =
    myEJBDispatchAction.execute(a, methodName);

    return result;

    } catch (Exception e) {


    }
    return null;
    }

    }


    NB:
    we can choose these kind of pattern only if we r having methods which can take same parameters so we can group them in an action class

    make actual implementation for action class type 1 & 2.

    Use action class Type1 if the business logic (one method-single class)is too heavy ..other wise use action2(dispatch one)
    mail me at joseph @ mastek.com for any clarifications
    www.mastek.com
    Joseph Antony Aruja
    Mastek Ltd.
    Millenium Business Park
    Mhape, Navi Mumbai
          Tel: 5591-4646 Ext:4920

  2. The enterprise bean must not attempt to query a class to obtain information about the declared
    members that are not otherwise accessible to the enterprise bean because of the security rules of the Java language. The enterprise bean must not attempt to use the Reflection API to access
    information that the security rules of the Java programming language make unavailable.

    participate also in :
    Strategy : Using reflection to invoke EJB.
  3. so i have to use "new" operator instead of class.forName()

    and ihave to to explicitly invoke the methods instead

    methodName = methodNamePrefix + methodName;
    Method method = clazz.getMethod(methodName, types);

    Object args[] = {a};
    result = (ArrayList) method.invoke(this, args);



    thanks
  4. email:vpvasavada@yahoo.com
  5. public class MySLBean implements SessionBean, MyBusinessInterface {

    >
    > private SessionContext sc;
    > private DataSource dataSource;
    >
    > public ArrayList execute(
    > TypeA a,
    > TypeB b,
    > String actionName)
    > throws Exception {
    >
    > ArrayList result = null;
    > try {
    > MyEJBAction myEJBAction =
    > MyEJBActionFactory.getEJBActionInstance(actionName);
    >
    > result =
    > myEJBAction.execute(a, b);
    > return result;
    >
    > } catch (Exception e) {
    >
    > }
    > return null;
    >
    > }
    >
    > public ArrayList execute(
    > Type a,
    > String actionName,
    > String methodName)
    > throws Exception {
    >
    > ArrayList result = null;
    >
    > try {
    > MyEJBDispatchAction myEJBDispatchAction =
    > MyEJBActionFactory.getEJBDispatchActionInstance(actionName);
    >
    > result =
    > myEJBDispatchAction.execute(a, methodName);
    >
    > return result;
    >
    > } catch (Exception e) {
    >
    >
    > }
    > return null;
    > }
    >
    > }
    >
    >
    Your session bean uses factory/classes that inturn uses Reflection API.
    You CAN NOT USE REFLECTION as per EJB Specification.
    Read ejb 2.0 spec for Programming Restrictions.
  6. Hi,
    > Your session bean uses factory/classes that inturn uses Reflection API.
    > You CAN NOT USE REFLECTION as per EJB Specification.
    > Read ejb 2.0 spec for Programming Restrictions.
    I think this part of the EJB spec :

    "The enterprise bean must not attempt to query a class to obtain information about the declared members that are not otherwise accessible to the enterprise bean because of the security rules of the Java language. The enterprise bean must not attempt to use the Reflection API to access information that the security rules of the Java programming language make unavailable. "

    is to be interpreted as : "The enterprise bean should not try to invoke/access methods/fields that are not accessible through ussuall mecahnism." So Ithink is safe to use reflection for factories, invoking methods, etc. as long as you don't try to do any nasty stuff. Anyway you should consult the security settings of your application server.
    Best regards, Mircea