KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.loader;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.make.DependencyContainer;
33 import com.caucho.server.util.CauchoSystem;
34 import com.caucho.util.CharBuffer;
35 import com.caucho.vfs.Depend;
36 import com.caucho.vfs.Dependency;
37 import com.caucho.vfs.JarPath;
38 import com.caucho.vfs.Path;
39
40 import javax.annotation.PostConstruct;
41 import java.net.URL JavaDoc;
42 import java.util.ArrayList JavaDoc;
43 import java.util.Vector JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Class loader which checks for changes in class files and automatically
49  * picks up new jars.
50  */

51 public class JarLoader extends Loader implements Dependency {
52   private static final Logger JavaDoc log
53     = Logger.getLogger(JarLoader.class.getName());
54
55   // list of the jars in the directory
56
private ArrayList JavaDoc<JarEntry> _jarList = new ArrayList JavaDoc<JarEntry>();
57   
58   // list of dependencies
59
private DependencyContainer _dependencyList = new DependencyContainer();
60
61   /**
62    * Creates a new directory loader.
63    */

64   public JarLoader()
65   {
66   }
67
68   /**
69    * Initialize
70    */

71   @PostConstruct
72   public void init()
73   {
74   }
75
76   /**
77    * Sets the owning class loader.
78    */

79   public void setLoader(DynamicClassLoader loader)
80   {
81     super.setLoader(loader);
82
83     for (int i = 0; i < _jarList.size(); i++)
84       loader.addURL(_jarList.get(i).getJarPath());
85   }
86
87   /**
88    * Validates the loader.
89    */

90   public void validate()
91     throws ConfigException
92   {
93     for (int i = 0; i < _jarList.size(); i++) {
94       _jarList.get(i).validate();
95     }
96   }
97   
98   /**
99    * True if any of the loaded classes have been modified. If true, the
100    * caller should drop the classpath and create a new one.
101    */

102   public boolean isModified()
103   {
104     return _dependencyList.isModified();
105   }
106
107   /**
108    * Adds a new jar.
109    */

110   public void addJar(Path jar)
111   {
112     JarPath jarPath = JarPath.create(jar);
113     JarEntry jarEntry = new JarEntry(jarPath);
114
115     if (_jarList.contains(jarEntry))
116       return;
117     
118     _jarList.add(jarEntry);
119
120     _dependencyList.add(new Depend(jarPath));
121
122     if (getLoader() != null)
123       getLoader().addURL(jarPath);
124   }
125
126   /**
127    * Fill data for the class path. fillClassPath() will add all
128    * .jar and .zip files in the directory list.
129    */

130   protected String JavaDoc getClassPath(String JavaDoc head)
131   {
132     CharBuffer cb = new CharBuffer();
133
134     cb.append(head);
135
136     for (int i = 0; i < _jarList.size(); i++) {
137       JarEntry jarEntry = _jarList.get(i);
138       JarPath jar = jarEntry.getJarPath();
139
140       if (cb.length() > 0)
141         cb.append(CauchoSystem.getPathSeparatorChar());
142       cb.append(jar.getContainer().getNativePath());
143     }
144
145     return cb.close();
146   }
147
148   /**
149    * Returns the class entry.
150    *
151    * @param name name of the class
152    */

153   protected ClassEntry getClassEntry(String JavaDoc name)
154     throws ClassNotFoundException JavaDoc
155   {
156     String JavaDoc pathName = name.replace('.', '/');
157     
158     String JavaDoc pkg = "";
159     int p = pathName.lastIndexOf('/');
160     if (p > 0)
161       pkg = pathName.substring(0, p + 1);
162          
163     pathName = pathName + ".class";
164
165     Path classPath = null;
166     
167     // Find the path corresponding to the class
168
for (int i = 0; i < _jarList.size(); i++) {
169       JarEntry jarEntry = _jarList.get(i);
170       Path path = jarEntry.getJarPath();
171
172       Path filePath = path.lookup(pathName);
173       
174       if (filePath.canRead() && filePath.getLength() > 0) {
175         ClassEntry entry = new ClassEntry(getLoader(), name, filePath,
176                                           filePath, getCodeSource(filePath));
177
178         ClassPackage classPackage = jarEntry.getPackage(pkg);
179
180         entry.setClassPackage(classPackage);
181
182         return entry;
183       }
184     }
185
186     return null;
187   }
188   
189   /**
190    * Adds resources to the enumeration.
191    */

192   public void getResources(Vector JavaDoc<URL JavaDoc> vector, String JavaDoc name)
193   {
194     for (int i = 0; i < _jarList.size(); i++) {
195       JarEntry jarEntry = _jarList.get(i);
196       Path path = jarEntry.getJarPath();
197
198       path = path.lookup(name);
199
200       if (path.canRead()) {
201     try {
202       vector.add(new URL JavaDoc(path.getURL()));
203     } catch (Exception JavaDoc e) {
204       log.log(Level.WARNING, e.toString(), e);
205     }
206       }
207     }
208   }
209
210   /**
211    * Find a given path somewhere in the classpath
212    *
213    * @param pathName the relative resourceName
214    *
215    * @return the matching path or null
216    */

217   public Path getPath(String JavaDoc pathName)
218   {
219     for (int i = 0; i < _jarList.size(); i++) {
220       JarEntry jarEntry = _jarList.get(i);
221       Path path = jarEntry.getJarPath();
222
223       Path filePath = path.lookup(pathName);
224
225       if (filePath.canRead())
226     return filePath;
227     }
228
229     return null;
230   }
231
232   public Path getCodePath()
233   {
234     return null;
235   }
236
237   public String JavaDoc toString()
238   {
239     return "JarLoader[]";
240   }
241 }
242
Popular Tags