Wednesday 11 April 2007

API helper

public class ApiHelper

{
private ApiHelper(){ }


private static ApiHelper apiHelper;

public static ApiHelper getInstance()
{
if(apiHelper==null){
apiHelper=new ApiHelper();
}
return apiHelper;
}
public ArrayList getMethodSignature(String classPath) throws ApiTestException { try
{
StringBuffer methodSignature=new StringBuffer("");
ArrayList methodList = new ArrayList();
Class inputClass = Class.forName(classPath);
//Method methArr []= inputClass.getDeclaredMethods();
Method methArr []= inputClass.getMethods();
boolean isCompattible=true;
for (int i=0;i methodSignature=new StringBuffer("");
isCompattible=true;
methodSignature.append(methArr[i].getName()+"(");
Class methodArgs[]= methArr[i].getParameterTypes();
for (int k = 0; k String argName = methodArgs[k].getName();
if (methodArgs[k].isPrimitive() argName.endsWith(".Long") argName.endsWith(".Integer") argName.endsWith(".Float") argName.endsWith(".Double") argName.endsWith(".Boolean") argName.endsWith(".String") argName.endsWith(".Timestamp") argName.endsWith(".Date")){

if (methodArgs[k].isPrimitive()){
log4Debug.debug("----------- data type is Primitive -------------");
if (k==(methodArgs.length-1))
methodSignature.append(methodArgs[k].getName());
else
methodSignature.append(methodArgs[k].getName()+",");
}
else
{
log4Debug.debug("----------- data type is not primitive -------------");
String argType = methodArgs[k].getName();
int lastIndex = argType.lastIndexOf(".");
if (k==(methodArgs.length-1))
methodSignature.append(argType.subSequence(lastIndex+1,argType.length()));
else
methodSignature.append(argType.subSequence(lastIndex+1,argType.length())+",");
}

}
else{ log4Debug.debug("----------- method is not compattible -------------");
i sCompattible = false;
}
}
if (!isCompattible){
methodSignature = new StringBuffer("non-compattitable");
} else{
methodSignature.append(")");
}
log4Debug.debug("----------- method signature is : ",methodSignature);
methodList.add(i,methodSignature.toString());
}
return methodList;
}
catch(ClassNotFoundException classNotFoundException){
log4Debug.debug(" =====class not found =======",classNotFoundException); throw new ApiTestException("Class not found "+ classNotFoundException.getMessage()); } } public String getReturnType(int methodPosition,String classPath) throws ApiTestException { Method selectedMethod = null; try { Class inputClass = Class.forName(classPath); Method methArr [] = inputClass.getMethods(); selectedMethod = methArr[methodPosition]; } catch(ClassNotFoundException classNotFoundException){ log4Debug.debug(" =====class not found =======",classNotFoundException); throw new ApiTestException(classNotFoundException.getMessage()); } return selectedMethod.getReturnType().toString(); } public int getNoOfArguments(int methodPosition,String classPath) throws ApiTestException { Method selectedMethod = null; try { Class inputClass = Class.forName(classPath); Method methArr [] = inputClass.getMethods(); selectedMethod = methArr[methodPosition]; log4Debug.debug("===selected method name ========",selectedMethod.getName()); } catch(ClassNotFoundException classNotFoundException){ log4Debug.debug(" =====class not found =======",classNotFoundException); throw new ApiTestException(classNotFoundException.getMessage()); } return selectedMethod.getParameterTypes().length; } public String[] getParameterName(String methodName){ CommonProperties commonProperties = CommonPropertiesFactory.getInstance().getCommonProperties("AperturaCT"); String paramName = commonProperties.getProperty(methodName); String param[]=null; if(paramName!=null){ param=paramName.split(","); } return param; } public Object[] convertArguments(int methodPosition,String arguments,String classPath) throws ApiTestException{
try { Class inputClass = Class.forName(classPath); Method methArr []= inputClass.getMethods(); Method selectedMethod = methArr[methodPosition]; log4Debug.debug("return type is : ",selectedMethod.getReturnType()); String args[] = arguments.split("\\^"); log4Debug.debug("=======argument length=======",String.valueOf(args.length)); Class methodArgs[]= selectedMethod.getParameterTypes(); Object objArr[]=new Object[methodArgs.length]; for (int k = 0; k try { log4Debug.debug(" going to invoke the metohd "); Class inputClass = Class.forName(classPath); Method methArr [] = inputClass.getMethods(); Method selectedMethod = methArr[methodPosition]; if(classPath.endsWith(".IGestoreInformazioneContoTitoli")){ log4Debug.debug("========== getting the instance for IGICT========== "); IGestoreInformazioneContoTitoli iGestoreInformazioneContoTitoli = GestoreInformazioneContoTitoliFactory.getInstance().getGestoreInformazioneContoTitoli(); return selectedMethod.invoke(iGestoreInformazioneContoTitoli,objArr); // return selectedMethod.invoke(iGestoreInformazioneContoTitoli,objArr); }else if(classPath.endsWith(".IGestoreContoTitoli")){ log4Debug.debug("========== getting the instance for IGestoreContoTitoli========== "); IGestoreContoTitoli iGestoreContoTitoli = GestoreContoTitoliImplFactory.getInstance().getGestoreContoTitoliImpl(); return selectedMethod.invoke(iGestoreContoTitoli,objArr); // return selectedMethod.invoke(iGestoreInformazioneContoTitoli,objArr); }else if(classPath.endsWith(".GestoreInformazioneContoTitoliBean")){ log4Debug.debug("========== getting the instance for GICT========== "); String classPathHome = classPath.replaceFirst("Bean","Home"); Class homeClass = Class.forName(classPathHome); Object homeObj = PortableRemoteObject.narrow(new InitialContext().lookup(classPathHome),homeClass); Method homeMethod= homeClass.getMethod("create",null); Object remoteObject = homeMethod.invoke(homeObj,null); return selectedMethod.invoke(remoteObject,null); /*gestoreInformazioneContoTitoli = GestoreInformazioneContoTitoliHome.create(); GestoreInformazioneContoTitoli iGestoreInformazioneContoTitoli = (GestoreInformazioneContoTitoli)GestoreInformazioneContoTitoliFactory.getInstance().getGestoreInformazioneContoTitoli(); return selectedMethod.invoke(iGestoreInformazioneContoTitoli,objArr); return selectedMethod.invoke(iGestoreInformazioneContoTitoli,objArr); */ }else if(classPath.endsWith(".IGestoreCRW")){ log4Debug.debug("========== getting the instance for IGestoreCRW========== "); IGestoreCRW iGestoreCRW =GestoreCRWFactory.getInstance().getGestoreCRW(); return selectedMethod.invoke(iGestoreCRW,objArr); // return selectedMethod.invoke(iGestoreInformazioneContoTitoli,objArr); } else{ return selectedMethod.invoke(inputClass.newInstance(),objArr); } } catch(ClassNotFoundException classNotFoundException){ log4Debug.debug(" =====class not found =======",classNotFoundException); throw new ApiTestException("class Not Found :" +classNotFoundException.getMessage()); } catch (IllegalArgumentException illegalArgumentException) { log4Debug.debug(" =====IllegalArgumentException =======",illegalArgumentException); throw new ApiTestException("Wrong No. Of Argument : " + illegalArgumentException.getMessage()); } catch (IllegalAccessException illegalAccessException) { log4Debug.debug(" =====IllegalAccessException =======",illegalAccessException); throw new ApiTestException(illegalAccessException.getMessage()); } catch (InstantiationException instantiationException) { log4Debug.debug(" =====InstantiationException =======",instantiationException); throw new ApiTestException("unable to create instance :" +instantiationException.getMessage()); } catch (ClassCastException classCastException) { log4Debug.debug(" =====ClassCastException =======",classCastException); throw new ApiTestException(classCastException.getMessage()); } catch (NamingException namingException) { log4Debug.debug(" =====NamingException =======",namingException); throw new ApiTestException("Jndi name not found :"+namingException.getMessage()); } catch (GestoreInformazioneContoTitoliFactoryException gestoreInformazioneContoTitoliFactoryException) { log4Debug.debug(" =====gestoreInformazioneContoTitoliFactoryException =======",gestoreInformazioneContoTitoliFactoryException); throw new ApiTestException(" gestoreInformazioneContoTitoliFactoryException :"+gestoreInformazioneContoTitoliFactoryException.getMessage()); } catch (GestoreContoTitoliImplFactoryException gestoreContoTitoliImplFactoryException) { log4Debug.debug(" =====gestoreContoTitoliImplFactoryException =======",gestoreContoTitoliImplFactoryException); throw new ApiTestException(" gestoreContoTitoliImplFactoryException :"+gestoreContoTitoliImplFactoryException.getMessage()); } catch (GestoreCRWFactoryException gestoreCRWFactoryException) { log4Debug.debug(" =====GestoreCRWFactoryException =======",gestoreCRWFactoryException); throw new ApiTestException("GestoreCRWFactoryException :" +gestoreCRWFactoryException.getMessage()); } catch (SecurityException securityException) { log4Debug.debug(" =====securityException =======",securityException); throw new ApiTestException(securityException.getMessage()); } catch (NoSuchMethodException noSuchMethodException) { log4Debug.debug(" =====noSuchMethodException =======",noSuchMethodException); throw new ApiTestException("No Method Found :"+noSuchMethodException.getMessage()); } } public String getReturnedViewValues(Object oriClass,Class returnClass) throws ApiTestException, InvocationTargetException{ try { Method methodArr[]=returnClass.getMethods(); String returnedValues=""; for (int i=0;i

No comments: