001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.loader;
031:
032: import com.caucho.config.Config;
033: import com.caucho.config.ConfigException;
034: import com.caucho.vfs.Path;
035: import com.caucho.vfs.Vfs;
036:
037: import java.io.File;
038: import java.io.IOException;
039: import java.net.URLClassLoader;
040:
041: /**
042: * ClassLoader that initalizes the environment and allows byte code
043: * enhancement of classes in the system classpath.
044: * <pre>
045: * java -Djava.system.class.loader=com.caucho.loader.SystemClassLoader ...
046: * </pre>
047: * If the system property "system.conf" is defined, it is used as a path
048: * to a configuration file that initializes the enviornment. Relative paths
049: * are relative to the current directory (See {@link com.caucho.vfs.Vfs#getPwd()}.
050: * <p/>
051: * Resources defined in system.conf are available to all classes loaded within the jvm.
052: * <pre>
053: * java -Dsystem.conf=tests/system.conf -Djava.system.class.loader=com.caucho.loader.SystemClassLoader ...
054: * </pre>
055: */
056: public class SystemClassLoader extends EnvironmentClassLoader implements
057: EnvironmentBean {
058: private boolean _isInit;
059: private boolean _hasBootClassPath;
060:
061: private URLClassLoader _loader;
062:
063: /**
064: * Creates a new SystemClassLoader.
065: */
066: public SystemClassLoader(ClassLoader parent) {
067: super (parent);
068:
069: setId("system");
070:
071: DynamicClassLoader.setJarCacheEnabled(true);
072: }
073:
074: public ClassLoader getClassLoader() {
075: return this ;
076: }
077:
078: public void init() {
079: if (_isInit)
080: return;
081:
082: _isInit = true;
083:
084: initClasspath();
085:
086: super .init();
087:
088: String systemConf = System.getProperty("system.conf");
089:
090: if (systemConf != null) {
091: try {
092: Path path = Vfs.lookup(systemConf);
093:
094: Config config = new Config();
095:
096: config.configure(this , path, getSchema());
097: } catch (Exception ex) {
098: ex.printStackTrace();
099:
100: throw new RuntimeException(ex.toString());
101: }
102: }
103: }
104:
105: private void initClasspath() {
106: boolean isValid = false;
107:
108: try {
109: String boot = System.getProperty("sun.boot.class.path");
110: if (boot != null) {
111: initClasspath(boot);
112: _hasBootClassPath = true;
113:
114: initExtDirs("java.ext.dirs");
115: initExtDirs("java.endorsed.dirs");
116: }
117:
118: initClasspath(System.getProperty("java.class.path"));
119:
120: isValid = true;
121: } catch (IOException e) {
122: e.printStackTrace();
123: } finally {
124: if (!isValid)
125: _hasBootClassPath = false;
126: }
127: }
128:
129: private void initExtDirs(String prop) throws IOException {
130: String extDirPath = System.getProperty(prop);
131:
132: if (extDirPath == null)
133: return;
134:
135: for (String extDir : extDirPath.split(File.pathSeparator, 512)) {
136: Path dir = Vfs.lookup(extDir);
137:
138: for (String fileName : dir.list()) {
139: Path root = dir.lookup(fileName);
140:
141: try {
142: if (root.exists())
143: addRoot(root);
144: } catch (Throwable e) {
145: _hasBootClassPath = false;
146: e.printStackTrace();
147: }
148: }
149: }
150: }
151:
152: private void initClasspath(String classpath) {
153: String[] classpathElements = classpath.split(
154: File.pathSeparator, 512);
155:
156: for (String classpathElement : classpathElements) {
157: Path root = Vfs.lookup(classpathElement);
158:
159: try {
160: if (root.exists())
161: addRoot(root);
162: } catch (Throwable e) {
163: _hasBootClassPath = false;
164: e.printStackTrace();
165: }
166: }
167: }
168:
169: @Override
170: protected void initEnvironment() {
171: // disable for terracotta
172: }
173:
174: /**
175: * Load a class using this class loader
176: *
177: * @param name the classname to load
178: * @param resolve if true, resolve the class
179: *
180: * @return the loaded classes
181: */
182:
183: protected Class loadClassImpl(String name, boolean resolve)
184: throws ClassNotFoundException {
185: // The JVM has already cached the classes, so we don't need to
186: Class cl = findLoadedClass(name);
187:
188: if (cl != null) {
189: if (resolve)
190: resolveClass(cl);
191: return cl;
192: }
193:
194: // This causes problems with JCE
195: if (_hasBootClassPath) {
196: String className = name.replace('.', '/') + ".class";
197:
198: if (findPath(className) == null)
199: return null;
200: }
201:
202: return super .loadClassImpl(name, resolve);
203: }
204:
205: protected String getSchema() {
206: return "com/caucho/loader/system.rnc";
207: }
208:
209: public String toString() {
210: return "SystemClassLoader[]";
211: }
212: }
|