1 28 29 package com.caucho.loader; 30 31 import com.caucho.make.DependencyContainer; 32 import com.caucho.util.CharBuffer; 33 import com.caucho.vfs.Dependency; 34 import com.caucho.vfs.JarPath; 35 import com.caucho.vfs.Path; 36 37 import javax.annotation.PostConstruct; 38 import java.io.IOException ; 39 import java.net.URL ; 40 import java.util.ArrayList ; 41 import java.util.HashSet ; 42 import java.util.Vector ; 43 import java.util.logging.Level ; 44 import java.util.logging.Logger ; 45 46 50 public class TreeLoader extends Loader implements Dependency 51 { 52 private static final Logger log 53 = Logger.getLogger(TreeLoader.class.getName()); 54 55 private Path _dir; 57 58 private long _lastModified; 60 61 private String []_fileNames; 62 63 private ArrayList <JarEntry> _jarList; 65 66 private HashSet <Path> _files = new HashSet <Path>(); 67 private HashSet <Path> _tempFiles = new HashSet <Path>(); 68 69 private DependencyContainer _dependencyList = new DependencyContainer(); 71 72 75 public TreeLoader() 76 { 77 } 78 79 82 public TreeLoader(Path dir) 83 { 84 _dir = dir; 85 86 init(); 87 } 88 89 92 public void setPath(Path path) 93 { 94 _dir = path; 95 } 96 97 100 public Path getPath() 101 { 102 return _dir; 103 } 104 105 111 public static DynamicClassLoader create(ClassLoader parent, Path dir) 112 { 113 DynamicClassLoader loader = new DynamicClassLoader(parent); 114 115 TreeLoader treeLoader = new TreeLoader(dir); 116 117 loader.addLoader(treeLoader); 118 119 loader.init(); 120 121 return loader; 122 } 123 124 127 @PostConstruct 128 public void init() 129 { 130 _lastModified = _dir.getLastModified(); 131 132 try { 133 _fileNames = _dir.list(); 134 } catch (IOException e) { 135 } 136 137 _jarList = new ArrayList <JarEntry>(); 138 _dependencyList = new DependencyContainer(); 139 140 fillJars(); 141 } 142 143 147 public boolean isModified() 148 { 149 return _dependencyList.isModified(); 150 } 151 152 155 public void setLoader(DynamicClassLoader loader) 156 { 157 super.setLoader(loader); 158 159 for (int i = 0; i < _jarList.size(); i++) 160 loader.addURL(_jarList.get(i).getJarPath()); 161 } 162 163 166 private void fillJars() 167 { 168 clearJars(); 169 170 fillJars(_dir); 171 } 172 173 176 private void fillJars(Path dir) 177 { 178 try { 179 String []list = dir.list(); 180 181 for (int j = 0; list != null && j < list.length; j++) { 182 Path path = dir.lookup(list[j]); 183 184 if (list[j].endsWith(".jar") || list[j].endsWith(".zip")) { 185 addJar(path); 186 } 187 else if (path.isDirectory()) { 188 fillJars(path); 189 } 190 } 191 192 } catch (IOException e) { 193 } 194 } 195 196 private void addJar(Path jar) 197 { 198 JarPath jarPath = JarPath.create(jar); 199 JarEntry jarEntry = new JarEntry(jarPath); 200 201 if (_jarList.contains(jarEntry)) 202 return; 203 204 _jarList.add(jarEntry); 205 _dependencyList.add(jarPath.getDepend()); 206 } 207 208 211 public void getResources(Vector <URL > vector, String name) 212 { 213 for (int i = 0; i < _jarList.size(); i++) { 214 JarEntry jarEntry = _jarList.get(i); 215 Path path = jarEntry.getJarPath(); 216 217 path = path.lookup(name); 218 219 if (path.canRead() || path.isDirectory()) { 220 try { 221 vector.add(new URL (path.getURL())); 222 } catch (Exception e) { 223 log.log(Level.WARNING, e.toString(), e); 224 } 225 } 226 } 227 } 228 229 233 protected String getClassPath(String head) 234 { 235 CharBuffer cb = new CharBuffer(); 236 237 cb.append(head); 238 239 for (int i = 0; i < _jarList.size(); i++) { 240 JarEntry jarEntry = _jarList.get(i); 241 JarPath jar = jarEntry.getJarPath(); 242 243 if (cb.length() > 0) 244 cb.append(Path.getPathSeparatorChar()); 245 cb.append(jar.getContainer().getNativePath()); 246 } 247 248 return cb.close(); 249 } 250 251 256 protected ClassEntry getClassEntry(String name) 257 throws ClassNotFoundException 258 { 259 String pathName = name.replace('.', '/'); 260 261 String pkg = ""; 262 int p = pathName.lastIndexOf('/'); 263 if (p > 0) 264 pkg = pathName.substring(0, p + 1); 265 266 pathName = pathName + ".class"; 267 268 Path classPath = null; 269 270 for (int i = 0; i < _jarList.size(); i++) { 272 JarEntry jarEntry = _jarList.get(i); 273 Path path = jarEntry.getJarPath(); 274 275 Path filePath = path.lookup(pathName); 276 277 if (filePath.canRead() && filePath.getLength() > 0) { 278 ClassEntry entry = new ClassEntry(getLoader(), name, filePath, 279 filePath, 280 jarEntry.getCodeSource(pathName)); 281 282 ClassPackage classPackage = jarEntry.getPackage(pkg); 283 284 entry.setClassPackage(classPackage); 285 286 return entry; 287 } 288 } 289 290 return null; 291 } 292 293 300 public Path getPath(String pathName) 301 { 302 for (int i = 0; i < _jarList.size(); i++) { 303 JarEntry jarEntry = _jarList.get(i); 304 Path path = jarEntry.getJarPath(); 305 306 Path filePath = path.lookup(pathName); 307 308 if (filePath.canRead()) 309 return filePath; 310 } 311 312 return null; 313 } 314 315 public Path getCodePath() 316 { 317 return _dir; 318 } 319 320 323 protected void destroy() 324 { 325 clearJars(); 326 } 327 328 331 private void clearJars() 332 { 333 ArrayList <JarEntry> jars = new ArrayList <JarEntry>(_jarList); 334 _jarList.clear(); 335 336 for (int i = 0; i < jars.size(); i++) { 337 JarEntry jarEntry = jars.get(i); 338 339 JarPath jarPath = jarEntry.getJarPath(); 340 341 jarPath.closeJar(); 342 } 343 } 344 345 public String toString() 346 { 347 return "TreeLoader[" + _dir + "]"; 348 } 349 } 350 | Popular Tags |