| |
"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){
try{
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
//ut.begin
//ourMember.setFirstName("Jane");
//ourMember.setLastName("Doe");
//ourMember.setMagazineName("Computer Weekly");
//ut.commit();
//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.setFirstName("Jane");
//mWrapper.setLastName("Doe");
//mWrapper.setMagazineName("Computer Weekly");
//ourMember.setSubscriptionInformation(mWrapper);
// OUR APPROACH
//Using EntityBeanServices, you prepare a hashtable
java.util.Hashtable ht2 = new java.util.Hashtable();
ht2.put("setFirstName","Jane");
ht2.put("setLastName","Doe");
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
application
ourMember.setBeanAttributes(ht2);
} //end try
catch(Exception e){
e.printStackTrace();
} //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();
try{
name = getClass().getName();
Method m[] = getClass().getDeclaredMethods();
while(keys.hasMoreElements()){
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
if(key.equalsIgnoreCase(methodName))
//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"
if(!methodName.startsWith("get"))
if(!methodName.startsWith("ejb"))
{
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){
e.printStackTrace();
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];
try{
//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)
continue;
//get methods do not have parameters
if(m[i].getModifiers() != Modifier.PUBLIC)
continue;
//get methods must be public
continue;
if(!m[i].getName().startsWith("get"))
//get methods must start with "get"
if(m[i].getName().equals("getBeanAttributes"))
continue;
//should not be itself
Object ob = m[i].invoke(this, arglist);
if (ob != null)
ht.put(m[i].getName(), ob);
}
}catch(Throwable e){
e.printStackTrace();
}
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
java.rmi.RemoteException;
public java.util.Hashtable getBeanAttributes() throws
java.rmi.RemoteException;
}
|
|