001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.openejb.util.proxy;
017:
018: import java.util.HashMap;
019:
020: public class ProxyManager {
021:
022: private static volatile ProxyFactory defaultFactory;
023: private static final HashMap factories = new HashMap();
024: private static volatile String defaultFactoryName;
025:
026: public static synchronized ProxyFactory registerFactory(
027: String factoryName, ProxyFactory factory) {
028: return (ProxyFactory) factories.put(factoryName, factory);
029: }
030:
031: public static synchronized ProxyFactory unregisterFactory(
032: String factoryName) {
033: return (ProxyFactory) factories.remove(factoryName);
034: }
035:
036: public static void checkDefaultFactory() {
037: if (defaultFactory == null)
038: throw new IllegalStateException(
039: "[Proxy Manager] No default proxy factory specified.");
040: }
041:
042: public static ProxyFactory getFactory(String factoryName) {
043: return (ProxyFactory) factories.get(factoryName);
044: }
045:
046: public static synchronized ProxyFactory setDefaultFactory(
047: String factoryName) {
048: ProxyFactory newFactory = getFactory(factoryName);
049: if (newFactory == null)
050: return defaultFactory;
051:
052: ProxyFactory oldFactory = defaultFactory;
053: defaultFactory = newFactory;
054: defaultFactoryName = factoryName;
055:
056: return oldFactory;
057: }
058:
059: public static ProxyFactory getDefaultFactory() {
060: return defaultFactory;
061: }
062:
063: public static String getDefaultFactoryName() {
064: return defaultFactoryName;
065: }
066:
067: public static InvocationHandler getInvocationHandler(Object proxy) {
068: checkDefaultFactory();
069: return defaultFactory.getInvocationHandler(proxy);
070: }
071:
072: public static Object setInvocationHandler(Object proxy,
073: InvocationHandler handler) {
074: checkDefaultFactory();
075: return defaultFactory.setInvocationHandler(proxy, handler);
076: }
077:
078: public static Class getProxyClass(Class interfaceType)
079: throws IllegalAccessException {
080: return getProxyClass(new Class[] { interfaceType });
081: }
082:
083: public static Class getProxyClass(Class[] interfaces)
084: throws IllegalAccessException {
085: checkDefaultFactory();
086: return defaultFactory.getProxyClass(interfaces);
087: }
088:
089: public static Object newProxyInstance(Class interfaceType,
090: InvocationHandler h) throws IllegalAccessException {
091: return newProxyInstance(new Class[] { interfaceType }, h);
092: }
093:
094: public static Object newProxyInstance(Class[] interfaces,
095: InvocationHandler h) throws IllegalAccessException {
096: checkDefaultFactory();
097: return defaultFactory.newProxyInstance(interfaces, h);
098: }
099:
100: public static boolean isProxyClass(Class cl) {
101: checkDefaultFactory();
102: return defaultFactory.isProxyClass(cl);
103: }
104:
105: public static Object newProxyInstance(Class proxyClass)
106: throws IllegalAccessException {
107: checkDefaultFactory();
108: return defaultFactory.newProxyInstance(proxyClass);
109: }
110:
111: }
|