1 28 29 package com.caucho.loader; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.config.types.FileSetType; 33 import com.caucho.config.types.PathPatternType; 34 import com.caucho.make.DependencyContainer; 35 import com.caucho.server.util.CauchoSystem; 36 import com.caucho.util.CharBuffer; 37 import com.caucho.vfs.Depend; 38 import com.caucho.vfs.Dependency; 39 import com.caucho.vfs.JarPath; 40 import com.caucho.vfs.Path; 41 42 import javax.annotation.PostConstruct; 43 import java.net.URL ; 44 import java.util.ArrayList ; 45 import java.util.Vector ; 46 import java.util.logging.Level ; 47 import java.util.logging.Logger ; 48 49 53 public class LibraryLoader extends Loader implements Dependency { 54 private static final Logger log 55 = Logger.getLogger(DirectoryLoader.class.getName()); 56 57 private Path _path; 59 60 private FileSetType _fileSet; 61 62 private long _lastModified; 64 65 private String []_fileNames; 66 67 private ArrayList <Path> _pathList = new ArrayList <Path>(); 69 70 private ArrayList <Path> _newPathList = new ArrayList <Path>(); 72 73 private ArrayList <JarEntry> _jarList; 75 76 private DependencyContainer _dependencyList = new DependencyContainer(); 78 79 82 public LibraryLoader() 83 { 84 } 85 86 89 public LibraryLoader(Path path) 90 { 91 _path = path; 92 93 try { 94 init(); 95 } catch (Exception e) { 96 throw new RuntimeException (e); 97 } 98 } 99 100 103 public void setPath(Path path) 104 { 105 _path = path; 106 } 107 108 111 public Path getPath() 112 { 113 return _path; 114 } 115 116 119 public void setFileSet(FileSetType fileSet) 120 { 121 _fileSet = fileSet; 122 } 123 124 130 public static DynamicClassLoader create(ClassLoader parent, Path path) 131 { 132 DynamicClassLoader loader = new DynamicClassLoader(parent); 133 134 LibraryLoader dirLoader = new LibraryLoader(path); 135 136 loader.addLoader(dirLoader); 137 138 loader.init(); 139 140 return loader; 141 } 142 143 146 @PostConstruct 147 public void init() 148 throws ConfigException 149 { 150 try { 151 if (_fileSet != null) { 152 } 153 else if (_path.getPath().endsWith(".jar") || 154 _path.getPath().endsWith(".zip")) { 155 _fileSet = new FileSetType(); 156 _fileSet.setDir(_path.getParent()); 157 _fileSet.addInclude(new PathPatternType(_path.getTail())); 158 } 159 else { 160 _fileSet = new FileSetType(); 161 _fileSet.setDir(_path); 162 _fileSet.addInclude(new PathPatternType("*.jar")); 163 _fileSet.addInclude(new PathPatternType("*.zip")); 164 } 165 166 _jarList = new ArrayList <JarEntry>(); 167 _dependencyList = new DependencyContainer(); 168 169 fillJars(); 170 } catch (ConfigException e) { 171 throw e; 172 } catch (Exception e) { 173 throw new ConfigException(e); 174 } 175 } 176 177 180 public void setLoader(DynamicClassLoader loader) 181 { 182 super.setLoader(loader); 183 184 for (int i = 0; i < _jarList.size(); i++) 185 loader.addURL(_jarList.get(i).getJarPath()); 186 } 187 188 191 public void validate() 192 throws ConfigException 193 { 194 for (int i = 0; i < _jarList.size(); i++) { 195 _jarList.get(i).validate(); 196 } 197 } 198 199 203 public boolean isModified() 204 { 205 _newPathList.clear(); 206 207 _fileSet.getPaths(_newPathList); 208 209 return ! _newPathList.equals(_pathList); 210 } 211 212 215 private void fillJars() 216 { 217 _pathList.clear(); 218 _jarList.clear(); 219 220 _fileSet.getPaths(_pathList); 221 222 for (int i = 0; i < _pathList.size(); i++) { 223 Path jar = _pathList.get(i); 224 225 addJar(jar); 226 } 227 } 228 229 private void addJar(Path jar) 230 { 231 JarPath jarPath = JarPath.create(jar); 232 _jarList.add(new JarEntry(jarPath)); 233 234 _dependencyList.add(new Depend(jarPath)); 235 236 if (getLoader() != null) 237 getLoader().addURL(jarPath); 238 } 239 240 244 protected String getClassPath(String head) 245 { 246 CharBuffer cb = new CharBuffer(); 247 248 cb.append(head); 249 250 for (int i = 0; i < _jarList.size(); i++) { 251 JarEntry jarEntry = _jarList.get(i); 252 JarPath jar = jarEntry.getJarPath(); 253 254 if (cb.length() > 0) 255 cb.append(CauchoSystem.getPathSeparatorChar()); 256 cb.append(jar.getContainer().getNativePath()); 257 } 258 259 return cb.close(); 260 } 261 262 267 protected ClassEntry getClassEntry(String name) 268 throws ClassNotFoundException 269 { 270 String pathName = name.replace('.', '/'); 271 272 String pkg = ""; 273 int p = pathName.lastIndexOf('/'); 274 if (p > 0) 275 pkg = pathName.substring(0, p + 1); 276 277 pathName = pathName + ".class"; 278 279 Path classPath = null; 280 281 for (int i = 0; i < _jarList.size(); i++) { 283 JarEntry jarEntry = _jarList.get(i); 284 Path path = jarEntry.getJarPath(); 285 286 Path filePath = path.lookup(pathName); 287 288 if (filePath.canRead() && filePath.getLength() > 0) { 289 ClassEntry entry = new ClassEntry(getLoader(), name, filePath, 290 filePath, 291 jarEntry.getCodeSource(pathName)); 292 293 ClassPackage classPackage = jarEntry.getPackage(pkg); 294 295 entry.setClassPackage(classPackage); 296 297 return entry; 298 } 299 } 300 301 return null; 302 } 303 304 307 public void getResources(Vector <URL > vector, String name) 308 { 309 for (int i = 0; i < _jarList.size(); i++) { 310 JarEntry jarEntry = _jarList.get(i); 311 Path path = jarEntry.getJarPath(); 312 313 path = path.lookup(name); 314 315 if (path.canRead() || path.isDirectory()) { 316 try { 317 vector.add(new URL (path.getURL())); 318 } catch (Exception e) { 319 log.log(Level.WARNING, e.toString(), e); 320 } 321 } 322 } 323 } 324 325 332 public Path getPath(String pathName) 333 { 334 for (int i = 0; i < _jarList.size(); i++) { 335 JarEntry jarEntry = _jarList.get(i); 336 Path path = jarEntry.getJarPath(); 337 338 Path filePath = path.lookup(pathName); 339 340 if (filePath.exists()) 341 return filePath; 342 } 343 344 return null; 345 } 346 347 public Path getCodePath() 348 { 349 return _fileSet.getDir(); 350 } 351 352 355 protected void destroy() 356 { 357 clearJars(); 358 } 359 360 363 private void clearJars() 364 { 365 ArrayList <JarEntry> jars = new ArrayList <JarEntry>(_jarList); 366 _jarList.clear(); 367 368 for (int i = 0; i < jars.size(); i++) { 369 JarEntry jarEntry = jars.get(i); 370 371 JarPath jarPath = jarEntry.getJarPath(); 372 373 jarPath.closeJar(); 374 } 375 } 376 377 public String toString() 378 { 379 return "LibraryLoader[" + _fileSet + "]"; 380 } 381 } 382 | Popular Tags |