Listing A. Functionality.Java.

Enterprise Java
Writing a Reusable Implementation of the MVC Design Pattern
Prashant Sarode
Listing A. Functionality.Java.


package com.framework;

import java.lang.reflect.*;
import java.io.Serializable;
import com.oncology.framework.*;

public class Functionality{

private Class functionalityClass;
private Object functionalityInstance;
private Method handleEventMethod;
private Method getDebugMsgMethod;
private Object[] arguments;

public static String debugMsg;

public Functionality(String functionalityClassName){
try{

//Get Functionality class object.
functionalityClass = Class.forName(functionalityClassName);

//Create Instance of Functionality class.
functionalityInstance=functionalityClass.newInstance();

//Get Method object representing ‘handleEvent (Event e)’.
handleEventMethod =
                        get_handleEventMethod(functionalityInstance);

//Get Method object representing ‘getDebugMsg()’.
getDebugMsgMethod =
                    get_getDebugMsgMethod(functionalityInstance);

} catch (Exception e){
debugMsg= “Error Encountered either while instantiation or method
invocation...”;
}

}

public Result handleEvent(Event event){

Result resultOfMethodInvocation=null;

//Ready the arguments to be passed when the above method is invoked.
arguments = new Object[] { event};

try{
//Invoke the handleEvent(Event e) method.
resultOfMethodInvocation = (Result)handleEventMethod.invoke
(functionalityInstance,arguments);

//Invoke the getDebugMsg() method.
debugMsg=
    (String)getDebugMsgMethod.invoke(functionalityInstance, null);

}catch(Exception e){

debugMsg= “Error While Method Invocation-” + e.getMessage();

}

return resultOfMethodInvocation;

}

private Method get_handleEventMethod(Object obj){

Class c = obj.getClass();
Method handleEventMethod=null;

try{

Class[] parameterTypes = new Class[] {c.forName(“com.framework.Event”)};

handleEventMethod= c.getMethod(“handleEvent”,parameterTypes );

}catch(Exception e){

debugMsg= e.getMessage();

}
return handleEventMethod;
}

private Method get_getDebugMsgMethod(Object obj){
Class c = obj.getClass();
Class[] parameterTypes = null;
Method getDebugMsgMethod=null;

try{
getDebugMsgMethod= c.getMethod(“getDebugMsg”,parameterTypes );

}catch(Exception e){

debugMsg= e.getMessage();
}

return getDebugMsgMethod;
}

}

public Object getFunctionalityInstance(){

    return functionalityInstance;

}

public String getDebugMsg() {

    return debugMsg;
 }