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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.loader;
030:
031: import com.caucho.config.ConfigException;
032: import com.caucho.log.Log;
033: import com.caucho.vfs.Path;
034: import com.caucho.server.util.*;
035:
036: import java.io.InputStream;
037: import java.net.URL;
038: import java.security.CodeSource;
039: import java.security.cert.Certificate;
040: import java.util.*;
041: import java.util.logging.Level;
042: import java.util.logging.Logger;
043:
044: /**
045: * Loads resources.
046: */
047: abstract public class Loader {
048: protected static final Logger log = Log.open(Loader.class);
049:
050: private DynamicClassLoader _loader;
051:
052: /**
053: * Sets the owning class loader.
054: */
055: public void setLoader(DynamicClassLoader loader) {
056: _loader = loader;
057: }
058:
059: /**
060: * Gets the owning class loader.
061: */
062: public DynamicClassLoader getLoader() {
063: return _loader;
064: }
065:
066: /**
067: * Validates the loader.
068: */
069: public void validate() throws ConfigException {
070: }
071:
072: /**
073: * Returns the class entry.
074: *
075: * @param name name of the class
076: */
077: protected ClassEntry getClassEntry(String name, String pathName)
078: throws ClassNotFoundException {
079: // Find the path corresponding to the class
080: Path path = getPath(pathName);
081:
082: if (path != null && path.getLength() > 0)
083: return new ClassEntry(_loader, name, path, path,
084: getCodeSource(path));
085: else
086: return null;
087: }
088:
089: /**
090: * Returns the resource
091: *
092: * @param name name of the resource
093: */
094: public URL getResource(String name) {
095: Path path;
096:
097: path = getPath(name);
098:
099: if (path != null && path.exists()) {
100: try {
101: return new URL(path.getURL());
102: } catch (Exception e) {
103: log.log(Level.FINER, e.toString(), e);
104: }
105: }
106:
107: return null;
108: }
109:
110: /**
111: * Returns the resource
112: *
113: * @param name name of the resource
114: */
115: public void getResources(Vector<URL> resources, String name) {
116: Path path;
117:
118: path = getPath(name);
119:
120: if (path != null && path.canRead()) {
121: try {
122: resources.add(new URL(path.getURL()));
123: } catch (Exception e) {
124: }
125: }
126: }
127:
128: /**
129: * Opens the stream to the resource.
130: *
131: * @param name name of the resource
132: */
133: public InputStream getResourceAsStream(String name) {
134: Path path;
135:
136: path = getPath(name);
137:
138: if (path != null && path.canRead()) {
139: try {
140: return path.openRead();
141: } catch (Exception e) {
142: }
143: }
144:
145: return null;
146: }
147:
148: /**
149: * Returns a path for the given name.
150: */
151: public Path getPath(String name) {
152: return null;
153: }
154:
155: /**
156: * Returns the code source for the path.
157: */
158: protected CodeSource getCodeSource(Path path) {
159: try {
160: return new CodeSource(new URL(path.getURL()),
161: (Certificate[]) path.getCertificates());
162: } catch (Exception e) {
163: log.log(Level.WARNING, e.toString(), e);
164:
165: return null;
166: }
167: }
168:
169: /**
170: * Adds the classpath of this loader.
171: */
172: protected final void buildClassPath(StringBuilder head) {
173: ArrayList<String> pathList = new ArrayList<String>();
174: buildClassPath(pathList);
175:
176: for (int i = 0; i < pathList.size(); i++) {
177: if (head.length() > 0)
178: head.append(CauchoSystem.getPathSeparatorChar());
179: head.append(pathList.get(i));
180: }
181: }
182:
183: /**
184: * Adds the sourcepath of this loader.
185: */
186: protected void buildClassPath(ArrayList<String> pathList) {
187: }
188:
189: /**
190: * Adds the sourcepath of this loader.
191: */
192: protected void buildSourcePath(StringBuilder head) {
193: buildClassPath(head);
194: }
195:
196: /**
197: * Adds the sourcepath of this loader.
198: */
199: protected void buildSourcePath(ArrayList<String> pathList) {
200: buildClassPath(pathList);
201: }
202:
203: /**
204: * Destroys the loader.
205: */
206: protected void destroy() {
207: }
208: }
|