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.util.HashMap;
   20   
   21   public class ProxyManager {
   22   
   23       private static volatile ProxyFactory defaultFactory = new Jdk13ProxyFactory();
   24       private static final HashMap factories = new HashMap();
   25       private static volatile String defaultFactoryName;
   26   
   27       public static synchronized ProxyFactory registerFactory(String factoryName, ProxyFactory factory) {
   28           return (ProxyFactory) factories.put(factoryName, factory);
   29       }
   30   
   31       public static synchronized ProxyFactory unregisterFactory(String factoryName) {
   32           return (ProxyFactory) factories.remove(factoryName);
   33       }
   34   
   35       public static void checkDefaultFactory() {
   36           if (defaultFactory == null) throw new IllegalStateException("[Proxy Manager] No default proxy factory specified.");
   37       }
   38   
   39       public static ProxyFactory getFactory(String factoryName) {
   40           return (ProxyFactory) factories.get(factoryName);
   41       }
   42   
   43       public static synchronized ProxyFactory setDefaultFactory(String factoryName) {
   44           ProxyFactory newFactory = getFactory(factoryName);
   45           if (newFactory == null) return defaultFactory;
   46   
   47           ProxyFactory oldFactory = defaultFactory;
   48           defaultFactory = newFactory;
   49           defaultFactoryName = factoryName;
   50   
   51           return oldFactory;
   52       }
   53   
   54       public static ProxyFactory getDefaultFactory() {
   55           return defaultFactory;
   56       }
   57   
   58       public static String getDefaultFactoryName() {
   59           return defaultFactoryName;
   60       }
   61   
   62       public static InvocationHandler getInvocationHandler(Object proxy) {
   63           checkDefaultFactory();
   64           return defaultFactory.getInvocationHandler(proxy);
   65       }
   66   
   67       public static Class getProxyClass(Class interfaceType) throws IllegalAccessException {
   68           return getProxyClass(new Class[]{interfaceType});
   69       }
   70   
   71       public static Class getProxyClass(Class[] interfaces) throws IllegalAccessException {
   72           checkDefaultFactory();
   73           return defaultFactory.getProxyClass(interfaces);
   74       }
   75   
   76       public static Object newProxyInstance(Class interfaceType, InvocationHandler h) throws IllegalAccessException {
   77           return newProxyInstance(new Class[]{interfaceType}, h);
   78       }
   79   
   80       public static Object newProxyInstance(Class[] interfaces, InvocationHandler h) throws IllegalAccessException {
   81           checkDefaultFactory();
   82           return defaultFactory.newProxyInstance(interfaces, h);
   83       }
   84   
   85       public static boolean isProxyClass(Class cl) {
   86           checkDefaultFactory();
   87           return defaultFactory.isProxyClass(cl);
   88       }
   89   
   90   }

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