KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > gbean > runtime > GBeanOperation


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.gbean.runtime;
19
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
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 /**
33  * @version $Rev: 485321 $ $Date: 2006-12-10 19:14:46 -0500 (Sun, 10 Dec 2006) $
34  */

35 public final class GBeanOperation {
36     private final GBeanInstance gbeanInstance;
37     private final String JavaDoc name;
38     private final List JavaDoc parameterTypes;
39     private final MethodInvoker methodInvoker;
40     private final boolean framework;
41     private final GOperationInfo operationInfo;
42
43     // TODO - deprecate this and add returnType
44
static GBeanOperation createFrameworkOperation(GBeanInstance gbeanInstance, String JavaDoc name, List JavaDoc parameterTypes, MethodInvoker methodInvoker) {
45         return new GBeanOperation(gbeanInstance, name, parameterTypes, methodInvoker);
46     }
47
48     private GBeanOperation(GBeanInstance gbeanInstance, String JavaDoc name, List JavaDoc parameterTypes, MethodInvoker methodInvoker) {
49         framework = true;
50         this.gbeanInstance = gbeanInstance;
51         this.name = name;
52         this.parameterTypes = Collections.unmodifiableList(new ArrayList JavaDoc(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         // get an array of the parameter classes
64
this.parameterTypes = Collections.unmodifiableList(new ArrayList JavaDoc(operationInfo.getParameterList()));
65         Class JavaDoc[] types = new Class JavaDoc[parameterTypes.size()];
66         ClassLoader JavaDoc classLoader = gbeanInstance.getClassLoader();
67         for (int i = 0; i < types.length; i++) {
68             String JavaDoc type = (String JavaDoc) parameterTypes.get(i);
69             try {
70                 types[i] = ClassLoading.loadClass((String JavaDoc) parameterTypes.get(i), classLoader);
71             } catch (ClassNotFoundException JavaDoc e) {
72                 throw new InvalidConfigurationException("Could not load operation parameter class:" +
73                         " name=" + operationInfo.getName() +
74                         " class=" + type);
75             }
76         }
77
78         // get a method invoker for the operation
79
if (operationInfo instanceof DynamicGOperationInfo) {
80             methodInvoker = new MethodInvoker() {
81                 private String JavaDoc[] types = (String JavaDoc[]) parameterTypes.toArray(new String JavaDoc[parameterTypes.size()]);
82
83                 public Object JavaDoc invoke(Object JavaDoc target, Object JavaDoc[] arguments) throws Exception JavaDoc {
84                     DynamicGBean dynamicGBean = (DynamicGBean) target;
85                     dynamicGBean.invoke(name, arguments, types);
86                     return null;
87                 }
88             };
89         } else {
90             try {
91                 Method JavaDoc 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 JavaDoc 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 JavaDoc getName() {
108         return name;
109     }
110
111     public List JavaDoc 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 JavaDoc invoke(Object JavaDoc target, final Object JavaDoc[] arguments) throws Exception JavaDoc {
124         return methodInvoker.invoke(target, arguments);
125     }
126
127     public String JavaDoc getDescription() {
128         String JavaDoc signature = name + "(";
129         for (Iterator JavaDoc iterator = parameterTypes.iterator(); iterator.hasNext();) {
130             String JavaDoc type = (String JavaDoc) 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