HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML

"Universal Wrapper For Entity Beans, by Andrei Povodyrev & Alan Askew"
Vol. 6, Issue 5, p. 50


Listing 1
public class EJBClient{ 
        public static void main(String [] args){ 
        Member ourMember; 
//Put code here to get the remote interface for a particular Member object 

//To change Member data before, you might have used one of the three styles: 

//Style 1. Calling set methods directly...Requires three network round trips 
//javax.transaction UserTransaction     
ut = //get the UserTransaction 
                        //ourMember.setMagazineName("Computer Weekly"); 

                        //Style 2. A long-parameter list set method is inflexible as 
     the number of arguments is //fixed 
                        // ourMember.setSubscriptionInformation("Jane", "Doe", 
     "Computer Weekly"); 

                        //Style 3. A set method with a custom wrapper class 
     (MemberWrapper)is cumbersome to maintain: changes in 
     entity bean will require corresponding changes on wrapper 
     class; each entity bean requires its own wrapper 
                        //MemberWrapper mWrapper = new MemberWrapper(); 
                        //mWrapper.setMagazineName("Computer Weekly"); 


                        // OUR APPROACH 
                        //Using EntityBeanServices, you prepare a hashtable 
                        java.util.Hashtable ht2 = new java.util.Hashtable(); 
                        ht2.put("setMagazineName","Computer Weekly"); 
                        //Then make a single call, uniting the set methods in a single 
                        transaction. //Universal form for every entity bean in your 

                } //end try 
                catch(Exception e){ 
                } //end catch 
        } //end main method 
} //end class 

Listing 2 

public class EntityBeanServices { 

        public void setBeanAttributes(Hashtable ht) throws java.rmi.RemoteException{ 

                //invoke set methods based on keys in ht 
                String key, name, methodName = null; 
                boolean methodNotFound = true; 
                Object arglist[] = new Object[1]; 
                Enumeration keys = ht.keys(); 

                        name  = getClass().getName(); 

                        Method m[] = getClass().getDeclaredMethods(); 

                                key =  (String)keys.nextElement(); 
                                methodNotFound = true; 
                                for(int i = 0; i < m.length; i++){ 
                                        methodName = m[i].getName(); 

// loop through bean's methods to find a match 
// some filtering based on common sense is desirable    
//match name of the method 
//get methods must be public 
                  if(m[i].getModifiers() == Modifier.PUBLIC) 
//set methods have a single argument 
                  if((m[i].getParameterTypes()).length == 1) 
//set methods must not  start with "get" 
    methodNotFound = false; 
    arglist[0] = ht.get(key); 
//invoke matched method 
 m[i].invoke(this, arglist); 
}// end if 
 }//end for 
   if(methodNotFound) throw new Exception("Attempt to 
     invoke a set method " + key + " on " + name + " failed. \n"  
	 + "       
	 Possible reasons: \n" 
	 + " 1) it is not a set method;\n " 
	 + " 2) method has more than 1 argument;\n" 
	 + " 3) method name does not start with 'set'\n" 
	 + " 4) no such method in the class" ); 

                        }//end while 

                }catch(Throwable e){ 
                  throw new java.rmi.RemoteException("Exception is rethrown");} 

        public Hashtable getBeanAttributes(){ 
                // identify and invoke all get methods for this entity bean 
                Hashtable ht = new Hashtable(); 
                Object arglist[] = new Object[0]; 

                        //get all methods of the EJB 
                        Method m[] = getClass().getDeclaredMethods(); 

                        // filter get methods and invoke them 
                        for(int i = 0; i < m.length; i++){ 
                                if((m[i].getParameterTypes()).length != 0)    
        //get methods do not have parameters 
                                if(m[i].getModifiers() != Modifier.PUBLIC)    
//get methods must be public                       
								//get methods must start with "get" 
		//should not be itself 

                                Object ob = m[i].invoke(this, arglist); 
                                if (ob != null) 
                                                ht.put(m[i].getName(), ob); 
                }catch(Throwable e){ 
                return ht; 


Listing 3 

* EntityBeanServicesInt is designed as a parent interface for 
   EJB remote interfaces. 
* Interface declares methods that are implemented in 
   EntitybeanServices class 
public interface EntityBeanServicesInt { 
        public void setBeanAttributes(java.util.Hashtable ht) throws 
        public java.util.Hashtable getBeanAttributes() throws 


All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.