001: /*
002: * @(#)MIDPImplementationClassLoader.java 1.13 06/11/07
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027: /*
028: * @(#)MIDPImplementationClassLoader.java 1.4 03/08/19
029: *
030: * Class loader for MIDP implementation running on CDC/PP
031: */
032: package sun.misc;
033:
034: import java.net.URL;
035: import java.net.URLConnection;
036: import java.net.URLClassLoader;
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.util.HashSet;
040: import java.security.CodeSource;
041: import java.security.PermissionCollection;
042: import java.security.ProtectionDomain;
043: import java.security.AccessController;
044: import java.security.AccessControlContext;
045: import java.security.PrivilegedExceptionAction;
046: import java.security.cert.Certificate;
047:
048: public class MIDPImplementationClassLoader extends URLClassLoader {
049:
050: URL myBase[];
051: private HashSet allowedClasses; /* the classes we can lookup in system */
052: private PermissionCollection perms;
053: private ClassLoader parent;
054:
055: public MIDPImplementationClassLoader(URL base[],
056: String allowedParentClasses[], PermissionCollection pc,
057: ClassLoader parent) {
058: super (base);
059: myBase = base;
060: perms = pc;
061: this .parent = parent;
062: hashAllowedParentClasses(allowedParentClasses);
063: // Register in case classes were preloaded
064: CVM.Preloader.registerClassLoader("midp", this );
065: }
066:
067: private void hashAllowedParentClasses(String allowedParentClasses[]) {
068: HashSet classes = allowedClasses = new HashSet();
069: for (int i = 0; i < allowedParentClasses.length; i++) {
070: String classname = allowedParentClasses[i].intern();
071: classes.add(classname);
072: }
073: }
074:
075: protected PermissionCollection getPermissions(CodeSource cs) {
076: URL srcLocation = cs.getLocation();
077: for (int i = 0; i < myBase.length; i++) {
078: if (srcLocation.equals(myBase[i])) {
079: return perms;
080: }
081: }
082: return super .getPermissions(cs);
083: }
084:
085: private Class loadFromParent(String classname, boolean resolve,
086: boolean restrict) throws ClassNotFoundException {
087: // make sure classname is on the list.
088: if (restrict && !allowedClasses.contains(classname)) {
089: return null;
090: }
091:
092: /* Call java.lang.ClassLoader(classname, resolve),
093: * which uses the NULL classLoader to load
094: * class if parent is null, or calls parent.loadClass()
095: * when the parent is not null. */
096: return super .loadClass(classname, resolve);
097: }
098:
099: public Class loadClass(String classname, boolean resolve)
100: throws ClassNotFoundException {
101: return loadClass(classname, resolve, false);
102: }
103:
104: public synchronized Class loadClass(String classname,
105: boolean resolve, boolean restrict)
106: throws ClassNotFoundException {
107: Class resultClass;
108: classname = classname.intern();
109: resultClass = findLoadedClass(classname);
110: if (resultClass == null) {
111: try {
112: resultClass = loadFromParent(classname, resolve,
113: restrict);
114: } catch (Exception e) {
115: /*DEBUG e.printStackTrace(); */
116: resultClass = null;
117: }
118: }
119:
120: if (resultClass == null) {
121: try {
122: resultClass = super .findClass(classname); // from URLClassLoader
123: } catch (Exception e) {
124: /*DEBUG e.printStackTrace(); */
125: resultClass = null;
126: }
127: }
128: if (resultClass == null)
129: throw new ClassNotFoundException(classname);
130: if (resolve) {
131: resolveClass(resultClass);
132: }
133: /*DEBUG if(helperClass){
134: * System.out.println("returning "+classname+"...");
135: * System.out.println(resultClass);
136: * }
137: */
138: return resultClass;
139: }
140:
141: /*
142: InputStream getResourceAsStream(String name)
143: */
144:
145: }
|