1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.io.ByteArrayInputStream;
29  import java.io.ByteArrayOutputStream;
30  import java.io.ObjectInputStream;
31  import java.io.ObjectOutputStream;
32  
33  import java.lang.Object;
34  import java.lang.reflect.InvocationTargetException;
35  import java.lang.reflect.Method;
36  
37  import java.util.ArrayList;
38  import java.util.List;
39  
40  /**
41   * <a href="ClassLoaderProxy.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class ClassLoaderProxy {
47  
48      public ClassLoaderProxy(Object obj, ClassLoader classLoader) {
49          _obj = obj;
50          _classLoader = classLoader;
51      }
52  
53      public ClassLoader getClassLoader() {
54          return _classLoader;
55      }
56  
57      public Object invoke(String methodName, Object[] args) throws Throwable {
58          Thread currentThread = Thread.currentThread();
59  
60          ClassLoader contextClassLoader = currentThread.getContextClassLoader();
61  
62          try {
63              currentThread.setContextClassLoader(_classLoader);
64  
65              Class<?> classObj = Class.forName(
66                  _obj.getClass().getName(), true, _classLoader);
67  
68              List<Class<?>> parameterTypes = new ArrayList<Class<?>>();
69  
70              for (int i = 0; i < args.length; i++) {
71                  Object arg = args[i];
72  
73                  Class<?> argClass = Class.forName(
74                      arg.getClass().getName(), true, _classLoader);
75  
76                  if (ClassUtil.isSubclass(argClass, PrimitiveWrapper.class)) {
77                      MethodKey methodKey = new MethodKey(
78                          argClass.getName(), "getValue", null);
79  
80                      Method method = MethodCache.get(methodKey);
81  
82                      args[i] = method.invoke(arg, (Object[])null);
83  
84                      argClass = (Class<?>)argClass.getField("TYPE").get(arg);
85                  }
86  
87                  parameterTypes.add(argClass);
88              }
89  
90              Method method = null;
91  
92              try {
93                  method = classObj.getMethod(
94                      methodName,
95                      parameterTypes.toArray(new Class[parameterTypes.size()]));
96              }
97              catch (NoSuchMethodException nsme) {
98                  Method[] methods = ((Class<?>)classObj).getMethods();
99  
100                 for (int i = 0; i < methods.length; i++) {
101                     Class<?>[] methodParameterTypes =
102                         methods[i].getParameterTypes();
103 
104                     if (methods[i].getName().equals(methodName) &&
105                         methodParameterTypes.length == parameterTypes.size()) {
106 
107                         boolean correctParams = true;
108 
109                         for (int j = 0; j < parameterTypes.size(); j++) {
110                             Class<?> a = parameterTypes.get(j);
111                             Class<?> b = methodParameterTypes[j];
112 
113                             if (!ClassUtil.isSubclass(a, b)) {
114                                 correctParams = false;
115 
116                                 break;
117                             }
118                         }
119 
120                         if (correctParams) {
121                             method = methods[i];
122 
123                             break;
124                         }
125                     }
126                 }
127 
128                 if (method == null) {
129                     throw nsme;
130                 }
131             }
132 
133             return method.invoke(_obj, args);
134         }
135         catch (InvocationTargetException ite) {
136             throw translateThrowable(ite.getCause(), contextClassLoader);
137         }
138         catch (Throwable t) {
139             _log.error(t, t);
140 
141             throw t;
142         }
143         finally {
144             currentThread.setContextClassLoader(contextClassLoader);
145         }
146     }
147 
148     protected Throwable translateThrowable(
149         Throwable t1, ClassLoader contextClassLoader) {
150 
151         try {
152             ByteArrayOutputStream baos = new ByteArrayOutputStream();
153             ObjectOutputStream oos = new ObjectOutputStream(baos);
154 
155             oos.writeObject(t1);
156 
157             oos.flush();
158             oos.close();
159 
160             byte[] bytes = baos.toByteArray();
161 
162             ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
163             ObjectInputStream ois = new ClassLoaderObjectInputStream(
164                 bais, contextClassLoader);
165 
166             t1 = (Throwable)ois.readObject();
167 
168             ois.close();
169 
170             return t1;
171         }
172         catch (Throwable t2) {
173             _log.error(t2, t2);
174 
175             return t2;
176         }
177     }
178 
179     private static Log _log = LogFactoryUtil.getLog(ClassLoaderProxy.class);
180 
181     private Object _obj;
182     private ClassLoader _classLoader;
183 
184 }