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.client.proxy;
017:
018: import java.util.Properties;
019:
020: public class ProxyManager {
021:
022: private static ProxyFactory defaultFactory;
023: private static String defaultFactoryName;
024:
025: static {
026: String version = null;
027: Class factory = null;
028: try {
029: version = System.getProperty("java.vm.version");
030: } catch (Exception e) {
031:
032: throw new RuntimeException(
033: "Unable to determine the version of your VM. No ProxyFactory Can be installed");
034: }
035: ClassLoader cl = getContextClassLoader();
036:
037: if (version.startsWith("1.1")) {
038: throw new RuntimeException(
039: "This VM version is not supported: " + version);
040: } else if (version.startsWith("1.2")) {
041: defaultFactoryName = "JDK 1.2 ProxyFactory";
042:
043: try {
044: Class.forName("org.opentools.proxies.Proxy", true, cl);
045: } catch (Exception e) {
046:
047: throw new RuntimeException(
048: "No ProxyFactory Can be installed. Unable to load the class org.opentools.proxies.Proxy. This class is needed for generating proxies in JDK 1.2 VMs.");
049: }
050:
051: try {
052: factory = Class
053: .forName(
054: "org.apache.openejb.client.proxy.Jdk12ProxyFactory",
055: true, cl);
056: } catch (Exception e) {
057:
058: throw new RuntimeException(
059: "No ProxyFactory Can be installed. Unable to load the class org.apache.openejb.client.proxy.Jdk12ProxyFactory.");
060: }
061: } else {
062: defaultFactoryName = "JDK 1.3 ProxyFactory";
063:
064: try {
065: factory = Class
066: .forName(
067: "org.apache.openejb.client.proxy.Jdk13ProxyFactory",
068: true, cl);
069: } catch (Exception e) {
070:
071: throw new RuntimeException(
072: "No ProxyFactory Can be installed. Unable to load the class org.apache.openejb.client.proxy.Jdk13ProxyFactory.");
073: }
074: }
075:
076: try {
077:
078: defaultFactory = (ProxyFactory) factory.newInstance();
079: defaultFactory.init(new Properties());
080:
081: } catch (Exception e) {
082:
083: throw new RuntimeException(
084: "No ProxyFactory Can be installed. Unable to load the class org.apache.openejb.client.proxy.Jdk13ProxyFactory.");
085: }
086:
087: }
088:
089: public static ProxyFactory getDefaultFactory() {
090: return defaultFactory;
091: }
092:
093: public static String getDefaultFactoryName() {
094: return defaultFactoryName;
095: }
096:
097: public static InvocationHandler getInvocationHandler(Object proxy) {
098: return defaultFactory.getInvocationHandler(proxy);
099: }
100:
101: public static Object setInvocationHandler(Object proxy,
102: InvocationHandler handler) {
103: return defaultFactory.setInvocationHandler(proxy, handler);
104: }
105:
106: public static Class getProxyClass(Class interfaceType)
107: throws IllegalAccessException {
108: return getProxyClass(new Class[] { interfaceType });
109: }
110:
111: public static Class getProxyClass(Class[] interfaces)
112: throws IllegalAccessException {
113: return defaultFactory.getProxyClass(interfaces);
114: }
115:
116: public static Object newProxyInstance(Class interfaceType,
117: InvocationHandler h) throws IllegalAccessException {
118: return newProxyInstance(new Class[] { interfaceType }, h);
119: }
120:
121: public static Object newProxyInstance(Class[] interfaces,
122: InvocationHandler h) throws IllegalAccessException {
123: return defaultFactory.newProxyInstance(interfaces, h);
124: }
125:
126: public static boolean isProxyClass(Class cl) {
127: return defaultFactory.isProxyClass(cl);
128: }
129:
130: public static Object newProxyInstance(Class proxyClass)
131: throws IllegalAccessException {
132: return defaultFactory.newProxyInstance(proxyClass);
133: }
134:
135: public static ClassLoader getContextClassLoader() {
136: return (ClassLoader) java.security.AccessController
137: .doPrivileged(new java.security.PrivilegedAction() {
138: public Object run() {
139: return Thread.currentThread()
140: .getContextClassLoader();
141: }
142: });
143: }
144: }
|