DefaultSessionBean

Discussions

EJB programming & troubleshooting: DefaultSessionBean

  1. DefaultSessionBean (2 messages)

    In AWT event handling, an event handling class can implement WindowListener
    (lot's of work) or extend WindowAdpater(less work). In swing, a table model
    can implement TableModel (lot's of work) or extend AbtractTableModel (less
    work). I have been using this same idea with ejb-classes for a few years now
    (See DefaultSessionBean class shown below). However, I never see it in any
    books or sample code and wonder why.

    I am posting the class for two reasons:

    1. Does anyone see any problem with using this class?
    2. If not, feel free to use it.

    Note: I am already aware of a few issues:

    1. You must be careful not to pass "this"
    2. You are precluded from inheriting from some other class (due to Java's
    single inheritance)
    3. If you misspell a SessionBean call-back method, it won't be caught by the
    compiler.

    These are all fairly minor.

    Below is the class:

    ==========================================================

    /* This class provides default implementations of the
    SessionBean interface and no-op implementations of
    EJBObject's methods. This helper class provides two benefits:

    1. No need to mindlessly type-in SessionBeans's
       methods, which are almost always no-op or boiler-plate.
       IMO, inheriting these default implementations is more
       elegant than using code-generation. This class is similar
       in concept to AWT's adapter classes
       (i.e. WindowAdapter,MouseAdapter) or swing's "default"
       classes (i.e. DefaultTableModel, DefaultTreeModel).

    2. The ejb-class can now implement the remote interface.
       This way, if you don't properly implement the interface,
       you receive a compile-time error rather than a deploy-time
       error (which can be cryptic at times).
       See Section 9.2.2 of the EJB 1.1 Spec.

       Note: when passing a self reference to another class,
       be sure to use:
         someOtherClass.foo(ejbContext.getEJBObject())
       rather than:
         someOtherClass.foo(this)

    Dave Ford
    http://www.smart-soft.com */

    package ss.util;

    import javax.ejb.*;
    import javax.naming.*;

    public class DefaultSessionBean implements EJBObject,SessionBean{

      protected SessionContext ejbContext;
      protected Context namingContext;
      protected boolean debug = true;

      /* methods from EJBObject */
      public EJBHome getEJBHome(){ return null; }
      public Handle getHandle(){return null;}
      public Object getPrimaryKey(){return null;}
      public boolean isIdentical(EJBObject obj){return false;}
      public void remove(){}


      /* methods from SessionBean */

      public void ejbActivate() {
        if(debug) System.out.println(this + " - ejbActivate");
      }

      public void ejbPassivate() {
        if(debug) System.out.println(this + " - ejbPassivate");
      }

      public void ejbRemove() {
        if(debug) System.out.println(this + " - ejbRemove");
      }

      public void setSessionContext(SessionContext c){
        if(debug) System.out.println(this + " - setSessionContext");
        ejbContext = c;
        try{
          namingContext = new InitialContext();
        }
        catch(NamingException ex){
          throw new EJBException(ex);
        }
      }
    }

    Threaded Messages (2)

  2. DefaultSessionBean[ Go to top ]

    i can advice you a good EJB book with title "Enterprise JavaBeans" by Richard Monson-Haefel from O'REILLY
  3. DefaultSessionBean[ Go to top ]

    That's funny. I own that book. Does it address this topic? Either I missed it or I have old version of the book.