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: // Explicitly resolve primitive class name.
108: // This will be done by the standard ObjectInputStream on JDK 1.4+,
109: // but needs to be done explicitly on JDK 1.3.
110: Class clazz = ClassUtils
111: .resolvePrimitiveClassName(classDesc.getName());
112: if (clazz != null) {
113: return clazz;
114: }
115: // If codebaseUrl is set, try to load the class with the RMIClassLoader.
116: // Else, propagate the ClassNotFoundException.
117: if (this .codebaseUrl == null) {
118: throw ex;
119: }
120: return RMIClassLoader.loadClass(this .codebaseUrl, classDesc
121: .getName());
122: }
123: }
124:
125: /**
126: * Overridden version delegates to super class first,
127: * falling back to the specified codebase if not found locally.
128: */
129: protected Class resolveProxyClass(String[] interfaces)
130: throws IOException, ClassNotFoundException {
131: if (this .classLoader != null) {
132: // Use the specified ClassLoader to resolve local proxy classes.
133: Class[] resolvedInterfaces = new Class[interfaces.length];
134: for (int i = 0; i < interfaces.length; i++) {
135: try {
136: resolvedInterfaces[i] = Class.forName(
137: interfaces[i], false, this .classLoader);
138: } catch (ClassNotFoundException ex) {
139: if (this .codebaseUrl == null) {
140: throw ex;
141: }
142: resolvedInterfaces[i] = RMIClassLoader.loadClass(
143: this .codebaseUrl, interfaces[i]);
144: }
145: }
146: try {
147: return Proxy.getProxyClass(this .classLoader,
148: resolvedInterfaces);
149: } catch (IllegalArgumentException ex) {
150: throw new ClassNotFoundException(null, ex);
151: }
152: } else {
153: // Let RMI use it's default ClassLoader...
154: try {
155: return super .resolveProxyClass(interfaces);
156: } catch (ClassNotFoundException ex) {
157: if (this .codebaseUrl == null) {
158: throw ex;
159: }
160: ClassLoader loader = RMIClassLoader
161: .getClassLoader(this .codebaseUrl);
162: Class[] resolvedInterfaces = new Class[interfaces.length];
163: for (int i = 0; i < interfaces.length; i++) {
164: resolvedInterfaces[i] = loader
165: .loadClass(interfaces[i]);
166: }
167: return Proxy.getProxyClass(loader, resolvedInterfaces);
168: }
169: }
170: }
171:
172: }
|