Home » geronimo-2.2-source-release » org.apache.geronimo.gbean.runtime » [javadoc | source]

    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;
   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   /**
   33    * @version $Rev: 690859 $ $Date: 2008-08-31 23:30:25 -0700 (Sun, 31 Aug 2008) $
   34    */
   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) {
   44           return new GBeanOperation(gbeanInstance, name, parameterTypes, methodInvoker);
   45       }
   46   
   47       private GBeanOperation(GBeanInstance gbeanInstance, String name, List parameterTypes, MethodInvoker methodInvoker) {
   48           framework = true;
   49           this.gbeanInstance = gbeanInstance;
   50           this.name = name;
   51           this.parameterTypes = Collections.unmodifiableList(new ArrayList(parameterTypes));
   52           this.methodInvoker = methodInvoker;
   53           // this is not used
   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(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, e);
   75               }
   76           }
   77   
   78           // get a method invoker for the operation
   79           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                   StringBuilder b = new StringBuilder().append("Target does not have specified method (declared in a GBeanInfo operation): name=")
   99                           .append(operationInfo.getName())
  100                           .append(" methodName=").append(operationInfo.getMethodName())
  101                           .append(" returnType=").append(operationInfo.getReturnType())
  102                           .append(" targetClass=").append(gbeanInstance.getType().getName());
  103                   b.append("\ntargetClassClassLoader=").append(gbeanInstance.getType().getClassLoader());
  104                   for (Class aType: types) {
  105                       b.append("\n  parameter type=").append(aType.getName()).append(" in classloader: ").append(aType.getClassLoader());
  106                   }
  107   
  108                   throw new InvalidConfigurationException(b.toString(), e);
  109               }
  110           }
  111       }
  112   
  113       public String getName() {
  114           return name;
  115       }
  116   
  117       public List getParameterTypes() {
  118           return parameterTypes;
  119       }
  120   
  121       public GOperationInfo getOperationInfo() {
  122           return operationInfo;
  123       }
  124   
  125       public boolean isFramework() {
  126           return framework;
  127       }
  128   
  129       public Object invoke(Object target, final Object[] arguments) throws Exception {
  130           return methodInvoker.invoke(target, arguments);
  131       }
  132   
  133       public String getDescription() {
  134           String signature = name + "(";
  135           for (Iterator iterator = parameterTypes.iterator(); iterator.hasNext();) {
  136               String type = (String) iterator.next();
  137               signature += type;
  138               if (iterator.hasNext()) {
  139                   signature += ", ";
  140               }
  141           }
  142           signature += ")";
  143           return "Operation Signature: " + signature + ", GBeanInstance: " + gbeanInstance.getName();
  144       }
  145   }

Home » geronimo-2.2-source-release » org.apache.geronimo.gbean.runtime » [javadoc | source]