1 14 package org.compiere.model; 15 16 import java.lang.reflect.*; 17 import java.util.*; 18 19 import org.compiere.util.*; 20 21 27 public class CalloutEngine implements Callout 28 { 29 32 public CalloutEngine() 33 { 34 super(); 35 } 36 37 protected Logger log = Logger.getCLogger(getClass()); 38 39 56 public String start (Properties ctx, String methodName, int WindowNo, 57 MTab mTab, MField mField, Object value, Object oldValue) 58 { 59 if (methodName == null || methodName.length() == 0) 60 throw new IllegalArgumentException ("No Method Name"); 61 String retValue = ""; 63 StringBuffer msg = new StringBuffer (methodName).append(" - ") 64 .append(mField.getColumnName()) 65 .append("=").append(value) 66 .append(" (old=").append(oldValue) 67 .append(") {active=").append(isCalloutActive()).append("}"); 68 log.info (msg); 69 70 Method method = getMethod(methodName); 72 if (method == null) 73 throw new IllegalArgumentException ("Method not found: " + methodName); 74 int argLength = method.getParameterTypes().length; 75 if (!(argLength == 5 || argLength == 6)) 76 throw new IllegalArgumentException ("Method " + methodName + " has invalid no of arguments: " + argLength); 77 78 try 80 { 81 Object [] args = null; 82 if (argLength == 6) 83 args = new Object [] {ctx, new Integer (WindowNo), mTab, mField, value, oldValue}; 84 else 85 args = new Object [] {ctx, new Integer (WindowNo), mTab, mField, value}; 86 retValue = (String )method.invoke(this, args); 87 } 88 catch (Exception e) 89 { 90 setCalloutActive(false); 91 Throwable ex = e.getCause(); if (ex == null) 93 ex = e; 94 log.error ("start: " + methodName, ex); 95 ex.printStackTrace(System.err); 96 retValue = ex.getLocalizedMessage(); 97 } 98 return retValue; 99 } 101 109 public String convert (String methodName, String value) 110 { 111 if (methodName == null || methodName.length() == 0) 112 throw new IllegalArgumentException ("No Method Name"); 113 String retValue = null; 115 StringBuffer msg = new StringBuffer (methodName).append(" - ").append(value); 116 log.info (msg); 117 Method method = getMethod(methodName); 120 if (method == null) 121 throw new IllegalArgumentException ("Method not found: " + methodName); 122 int argLength = method.getParameterTypes().length; 123 if (argLength != 1) 124 throw new IllegalArgumentException ("Method " + methodName + " has invalid no of arguments: " + argLength); 125 126 try 128 { 129 Object [] args = new Object [] {value}; 130 retValue = (String )method.invoke(this, args); 131 } 132 catch (Exception e) 133 { 134 setCalloutActive(false); 135 log.error ("convert: " + methodName, e); 136 e.printStackTrace(System.err); 137 } 138 return retValue; 139 } 141 146 private Method getMethod (String methodName) 147 { 148 Method[] allMethods = getClass().getMethods(); 149 for (int i = 0; i < allMethods.length; i++) 150 { 151 if (methodName.equals(allMethods[i].getName())) 152 return allMethods[i]; 153 } 154 return null; 155 } 157 158 159 private static boolean s_calloutActive = false; 160 161 165 protected static boolean isCalloutActive() 166 { 167 return s_calloutActive; 168 } 170 174 protected static void setCalloutActive (boolean active) 175 { 176 s_calloutActive = active; 177 } 179 }
| Popular Tags
|