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.*;
032: import com.caucho.make.DependencyContainer;
033: import com.caucho.util.*;
034: import com.caucho.vfs.Dependency;
035: import com.caucho.vfs.JarPath;
036: import com.caucho.vfs.Path;
037:
038: import javax.annotation.PostConstruct;
039: import java.io.IOException;
040: import java.net.URL;
041: import java.util.ArrayList;
042: import java.util.HashSet;
043: import java.util.Vector;
044: import java.util.logging.Level;
045: import java.util.logging.Logger;
046:
047: /**
048: * Class loader which checks for changes in class files and automatically
049: * picks up new jars.
050: */
051: public class TreeLoader extends JarListLoader implements Dependency {
052: private static final L10N L = new L10N(TreeLoader.class);
053:
054: private static final Logger log = Logger.getLogger(TreeLoader.class
055: .getName());
056:
057: // Directory which may have jars dynamically added
058: private Path _dir;
059:
060: // When the directory was last modified
061: private long _lastModified;
062:
063: private String[] _fileNames;
064:
065: private HashSet<Path> _files = new HashSet<Path>();
066: private HashSet<Path> _tempFiles = new HashSet<Path>();
067:
068: /**
069: * Creates a new directory loader.
070: */
071: public TreeLoader() {
072: }
073:
074: /**
075: * Creates a new directory loader.
076: */
077: public TreeLoader(Path dir) {
078: _dir = dir;
079:
080: init();
081: }
082:
083: /**
084: * The directory loader's path.
085: */
086: public void setPath(Path path) {
087: _dir = path;
088: }
089:
090: /**
091: * The directory loader's path.
092: */
093: public Path getPath() {
094: return _dir;
095: }
096:
097: /**
098: * Create a new class loader
099: *
100: * @param parent parent class loader
101: * @param dir directories which can handle dynamic jar addition
102: */
103: public static DynamicClassLoader create(ClassLoader parent, Path dir) {
104: DynamicClassLoader loader = new DynamicClassLoader(parent);
105:
106: TreeLoader treeLoader = new TreeLoader(dir);
107:
108: loader.addLoader(treeLoader);
109:
110: loader.init();
111:
112: return loader;
113: }
114:
115: /**
116: * Initialize
117: */
118: @PostConstruct
119: @Override
120: public void init() {
121: super .init();
122:
123: if (_dir == null)
124: throw new ConfigException(L
125: .l("<tree-loader> requires a 'path' attribute"));
126:
127: _lastModified = _dir.getLastModified();
128:
129: try {
130: _fileNames = _dir.list();
131: } catch (IOException e) {
132: }
133:
134: fillJars();
135: }
136:
137: /**
138: * True if the classes in the directory have changed.
139: */
140: public boolean logModified(Logger log) {
141: if (isModified()) {
142: log.info(_dir.getNativePath() + " has modified jar files");
143: return true;
144: } else
145: return false;
146: }
147:
148: /**
149: * Sets the owning class loader.
150: */
151: public void setLoader(DynamicClassLoader loader) {
152: super .setLoader(loader);
153:
154: for (int i = 0; i < _jarList.size(); i++)
155: loader.addURL(_jarList.get(i).getJarPath());
156: }
157:
158: /**
159: * Find all the jars in this directory and add them to jarList.
160: */
161: private void fillJars() {
162: clearJars();
163:
164: fillJars(_dir);
165: }
166:
167: /**
168: * Find all the jars in this directory and add them to jarList.
169: */
170: private void fillJars(Path dir) {
171: try {
172: String[] list = dir.list();
173:
174: for (int j = 0; list != null && j < list.length; j++) {
175: Path path = dir.lookup(list[j]);
176:
177: if (list[j].endsWith(".jar")
178: || list[j].endsWith(".zip")) {
179: addJar(path);
180: } else if (path.isDirectory()) {
181: fillJars(path);
182: }
183: }
184:
185: } catch (IOException e) {
186: }
187: }
188:
189: public Path getCodePath() {
190: return _dir;
191: }
192:
193: /**
194: * Destroys the loader, closing the jars.
195: */
196: protected void destroy() {
197: clearJars();
198: }
199:
200: public String toString() {
201: return "TreeLoader[" + _dir + "]";
202: }
203: }
|