You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ClassPathManager.java 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /* *******************************************************************
  2. * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v1.0
  6. * which accompanies this distribution and is available at
  7. * http://www.eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.io.File;
  14. import java.io.FileInputStream;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.util.ArrayList;
  19. import java.util.Enumeration;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.zip.ZipEntry;
  23. import java.util.zip.ZipFile;
  24. import org.aspectj.bridge.IMessageHandler;
  25. import org.aspectj.bridge.MessageUtil;
  26. import org.aspectj.weaver.BCException;
  27. import org.aspectj.weaver.UnresolvedType;
  28. import org.aspectj.weaver.WeaverMessages;
  29. import org.aspectj.weaver.tools.Trace;
  30. import org.aspectj.weaver.tools.TraceFactory;
  31. public class ClassPathManager {
  32. private List entries;
  33. // In order to control how many open files we have, we maintain a list.
  34. // The max number is configured through the property:
  35. // org.aspectj.weaver.openarchives
  36. // and it defaults to 1000
  37. private List openArchives = new ArrayList();
  38. private static int maxOpenArchives = -1;
  39. private static final int MAXOPEN_DEFAULT = 1000;
  40. private static Trace trace = TraceFactory.getTraceFactory().getTrace(ClassPathManager.class);
  41. static {
  42. String openzipsString = getSystemPropertyWithoutSecurityException("org.aspectj.weaver.openarchives",Integer.toString(MAXOPEN_DEFAULT));
  43. maxOpenArchives=Integer.parseInt(openzipsString);
  44. if (maxOpenArchives<20) maxOpenArchives=1000;
  45. }
  46. public ClassPathManager(List classpath, IMessageHandler handler) {
  47. if (trace.isTraceEnabled()) trace.enter("<init>",this,new Object[] { classpath, handler });
  48. entries = new ArrayList();
  49. for (Iterator i = classpath.iterator(); i.hasNext();) {
  50. String name = (String) i.next();
  51. addPath(name, handler);
  52. }
  53. if (trace.isTraceEnabled()) trace.exit("<init>");
  54. }
  55. protected ClassPathManager() {};
  56. public void addPath (String name, IMessageHandler handler) {
  57. File f = new File(name);
  58. String lc = name.toLowerCase();
  59. if (!f.isDirectory()) {
  60. if (!f.isFile()) {
  61. if (!lc.endsWith(".jar") || lc.endsWith(".zip")) {
  62. // heuristic-only: ending with .jar or .zip means probably a zip file
  63. MessageUtil.info(handler, WeaverMessages.format(WeaverMessages.ZIPFILE_ENTRY_MISSING,name));
  64. } else {
  65. MessageUtil.info(handler, WeaverMessages.format(WeaverMessages.DIRECTORY_ENTRY_MISSING,name));
  66. }
  67. return;
  68. }
  69. try {
  70. entries.add(new ZipFileEntry(f));
  71. } catch (IOException ioe) {
  72. MessageUtil.warn(handler, WeaverMessages.format(WeaverMessages.ZIPFILE_ENTRY_INVALID,name,ioe.getMessage()));
  73. return;
  74. }
  75. } else {
  76. entries.add(new DirEntry(f));
  77. }
  78. }
  79. public ClassFile find(UnresolvedType type) {
  80. String name = type.getName();
  81. for (Iterator i = entries.iterator(); i.hasNext(); ) {
  82. Entry entry = (Entry)i.next();
  83. try {
  84. ClassFile ret = entry.find(name);
  85. if (ret != null) return ret;
  86. } catch (IOException ioe) {
  87. // this is NOT an error: it's valid to have missing classpath entries
  88. i.remove();
  89. }
  90. }
  91. return null;
  92. }
  93. public String toString() {
  94. StringBuffer buf = new StringBuffer();
  95. boolean start = true;
  96. for (Iterator i = entries.iterator(); i.hasNext(); ) {
  97. if (start) { start = false; }
  98. else {buf.append(File.pathSeparator); }
  99. buf.append(i.next());
  100. }
  101. return buf.toString();
  102. }
  103. /**
  104. * This method is extremely expensive and should only be called rarely
  105. */
  106. public List getAllClassFiles() {
  107. List ret = new ArrayList();
  108. for (Iterator i = entries.iterator(); i.hasNext(); ) {
  109. Entry entry = (Entry)i.next();
  110. try {
  111. ret.addAll(entry.getAllClassFiles());
  112. } catch (IOException e) {
  113. i.remove();
  114. }
  115. }
  116. return ret;
  117. }
  118. public abstract static class ClassFile {
  119. public abstract InputStream getInputStream() throws IOException;
  120. public abstract String getPath();
  121. public abstract void close();
  122. }
  123. public abstract static class Entry {
  124. public abstract ClassFile find(String name) throws IOException;
  125. public abstract List getAllClassFiles() throws IOException;
  126. }
  127. private static class FileClassFile extends ClassFile {
  128. private File file;
  129. private FileInputStream fis;
  130. public FileClassFile(File file) {
  131. this.file = file;
  132. }
  133. public InputStream getInputStream() throws IOException {
  134. fis = new FileInputStream(file);
  135. return fis;
  136. }
  137. public void close() {
  138. try {
  139. if (fis!=null) fis.close();
  140. } catch (IOException ioe) {
  141. throw new BCException("Can't close class file : "+file.getName(),ioe);
  142. } finally {
  143. fis = null;
  144. }
  145. }
  146. public String getPath() { return file.getPath(); }
  147. }
  148. public class DirEntry extends Entry {
  149. private String dirPath;
  150. public DirEntry(File dir) { this.dirPath = dir.getPath(); }
  151. public DirEntry(String dirPath) { this.dirPath = dirPath; }
  152. public ClassFile find(String name) {
  153. File f = new File(dirPath + File.separator + name.replace('.', File.separatorChar) + ".class");
  154. if (f.isFile()) return new FileClassFile(f);
  155. else return null;
  156. }
  157. public List getAllClassFiles() {
  158. throw new RuntimeException("unimplemented");
  159. }
  160. public String toString() { return dirPath; }
  161. }
  162. private static class ZipEntryClassFile extends ClassFile {
  163. private ZipEntry entry;
  164. private ZipFileEntry zipFile;
  165. private InputStream is;
  166. public ZipEntryClassFile(ZipFileEntry zipFile, ZipEntry entry) {
  167. this.zipFile = zipFile;
  168. this.entry = entry;
  169. }
  170. public InputStream getInputStream() throws IOException {
  171. is = zipFile.getZipFile().getInputStream(entry);
  172. return is;
  173. }
  174. public void close() {
  175. try {
  176. if (is!=null) is.close();
  177. } catch (IOException e) {
  178. e.printStackTrace();
  179. } finally {
  180. is = null;
  181. }
  182. }
  183. public String getPath() { return entry.getName(); }
  184. }
  185. public class ZipFileEntry extends Entry {
  186. private File file;
  187. private ZipFile zipFile;
  188. public ZipFileEntry(File file) throws IOException {
  189. this.file = file;
  190. }
  191. public ZipFileEntry(ZipFile zipFile) {
  192. this.zipFile = zipFile;
  193. }
  194. public ZipFile getZipFile() {
  195. return zipFile;
  196. }
  197. public ClassFile find(String name) throws IOException {
  198. ensureOpen();
  199. String key = name.replace('.', '/') + ".class";
  200. ZipEntry entry = zipFile.getEntry(key);
  201. if (entry != null) return new ZipEntryClassFile(this, entry);
  202. else return null; // This zip will be closed when necessary...
  203. }
  204. public List getAllClassFiles() throws IOException {
  205. ensureOpen();
  206. List ret = new ArrayList();
  207. for (Enumeration e = zipFile.entries(); e.hasMoreElements(); ) {
  208. ZipEntry entry = (ZipEntry)e.nextElement();
  209. String name = entry.getName();
  210. if (hasClassExtension(name)) ret.add(new ZipEntryClassFile(this, entry));
  211. }
  212. // if (ret.isEmpty()) close();
  213. return ret;
  214. }
  215. private void ensureOpen() throws IOException {
  216. if (zipFile != null && openArchives.contains(zipFile)) {
  217. if (isReallyOpen()) return;
  218. }
  219. if (openArchives.size()>=maxOpenArchives) {
  220. closeSomeArchives(openArchives.size()/10); // Close 10% of those open
  221. }
  222. zipFile = new ZipFile(file);
  223. if (!isReallyOpen()) {
  224. throw new FileNotFoundException("Can't open archive: "+file.getName()+" (size() check failed)");
  225. }
  226. openArchives.add(zipFile);
  227. }
  228. private boolean isReallyOpen() {
  229. try {
  230. zipFile.size(); // this will fail if the file has been closed for
  231. // some reason;
  232. return true;
  233. } catch (IllegalStateException ex) {
  234. // this means the zip file is closed...
  235. return false;
  236. }
  237. }
  238. public void closeSomeArchives(int n) {
  239. for (int i=n-1;i>=0;i--) {
  240. ZipFile zf = (ZipFile)openArchives.get(i);
  241. try {
  242. zf.close();
  243. } catch (IOException e) {
  244. e.printStackTrace();
  245. }
  246. openArchives.remove(i);
  247. }
  248. }
  249. public void close() {
  250. if (zipFile == null) return;
  251. try {
  252. openArchives.remove(zipFile);
  253. zipFile.close();
  254. } catch (IOException ioe) {
  255. throw new BCException("Can't close archive: "+file.getName(),ioe);
  256. } finally {
  257. zipFile = null;
  258. }
  259. }
  260. public String toString() { return file.getName(); }
  261. }
  262. /* private */ static boolean hasClassExtension(String name) {
  263. return name.toLowerCase().endsWith((".class"));
  264. }
  265. public void closeArchives() {
  266. for (Iterator i = entries.iterator(); i.hasNext(); ) {
  267. Entry entry = (Entry)i.next();
  268. if (entry instanceof ZipFileEntry) {
  269. ((ZipFileEntry)entry).close();
  270. }
  271. openArchives.clear();
  272. }
  273. }
  274. // Copes with the security manager
  275. private static String getSystemPropertyWithoutSecurityException (String aPropertyName, String aDefaultValue) {
  276. try {
  277. return System.getProperty(aPropertyName, aDefaultValue);
  278. } catch (SecurityException ex) {
  279. return aDefaultValue;
  280. }
  281. }
  282. }