001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.remoting.rmi;
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.ObjectInputStream;
022: import java.io.ObjectStreamClass;
023: import java.lang.reflect.Proxy;
024: import java.rmi.server.RMIClassLoader;
025:
026: import org.springframework.util.ClassUtils;
027:
028: /**
029: * Special ObjectInputStream subclass that falls back to a specified codebase
030: * to load classes from if not found locally. In contrast to standard RMI
031: * conventions for dynamic class download, it is the client that determines
032: * the codebase URL here, rather than the "java.rmi.server.codebase" system
033: * property on the server.
034: *
035: * <p>Uses the JDK's RMIClassLoader to load classes from the specified codebase.
036: * The codebase can consist of multiple URLs, separated by spaces.
037: * Note that RMIClassLoader requires a SecurityManager to be set, like when
038: * using dynamic class download with standard RMI! (See the RMI documentation
039: * for details.)
040: *
041: * <p>Despite residing in the RMI package, this class is <i>not</i> used for
042: * RmiClientInterceptor, which uses the standard RMI infrastructure instead
043: * and thus is only able to rely on RMI's standard dynamic class download via
044: * "java.rmi.server.codebase". CodebaseAwareObjectInputStream is used by
045: * HttpInvokerClientInterceptor (see the "codebaseUrl" property there).
046: *
047: * <p>Thanks to Lionel Mestre for suggesting the option and providing
048: * a prototype!
049: *
050: * @author Juergen Hoeller
051: * @since 1.1.3
052: * @see java.rmi.server.RMIClassLoader
053: * @see org.springframework.remoting.httpinvoker.HttpInvokerClientInterceptor#setCodebaseUrl
054: */
055: public class CodebaseAwareObjectInputStream extends ObjectInputStream {
056:
057: private final ClassLoader classLoader;
058:
059: private final String codebaseUrl;
060:
061: /**
062: * Create a new CodebaseAwareObjectInputStream for the given InputStream and codebase.
063: * @param in the InputStream to read from
064: * @param codebaseUrl the codebase URL to load classes from if not found locally
065: * (can consist of multiple URLs, separated by spaces)
066: * @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
067: */
068: public CodebaseAwareObjectInputStream(InputStream in,
069: String codebaseUrl) throws IOException {
070: this (in, null, codebaseUrl);
071: }
072:
073: /**
074: * Create a new CodebaseAwareObjectInputStream for the given InputStream and codebase.
075: * @param in the InputStream to read from
076: * @param classLoader the ClassLoader to use for loading local classes
077: * (may be <code>null</code> to indicate RMI's default ClassLoader)
078: * @param codebaseUrl the codebase URL to load classes from if not found locally
079: * (can consist of multiple URLs, separated by spaces)
080: * @see java.io.ObjectInputStream#ObjectInputStream(java.io.InputStream)
081: */
082: public CodebaseAwareObjectInputStream(InputStream in,
083: ClassLoader classLoader, String codebaseUrl)
084: throws IOException {
085:
086: super (in);
087: this .classLoader = classLoader;
088: this .codebaseUrl = codebaseUrl;
089: }
090:
091: /**
092: * Overridden version delegates to super class first,
093: * falling back to the specified codebase if not found locally.
094: */
095: protected Class resolveClass(ObjectStreamClass classDesc)
096: throws IOException, ClassNotFoundException {
097: try {
098: if (this .classLoader != null) {
099: // Use the specified ClassLoader to resolve local classes.
100: return Class.forName(classDesc.getName(), false,
101: this .classLoader);
102: } else {
103: // Let RMI use it's default ClassLoader...
104: return super .resolveClass(classDesc);
105: }
106: } catch (ClassNotFoundException ex) {
107: // If codebaseUrl is set, try to load the class with the RMIClassLoader.
108: // Else, propagate the ClassNotFoundException.
109: if (this .codebaseUrl == null) {
110: throw ex;
111: }
112: return RMIClassLoader.loadClass(this .codebaseUrl, classDesc
113: .getName());
114: }
115: }
116:
117: /**
118: * Overridden version delegates to super class first,
119: * falling back to the specified codebase if not found locally.
120: */
121: protected Class resolveProxyClass(String[] interfaces)
122: throws IOException, ClassNotFoundException {
123: if (this .classLoader != null) {
124: // Use the specified ClassLoader to resolve local proxy classes.
125: Class[] resolvedInterfaces = new Class[interfaces.length];
126: for (int i = 0; i < interfaces.length; i++) {
127: try {
128: resolvedInterfaces[i] = Class.forName(
129: interfaces[i], false, this .classLoader);
130: } catch (ClassNotFoundException ex) {
131: if (this .codebaseUrl == null) {
132: throw ex;
133: }
134: resolvedInterfaces[i] = RMIClassLoader.loadClass(
135: this .codebaseUrl, interfaces[i]);
136: }
137: }
138: try {
139: return Proxy.getProxyClass(this .classLoader,
140: resolvedInterfaces);
141: } catch (IllegalArgumentException ex) {
142: throw new ClassNotFoundException(null, ex);
143: }
144: } else {
145: // Let RMI use it's default ClassLoader...
146: try {
147: return super .resolveProxyClass(interfaces);
148: } catch (ClassNotFoundException ex) {
149: if (this .codebaseUrl == null) {
150: throw ex;
151: }
152: ClassLoader loader = RMIClassLoader
153: .getClassLoader(this .codebaseUrl);
154: Class[] resolvedInterfaces = new Class[interfaces.length];
155: for (int i = 0; i < interfaces.length; i++) {
156: resolvedInterfaces[i] = loader
157: .loadClass(interfaces[i]);
158: }
159: return Proxy.getProxyClass(loader, resolvedInterfaces);
160: }
161: }
162: }
163:
164: }
|