Home » openejb-3.1.2-src » org.apache » openejb » util » proxy » [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.openejb.util.proxy;
   18   
   19   import java.lang.reflect.Constructor;
   20   import java.lang.reflect.InvocationTargetException;
   21   import java.lang.reflect.Proxy;
   22   import java.util.Properties;
   23   
   24   import org.apache.openejb.OpenEJBException;
   25   
   26   /**
   27    * @org.apache.xbean.XBean 
   28    */
   29   public class Jdk13ProxyFactory implements ProxyFactory {
   30   
   31       public Jdk13ProxyFactory() {
   32       }
   33   
   34       public void init(Properties props) throws OpenEJBException {
   35       }
   36   
   37       public org.apache.openejb.util.proxy.InvocationHandler getInvocationHandler(Object proxy) throws IllegalArgumentException {
   38           InvocationHandler handler = (InvocationHandler) Proxy.getInvocationHandler(proxy);
   39           if (handler == null) return null;
   40           return handler.getInvocationHandler();
   41       }
   42   
   43       public Class getProxyClass(Class interfce) throws IllegalArgumentException {
   44           return Proxy.getProxyClass(interfce.getClassLoader(), new Class[]{interfce});
   45       }
   46   
   47       public Class getProxyClass(Class[] interfaces) throws IllegalArgumentException {
   48           if (interfaces.length < 1) {
   49               throw new IllegalArgumentException("It's boring to implement 0 interfaces!");
   50           }
   51           return Proxy.getProxyClass(interfaces[0].getClassLoader(), interfaces);
   52       }
   53   
   54       /*
   55        * Returns true if and only if the specified class was dynamically generated to be a proxy class using the getProxyClass method or the newProxyInstance method.
   56        */
   57       public boolean isProxyClass(Class cl) {
   58           return Proxy.isProxyClass(cl);
   59       }
   60   
   61       /*
   62        * Returns an instance of a proxy class for the specified interface that dispatches method invocations to
   63        * the specified invocation handler.
   64        */
   65       public Object newProxyInstance(Class interfce, org.apache.openejb.util.proxy.InvocationHandler h) throws IllegalArgumentException {
   66           return Proxy.newProxyInstance(interfce.getClassLoader(), new Class[]{interfce}, h);
   67       }
   68   
   69       /*
   70        * Returns an instance of a proxy class for the specified interface that dispatches method invocations to
   71        * the specified invocation handler.
   72        */
   73       public Object newProxyInstance(Class[] interfaces, org.apache.openejb.util.proxy.InvocationHandler handler) throws IllegalArgumentException {
   74           if (interfaces.length < 1) {
   75               throw new IllegalArgumentException("It's boring to implement 0 interfaces!");
   76           }
   77   
   78           try {
   79               return Proxy.newProxyInstance(interfaces[0].getClassLoader(), interfaces, handler);
   80           } catch (IllegalArgumentException e) {
   81               ClassLoader tccl = Thread.currentThread().getContextClassLoader();
   82               try {
   83                   Class tcclHomeClass = tccl.loadClass(interfaces[0].getName());
   84                   if (tcclHomeClass == interfaces[0]) {
   85                       return Proxy.newProxyInstance(tccl, interfaces, handler);
   86                   }
   87               } catch (ClassNotFoundException e1) {
   88                   throw e;
   89               }
   90               throw e;
   91           }
   92       }
   93   }
   94   

Home » openejb-3.1.2-src » org.apache » openejb » util » proxy » [javadoc | source]