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   package org.apache.geronimo.gbean.runtime;
   18   
   19   import org.apache.geronimo.gbean.AbstractName;
   20   import org.apache.geronimo.gbean.GReferenceInfo;
   21   import org.apache.geronimo.gbean.InvalidConfigurationException;
   22   import org.apache.geronimo.kernel.ClassLoading;
   23   import org.apache.geronimo.kernel.GBeanNotFoundException;
   24   import org.apache.geronimo.kernel.Kernel;
   25   import org.apache.geronimo.kernel.management.State;
   26   import org.slf4j.Logger;
   27   import org.slf4j.LoggerFactory;
   28   
   29   import java.lang.reflect.Method;
   30   import java.lang.reflect.Modifier;
   31   
   32   /**
   33    * @version $Rev: 651684 $ $Date: 2008-04-25 12:11:52 -0700 (Fri, 25 Apr 2008) $
   34    */
   35   public abstract class AbstractGBeanReference implements GBeanReference {
   36       /**
   37        * Should we proxy references.
   38        */
   39       protected static final boolean NO_PROXY;
   40       static {
   41           Logger log = LoggerFactory.getLogger(AbstractGBeanReference.class);
   42           String no_proxy = System.getProperty("Xorg.apache.geronimo.gbean.NoProxy", "true");
   43           NO_PROXY = no_proxy.equalsIgnoreCase("true");
   44           if (NO_PROXY) {
   45               log.info("GBean references are not using proxies");
   46           } else {
   47               log.info("GBean references are using proxies");
   48           }
   49       }
   50   
   51       /**
   52        * Name of this reference.
   53        */
   54       private final String name;
   55   
   56       /**
   57        * Interface this GBeanInstance uses to refer to the other.
   58        */
   59       private final Class referenceType;
   60   
   61       /**
   62        * Proxy type which is injected into the GBeanInstance.
   63        */
   64       private final Class proxyType;
   65   
   66       /**
   67        * The GBeanInstance to which this reference belongs.
   68        */
   69       private final GBeanInstance gbeanInstance;
   70   
   71       /**
   72        * The method that will be called to set the attribute value.  If null, the value will be set with
   73        * a constructor argument
   74        */
   75       private final MethodInvoker setInvoker;
   76   
   77       private final boolean hasTargets;
   78   
   79       /**
   80        * The metadata for this reference
   81        */
   82       private final GReferenceInfo referenceInfo;
   83   
   84       /**
   85        * The kernel to which the reference is bound.
   86        */
   87       private final Kernel kernel;
   88   
   89       /**
   90        * Proxy for this reference
   91        */
   92       private Object proxy;
   93   
   94   
   95       public AbstractGBeanReference(GBeanInstance gbeanInstance, GReferenceInfo referenceInfo, Kernel kernel, boolean hasTargets) throws InvalidConfigurationException {
   96           this.gbeanInstance = gbeanInstance;
   97           this.referenceInfo = referenceInfo;
   98           this.kernel = kernel;
   99           this.hasTargets = hasTargets;
  100   
  101           this.name = referenceInfo.getName();
  102           try {
  103               this.referenceType = ClassLoading.loadClass(referenceInfo.getReferenceType(), gbeanInstance.getType().getClassLoader());
  104           } catch (ClassNotFoundException e) {
  105               throw new InvalidConfigurationException("Could not load Reference Type: " + getDescription(), e);
  106           }
  107           if (Modifier.isFinal(referenceType.getModifiers())) {
  108               throw new IllegalArgumentException("Proxy interface cannot be a final class: " + referenceType.getName());
  109           }
  110           try {
  111               this.proxyType = ClassLoading.loadClass(referenceInfo.getProxyType(), gbeanInstance.getType().getClassLoader());
  112           } catch (ClassNotFoundException e) {
  113               throw new InvalidConfigurationException("Could not load Proxy Type:" + getDescription(), e);
  114           }
  115   
  116           if (referenceInfo.getSetterName() != null) {
  117               try {
  118                   String setterName = referenceInfo.getSetterName();
  119                   Method setterMethod = gbeanInstance.getType().getMethod(setterName, new Class[] {proxyType});
  120                   if (NO_PROXY) {
  121                       setInvoker = new ReflectionMethodInvoker(setterMethod);
  122                   } else {
  123                       setInvoker = new FastMethodInvoker(setterMethod);
  124                   }
  125               } catch (NoSuchMethodException e) {
  126                   throw new InvalidConfigurationException("Setter method not found " + getDescription(), e);
  127               }
  128           } else {
  129               setInvoker = null;
  130           }
  131   
  132       }
  133   
  134       protected final Kernel getKernel() {
  135           return kernel;
  136       }
  137   
  138       public final GBeanInstance getGBeanInstance() {
  139           return gbeanInstance;
  140       }
  141   
  142       public final String getName() {
  143           return name;
  144       }
  145   
  146       public final GReferenceInfo getReferenceInfo() {
  147           return referenceInfo;
  148       }
  149   
  150       public final Class getReferenceType() {
  151           return referenceType;
  152       }
  153   
  154       public final Class getProxyType() {
  155           return proxyType;
  156       }
  157   
  158       public final Object getProxy() {
  159           return proxy;
  160       }
  161   
  162       protected final void setProxy(Object proxy) {
  163           this.proxy = proxy;
  164       }
  165   
  166       /**
  167        * Is the component in the Running state
  168        *
  169        * @param abstractName name of the component to check
  170        * @return true if the component is running; false otherwise
  171        */
  172       protected boolean isRunning(Kernel kernel, AbstractName abstractName) {
  173           try {
  174               final int state = kernel.getGBeanState(abstractName);
  175               return state == State.RUNNING_INDEX;
  176           } catch (GBeanNotFoundException e) {
  177               // mbean is no longer registerd
  178               return false;
  179           } catch (Exception e) {
  180               // problem getting the attribute, mbean has most likely failed
  181               return false;
  182           }
  183       }
  184   
  185       protected final String getDescription() {
  186           return "\n    GBeanInstance: " + gbeanInstance.getName() +
  187                   "\n    Reference Name: " + getName() +
  188                   "\n    Reference Type: " + referenceInfo.getReferenceType() +
  189                   "\n    Proxy Type: " + referenceInfo.getProxyType();
  190       }
  191   
  192   }

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