1 17 18 package org.apache.geronimo.gbean.runtime; 19 20 import java.lang.reflect.Method ; 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 26 import org.apache.geronimo.gbean.DynamicGBean; 27 import org.apache.geronimo.gbean.DynamicGOperationInfo; 28 import org.apache.geronimo.gbean.GOperationInfo; 29 import org.apache.geronimo.gbean.InvalidConfigurationException; 30 import org.apache.geronimo.kernel.ClassLoading; 31 32 35 public final class GBeanOperation { 36 private final GBeanInstance gbeanInstance; 37 private final String name; 38 private final List parameterTypes; 39 private final MethodInvoker methodInvoker; 40 private final boolean framework; 41 private final GOperationInfo operationInfo; 42 43 static GBeanOperation createFrameworkOperation(GBeanInstance gbeanInstance, String name, List parameterTypes, MethodInvoker methodInvoker) { 45 return new GBeanOperation(gbeanInstance, name, parameterTypes, methodInvoker); 46 } 47 48 private GBeanOperation(GBeanInstance gbeanInstance, String name, List parameterTypes, MethodInvoker methodInvoker) { 49 framework = true; 50 this.gbeanInstance = gbeanInstance; 51 this.name = name; 52 this.parameterTypes = Collections.unmodifiableList(new ArrayList (parameterTypes)); 53 this.methodInvoker = methodInvoker; 54 this.operationInfo = new GOperationInfo(this.name, this.parameterTypes, "java.lang.Object"); 55 } 56 57 public GBeanOperation(GBeanInstance gbeanInstance, GOperationInfo operationInfo) throws InvalidConfigurationException { 58 framework = false; 59 this.gbeanInstance = gbeanInstance; 60 this.name = operationInfo.getName(); 61 this.operationInfo = operationInfo; 62 63 this.parameterTypes = Collections.unmodifiableList(new ArrayList (operationInfo.getParameterList())); 65 Class [] types = new Class [parameterTypes.size()]; 66 ClassLoader classLoader = gbeanInstance.getClassLoader(); 67 for (int i = 0; i < types.length; i++) { 68 String type = (String ) parameterTypes.get(i); 69 try { 70 types[i] = ClassLoading.loadClass((String ) parameterTypes.get(i), classLoader); 71 } catch (ClassNotFoundException e) { 72 throw new InvalidConfigurationException("Could not load operation parameter class:" + 73 " name=" + operationInfo.getName() + 74 " class=" + type); 75 } 76 } 77 78 if (operationInfo instanceof DynamicGOperationInfo) { 80 methodInvoker = new MethodInvoker() { 81 private String [] types = (String []) parameterTypes.toArray(new String [parameterTypes.size()]); 82 83 public Object invoke(Object target, Object [] arguments) throws Exception { 84 DynamicGBean dynamicGBean = (DynamicGBean) target; 85 dynamicGBean.invoke(name, arguments, types); 86 return null; 87 } 88 }; 89 } else { 90 try { 91 Method javaMethod = gbeanInstance.getType().getMethod(operationInfo.getMethodName(), types); 92 if (AbstractGBeanReference.NO_PROXY) { 93 methodInvoker = new ReflectionMethodInvoker(javaMethod); 94 } else { 95 methodInvoker = new FastMethodInvoker(javaMethod); 96 } 97 } catch (Exception e) { 98 throw new InvalidConfigurationException("Target does not have specified method (declared in a GBeanInfo operation):" + 99 " name=" + operationInfo.getName() + 100 " methodName=" + operationInfo.getMethodName() + 101 " returnType=" + operationInfo.getReturnType() + 102 " targetClass=" + gbeanInstance.getType().getName()); 103 } 104 } 105 } 106 107 public String getName() { 108 return name; 109 } 110 111 public List getParameterTypes() { 112 return parameterTypes; 113 } 114 115 public GOperationInfo getOperationInfo() { 116 return operationInfo; 117 } 118 119 public boolean isFramework() { 120 return framework; 121 } 122 123 public Object invoke(Object target, final Object [] arguments) throws Exception { 124 return methodInvoker.invoke(target, arguments); 125 } 126 127 public String getDescription() { 128 String signature = name + "("; 129 for (Iterator iterator = parameterTypes.iterator(); iterator.hasNext();) { 130 String type = (String ) iterator.next(); 131 signature += type; 132 if (iterator.hasNext()) { 133 signature += ", "; 134 } 135 } 136 signature += ")"; 137 return "Operation Signature: " + signature + ", GBeanInstance: " + gbeanInstance.getName(); 138 } 139 } 140 | Popular Tags |