KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > SimpleLoader


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free SoftwareFoundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.loader;
31
32 import com.caucho.config.ConfigException;
33 import com.caucho.log.Log;
34 import com.caucho.util.CharBuffer;
35 import com.caucho.vfs.JarPath;
36 import com.caucho.vfs.Path;
37
38 import javax.annotation.PostConstruct;
39 import java.net.URL JavaDoc;
40 import java.security.CodeSource JavaDoc;
41 import java.security.cert.Certificate JavaDoc;
42 import java.util.logging.Level JavaDoc;
43 import java.util.logging.Logger JavaDoc;
44
45 /**
46  * Class loader which checks for changes in class files and automatically
47  * picks up new jars.
48  */

49 public class SimpleLoader extends Loader {
50   private static final Logger JavaDoc log = Log.open(SimpleLoader.class);
51   
52   // The class directory
53
private Path _path;
54   private String JavaDoc _prefix;
55   private String JavaDoc _pathPrefix;
56
57   private CodeSource JavaDoc _codeSource;
58
59   /**
60    * Null constructor for the simple loader.
61    */

62   public SimpleLoader()
63   {
64   }
65
66   /**
67    * Creates the simple loader with the specified path.
68    *
69    * @param path specifying the root of the resources
70    */

71   public SimpleLoader(Path path)
72   {
73     setPath(path);
74   }
75
76   /**
77    * Creates the simple loader with the specified path and prefix.
78    *
79    * @param path specifying the root of the resources
80    * @param prefix the prefix that the resources must match
81    */

82   public SimpleLoader(Path path, String JavaDoc prefix)
83   {
84     setPath(path);
85     setPrefix(prefix);
86   }
87
88   /**
89    * Create a class loader based on the SimpleLoader
90    *
91    * @param parent parent class loader
92    * @param path traditional classpath
93    * @param prefix the class prefix restriction
94    *
95    * @return the new ClassLoader
96    */

97   public static DynamicClassLoader create(ClassLoader JavaDoc parent,
98                                           Path path,
99                                           String JavaDoc prefix)
100   {
101     DynamicClassLoader loader = new DynamicClassLoader(parent, false);
102
103     loader.addLoader(new SimpleLoader(path, prefix));
104
105     loader.init();
106
107     return loader;
108   }
109
110   /**
111    * Create a class loader based on the SimpleLoader
112    *
113    * @param parent parent class loader
114    * @param path traditional classpath
115    *
116    * @return the new ClassLoader
117    */

118   public static DynamicClassLoader create(ClassLoader JavaDoc parent,
119                                           Path path)
120   {
121     DynamicClassLoader loader = new DynamicClassLoader(parent, false);
122
123     loader.addLoader(new SimpleLoader(path));
124
125     loader.init();
126     
127     return loader;
128   }
129
130   /**
131    * Create a class loader based on the SimpleLoader
132    *
133    * @param path traditional classpath
134    *
135    * @return the new ClassLoader
136    */

137   public static DynamicClassLoader create(Path path)
138   {
139     ClassLoader JavaDoc parent = Thread.currentThread().getContextClassLoader();
140
141     return create(parent, path);
142   }
143
144   /**
145    * Sets the resource directory.
146    */

147   public void setPath(Path path)
148   {
149     if (path.getPath().endsWith(".jar") ||
150         path.getPath().endsWith(".zip"))
151       path = JarPath.create(path);
152
153     _path = path;
154   }
155
156   /**
157    * Gets the resource path.
158    */

159   public Path getPath()
160   {
161     return _path;
162   }
163
164   /**
165    * Sets the resource prefix
166    */

167   public void setPrefix(String JavaDoc prefix)
168   {
169     _prefix = prefix;
170     
171     if (prefix != null)
172       _pathPrefix = prefix.replace('.', '/');
173   }
174
175   /**
176    * Gets the resource prefix
177    */

178   public String JavaDoc getPrefix()
179   {
180     return _prefix;
181   }
182
183   /**
184    * Sets the owning class loader.
185    */

186   public void setLoader(DynamicClassLoader loader)
187   {
188     super.setLoader(loader);
189
190     loader.addURL(_path);
191   }
192
193   /**
194    * Initializes the loader.
195    */

196   @PostConstruct
197   public void init()
198     throws ConfigException
199   {
200     try {
201       _codeSource = new CodeSource JavaDoc(new URL JavaDoc(_path.getURL()), (Certificate JavaDoc []) null);
202     } catch (Exception JavaDoc e) {
203       log.log(Level.FINE, e.toString(), e);
204     }
205   }
206
207   /**
208    * Given a class or resource name, returns a patch to that resource.
209    *
210    * @param name the class or resource name.
211    *
212    * @return the path representing the class or resource.
213    */

214   public Path getPath(String JavaDoc name)
215   {
216     if (_prefix != null && _pathPrefix == null)
217       _pathPrefix = _prefix.replace('.', '/');
218
219     if (_pathPrefix != null && ! name.startsWith(_pathPrefix))
220       return null;
221
222     return _path.lookup(name);
223   }
224
225   /**
226    * Returns the code source for the directory.
227    */

228   protected CodeSource JavaDoc getCodeSource(Path path)
229   {
230     return _codeSource;
231   }
232
233   /**
234    * Adds the class of this resource.
235    */

236   protected String JavaDoc getClassPath(String JavaDoc head)
237   {
238     CharBuffer cb = new CharBuffer();
239
240     if (! head.equals("")) {
241       cb.append(head);
242       cb.append(Path.getPathSeparatorChar());
243     }
244     
245     if (_path instanceof JarPath)
246       cb.append(((JarPath) _path).getContainer().getNativePath());
247     else if (_path.isDirectory())
248       cb.append(_path.getNativePath());
249
250     return cb.toString();
251   }
252
253   /**
254    * Returns a printable representation of the loader.
255    */

256   public String JavaDoc toString()
257   {
258     if (_prefix != null)
259       return "SimpleLoader[" + _path + ",prefix=" + _prefix + "]";
260     else
261       return "SimpleLoader[" + _path + "]";
262   }
263 }
264
Popular Tags