1 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 ; 40 import java.security.CodeSource ; 41 import java.security.cert.Certificate ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 45 49 public class SimpleLoader extends Loader { 50 private static final Logger log = Log.open(SimpleLoader.class); 51 52 private Path _path; 54 private String _prefix; 55 private String _pathPrefix; 56 57 private CodeSource _codeSource; 58 59 62 public SimpleLoader() 63 { 64 } 65 66 71 public SimpleLoader(Path path) 72 { 73 setPath(path); 74 } 75 76 82 public SimpleLoader(Path path, String prefix) 83 { 84 setPath(path); 85 setPrefix(prefix); 86 } 87 88 97 public static DynamicClassLoader create(ClassLoader parent, 98 Path path, 99 String 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 118 public static DynamicClassLoader create(ClassLoader 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 137 public static DynamicClassLoader create(Path path) 138 { 139 ClassLoader parent = Thread.currentThread().getContextClassLoader(); 140 141 return create(parent, path); 142 } 143 144 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 159 public Path getPath() 160 { 161 return _path; 162 } 163 164 167 public void setPrefix(String prefix) 168 { 169 _prefix = prefix; 170 171 if (prefix != null) 172 _pathPrefix = prefix.replace('.', '/'); 173 } 174 175 178 public String getPrefix() 179 { 180 return _prefix; 181 } 182 183 186 public void setLoader(DynamicClassLoader loader) 187 { 188 super.setLoader(loader); 189 190 loader.addURL(_path); 191 } 192 193 196 @PostConstruct 197 public void init() 198 throws ConfigException 199 { 200 try { 201 _codeSource = new CodeSource (new URL (_path.getURL()), (Certificate []) null); 202 } catch (Exception e) { 203 log.log(Level.FINE, e.toString(), e); 204 } 205 } 206 207 214 public Path getPath(String 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 228 protected CodeSource getCodeSource(Path path) 229 { 230 return _codeSource; 231 } 232 233 236 protected String getClassPath(String 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 256 public String toString() 257 { 258 if (_prefix != null) 259 return "SimpleLoader[" + _path + ",prefix=" + _prefix + "]"; 260 else 261 return "SimpleLoader[" + _path + "]"; 262 } 263 } 264 | Popular Tags |