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.lang.reflect.Constructor;
019: import java.lang.reflect.InvocationTargetException;
020: import java.lang.reflect.Proxy;
021: import java.util.Properties;
022:
023: public class Jdk13ProxyFactory implements ProxyFactory {
024:
025: private final Class[] constructorParams = { java.lang.reflect.InvocationHandler.class };
026:
027: public void init(Properties props) {
028: String version = "";
029: String badVersion = "1.3.0-";
030: try {
031: version = System.getProperty("java.vm.version");
032: } catch (Exception e) {
033: }
034:
035: if (version.indexOf(badVersion) != -1) {
036: String message = ""
037: + "INCOMPATIBLE VM: \n\n"
038: + "The Java Virtual Machine you are using contains a bug\n"
039: + "in the proxy generation logic. This bug has been \n"
040: + "documented by Sun and has been fixed in later VMs. \n"
041: + "Please download the latest 1.3 Virtual Machine. \n"
042: + "For more details see: \n"
043: + "http://developer.java.sun.com/developer/bugParade/bugs/4346224.html\n ";
044: throw new RuntimeException(message);
045: }
046: }
047:
048: public InvocationHandler getInvocationHandler(Object proxy)
049: throws IllegalArgumentException {
050:
051: Jdk13InvocationHandler handler = (Jdk13InvocationHandler) Proxy
052: .getInvocationHandler(proxy);
053:
054: if (handler == null)
055: return null;
056:
057: return handler.getInvocationHandler();
058: }
059:
060: public Object setInvocationHandler(Object proxy,
061: InvocationHandler handler) throws IllegalArgumentException {
062:
063: Jdk13InvocationHandler jdk13 = (Jdk13InvocationHandler) Proxy
064: .getInvocationHandler(proxy);
065:
066: if (jdk13 == null)
067: throw new IllegalArgumentException("Proxy " + proxy
068: + " unknown!");
069:
070: return jdk13.setInvocationHandler(handler);
071: }
072:
073: public Class getProxyClass(Class interfce)
074: throws IllegalArgumentException {
075: return Proxy.getProxyClass(interfce.getClassLoader(),
076: new Class[] { interfce });
077: }
078:
079: public Class getProxyClass(Class[] interfaces)
080: throws IllegalArgumentException {
081: if (interfaces.length < 1) {
082: throw new IllegalArgumentException(
083: "There must be at least one interface to implement.");
084: }
085:
086: return Proxy.getProxyClass(interfaces[0].getClassLoader(),
087: interfaces);
088: }
089:
090: public boolean isProxyClass(Class cl) {
091: return Proxy.isProxyClass(cl);
092: }
093:
094: public Object newProxyInstance(Class proxyClass)
095: throws IllegalArgumentException {
096: if (!Proxy.isProxyClass(proxyClass)) {
097: throw new IllegalArgumentException(
098: "This class is not a proxy.");
099: }
100:
101: try {
102:
103: Constructor cons = proxyClass
104: .getConstructor(constructorParams);
105: return cons
106: .newInstance(new Object[] { new Jdk13InvocationHandler() });
107:
108: } catch (NoSuchMethodException e) {
109: throw new InternalError(e.toString());
110: } catch (IllegalAccessException e) {
111: throw new InternalError(e.toString());
112: } catch (InstantiationException e) {
113: throw new InternalError(e.toString());
114: } catch (InvocationTargetException e) {
115: throw new InternalError(e.toString());
116: }
117: }
118:
119: public Object newProxyInstance(Class interfce, InvocationHandler h)
120: throws IllegalArgumentException {
121:
122: Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h);
123:
124: return Proxy.newProxyInstance(interfce.getClassLoader(),
125: new Class[] { interfce }, handler);
126: }
127:
128: public Object newProxyInstance(Class[] interfaces,
129: InvocationHandler h) throws IllegalArgumentException {
130: if (interfaces.length < 1) {
131: throw new IllegalArgumentException(
132: "There must be at least one interface to implement.");
133: }
134:
135: Jdk13InvocationHandler handler = new Jdk13InvocationHandler(h);
136:
137: return Proxy.newProxyInstance(interfaces[0].getClassLoader(),
138: interfaces, handler);
139: }
140: }
|