1 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 ; 42 import java.util.ArrayList ; 43 import java.util.Vector ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 51 public class JarLoader extends Loader implements Dependency { 52 private static final Logger log 53 = Logger.getLogger(JarLoader.class.getName()); 54 55 private ArrayList <JarEntry> _jarList = new ArrayList <JarEntry>(); 57 58 private DependencyContainer _dependencyList = new DependencyContainer(); 60 61 64 public JarLoader() 65 { 66 } 67 68 71 @PostConstruct 72 public void init() 73 { 74 } 75 76 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 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 102 public boolean isModified() 103 { 104 return _dependencyList.isModified(); 105 } 106 107 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 130 protected String getClassPath(String 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 153 protected ClassEntry getClassEntry(String name) 154 throws ClassNotFoundException 155 { 156 String pathName = name.replace('.', '/'); 157 158 String 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 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 192 public void getResources(Vector <URL > vector, String 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 (path.getURL())); 203 } catch (Exception e) { 204 log.log(Level.WARNING, e.toString(), e); 205 } 206 } 207 } 208 } 209 210 217 public Path getPath(String 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 toString() 238 { 239 return "JarLoader[]"; 240 } 241 } 242 | Popular Tags |