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.boot;
031:
032: import com.caucho.management.server.*;
033: import com.caucho.vfs.*;
034:
035: import java.io.*;
036: import java.security.*;
037: import java.lang.instrument.*;
038:
039: /**
040: * Class loader which checks for changes in class files and automatically
041: * picks up new jars.
042: *
043: * <p>DynamicClassLoaders can be chained creating one virtual class loader.
044: * From the perspective of the JDK, it's all one classloader. Internally,
045: * the class loader chain searches like a classpath.
046: */
047: public class ProLoader extends SecureClassLoader {
048: private Path _resinHome;
049: private Path _libexec;
050: private JarPath _proJar;
051: private JarPath _licenseJar;
052:
053: /**
054: * Create a new class loader.
055: *
056: * @param parent parent class loader
057: */
058: private ProLoader(Path resinHome) {
059: super (ClassLoader.getSystemClassLoader());
060:
061: _resinHome = resinHome;
062:
063: boolean is64bit = "64".equals(System
064: .getProperty("sun.arch.data.model"));
065:
066: if (is64bit)
067: _libexec = _resinHome.lookup("libexec64");
068: else
069: _libexec = _resinHome.lookup("libexec");
070:
071: _proJar = JarPath.create(_resinHome.lookup("lib/pro.jar"));
072: _licenseJar = JarPath.create(_resinHome
073: .lookup("lib/license.jar"));
074: }
075:
076: static ProLoader create(Path resinHome) {
077: if (resinHome.lookup("lib/pro.jar").canRead())
078: return new ProLoader(resinHome);
079: else
080: return null;
081: }
082:
083: /**
084: * Load a class using this class loader
085: *
086: * @param name the classname to load
087: * @param resolve if true, resolve the class
088: *
089: * @return the loaded classes
090: */
091: // XXX: added synchronized for RSN-373
092: protected synchronized Class loadClass(String name, boolean resolve)
093: throws ClassNotFoundException {
094: String className = name.replace('.', '/') + ".class";
095:
096: Path path = _proJar.lookup(className);
097:
098: if (path.getLength() < 0)
099: path = _licenseJar.lookup(className);
100:
101: int length = (int) path.getLength();
102:
103: if (length > 0) {
104: byte[] buffer = new byte[length];
105:
106: try {
107: ReadStream is = null;
108: try {
109: is = path.openRead();
110:
111: is.readAll(buffer, 0, buffer.length);
112:
113: Class cl = defineClass(name, buffer, 0,
114: buffer.length, (CodeSource) null);
115:
116: return cl;
117: } finally {
118: is.close();
119: }
120: } catch (IOException e) {
121: throw new RuntimeException(e);
122: }
123: }
124:
125: return super .loadClass(name, resolve);
126: }
127:
128: /**
129: * Load a class using this class loader
130: *
131: * @param name the classname using either '/' or '.'
132: *
133: * @return the loaded class
134: */
135: protected Class findClass(String name)
136: throws ClassNotFoundException {
137: return super .findClass(name);
138: }
139:
140: /**
141: * Returns the full library path for the name.
142: */
143: public String findLibrary(String name) {
144: Path path = _libexec.lookup("lib" + name + ".so");
145:
146: if (path.canRead()) {
147: return path.getNativePath();
148: }
149:
150: path = _libexec.lookup("lib" + name + ".jnilib");
151:
152: if (path.canRead()) {
153: return path.getNativePath();
154: }
155:
156: return super.findLibrary(name);
157: }
158: }
|