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: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Mikhail A. Markov
021: * @version $Revision: 1.8.4.2 $
022: */package java.rmi.server;
023:
024: import java.io.InputStream;
025: import java.io.IOException;
026: import java.util.StringTokenizer;
027: import java.net.MalformedURLException;
028: import java.net.URL;
029: import java.security.AccessController;
030: import java.security.PrivilegedAction;
031:
032: import org.apache.harmony.rmi.DefaultRMIClassLoaderSpi;
033: import org.apache.harmony.rmi.internal.nls.Messages;
034:
035: /**
036: * @com.intel.drl.spec_ref
037: * This class could not be instantiated.
038: *
039: * @author Mikhail A. Markov
040: * @version $Revision: 1.8.4.2 $
041: */
042: public class RMIClassLoader {
043:
044: // The name of property for custom RMIClassLoaderSpi.
045: private static final String spiProp = "java.rmi.server.RMIClassLoaderSpi"; //$NON-NLS-1$
046:
047: // The name of resource for custom RMIClassLoaderSpi.
048: private static final String spiResource = "META-INF/services/java.rmi.server.RMIClassLoaderSpi"; //$NON-NLS-1$
049:
050: /*
051: * Default RMIClassLoaderSpi instance.
052: */
053: private static RMIClassLoaderSpi defaultSpi = new DefaultRMIClassLoaderSpi();
054:
055: /*
056: * Implementation of RMIClassLoaderSpi which will be used for delegating
057: * method calls of this class to.
058: */
059: private static RMIClassLoaderSpi activeSpi = (RMIClassLoaderSpi) AccessController
060: .doPrivileged(new PrivilegedAction<Object>() {
061: public Object run() {
062: return initActiveSpi();
063: }
064: });
065:
066: // This class could not be instantiated.
067: private RMIClassLoader() {
068: }
069:
070: /**
071: * @com.intel.drl.spec_ref
072: */
073: public static Class<?> loadProxyClass(String codebase,
074: String[] interf, ClassLoader defaultCl)
075: throws ClassNotFoundException, MalformedURLException {
076: return activeSpi.loadProxyClass(codebase, interf, defaultCl);
077: }
078:
079: /**
080: * @com.intel.drl.spec_ref
081: */
082: public static Class<?> loadClass(String codebase, String name,
083: ClassLoader defaultCl) throws MalformedURLException,
084: ClassNotFoundException {
085: return activeSpi.loadClass(codebase, name, defaultCl);
086: }
087:
088: /**
089: * @com.intel.drl.spec_ref
090: */
091: public static Class<?> loadClass(URL codebase, String name)
092: throws MalformedURLException, ClassNotFoundException {
093: return activeSpi.loadClass((codebase == null) ? null : codebase
094: .toString(), name, null);
095: }
096:
097: /**
098: * @com.intel.drl.spec_ref
099: */
100: public static Class<?> loadClass(String codebase, String name)
101: throws MalformedURLException, ClassNotFoundException {
102: return activeSpi.loadClass(codebase, name, null);
103: }
104:
105: /**
106: * @com.intel.drl.spec_ref
107: */
108: public static String getClassAnnotation(Class<?> cl) {
109: return activeSpi.getClassAnnotation(cl);
110: }
111:
112: /**
113: * @com.intel.drl.spec_ref
114: * It's depricated so we just return null.
115: * @deprecated since Java v1.2 this method is no longer used by RMI
116: * framework
117: */
118: @Deprecated
119: public static Object getSecurityContext(ClassLoader loader) {
120: return null;
121: }
122:
123: /**
124: * @com.intel.drl.spec_ref
125: */
126: public static ClassLoader getClassLoader(String codebase)
127: throws MalformedURLException, SecurityException {
128: return activeSpi.getClassLoader(codebase);
129: }
130:
131: /**
132: * @com.intel.drl.spec_ref
133: * @deprecated method loadClass(String, String) should be used instead
134: */
135: @Deprecated
136: public static Class<?> loadClass(String name)
137: throws MalformedURLException, ClassNotFoundException {
138: return loadClass((String) null, name);
139: }
140:
141: /**
142: * @com.intel.drl.spec_ref
143: */
144: public static RMIClassLoaderSpi getDefaultProviderInstance() {
145: SecurityManager mgr = System.getSecurityManager();
146:
147: if (mgr != null) {
148: mgr.checkPermission(new RuntimePermission("setFactory")); //$NON-NLS-1$
149: }
150: return defaultSpi;
151: }
152:
153: /*
154: * Initialize active RMIClassLoaderSpi.
155: */
156: private static RMIClassLoaderSpi initActiveSpi() {
157: String spi = System.getProperty(spiProp);
158:
159: if (spi != null) {
160: if (spi.equals("default")) { //$NON-NLS-1$
161: return defaultSpi;
162: }
163:
164: try {
165: return (RMIClassLoaderSpi) (Class.forName(spi, false,
166: ClassLoader.getSystemClassLoader())
167: .newInstance());
168: } catch (Exception ex) {
169: // rmi.1B=Unable to initialize RMIClassLoaderSpi instance {0}, specified in {1} property
170: throw new Error(Messages.getString(
171: "rmi.1B", spi, spiProp), ex); //$NON-NLS-1$
172: }
173: }
174:
175: try {
176: spi = getSpiFromResource();
177: } catch (IOException ioe) {
178: // rmi.1C=Unable to get RMIClassLoaderSpi name from resource {0}
179: throw new Error(
180: Messages.getString("rmi.1C", spiResource), ioe); //$NON-NLS-1$
181: }
182:
183: if (spi != null) {
184: try {
185: return (RMIClassLoaderSpi) (Class.forName(spi, true,
186: ClassLoader.getSystemClassLoader())
187: .newInstance());
188: } catch (Exception ex) {
189: // rmi.1D=Unable to initialize RMIClassLoaderSpi instance {0}, specified in {1} resource
190: throw new Error(Messages.getString(
191: "rmi.1D", spi, spiResource), ex); //$NON-NLS-1$
192: }
193: }
194: return defaultSpi;
195: }
196:
197: /*
198: * Returns provider obtained from default resource.
199: *
200: * @return provider obtained from default resource
201: *
202: * @throws IOException if any I/O error occurred while trying to read
203: * provider name from default resource
204: */
205: private static String getSpiFromResource() throws IOException {
206: InputStream in = ClassLoader.getSystemClassLoader()
207: .getResourceAsStream(spiResource);
208:
209: if (in == null) {
210: // resource not found
211: return null;
212: }
213: Object obj = null;
214: byte[] buf = new byte[in.available()];
215: in.read(buf);
216: String str = new String(buf, "UTF-8"); //$NON-NLS-1$
217: StringTokenizer tok = new StringTokenizer(str, "\n\r"); //$NON-NLS-1$
218:
219: while (tok.hasMoreTokens()) {
220: String spiName = tok.nextToken();
221: int idx = spiName.indexOf("#"); //$NON-NLS-1$
222:
223: if (idx != -1) {
224: // this is commented line
225: spiName = spiName.substring(0, idx);
226: }
227: spiName = spiName.trim();
228:
229: if (spiName.length() > 0) {
230: // not empty line
231: return spiName;
232: }
233:
234: // skip empty line
235: }
236:
237: // we did not found any uncommented non-empty lines
238: return ""; //$NON-NLS-1$
239: }
240: }
|