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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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
  43. .toString(MAXOPEN_DEFAULT));
  44. maxOpenArchives = Integer.parseInt(openzipsString);
  45. if (maxOpenArchives < 20)
  46. maxOpenArchives = 1000;
  47. }
  48. public ClassPathManager(List classpath, IMessageHandler handler) {
  49. if (trace.isTraceEnabled())
  50. trace.enter("<init>", this, new Object[] { classpath, handler });
  51. entries = new ArrayList();
  52. for (Iterator i = classpath.iterator(); i.hasNext();) {
  53. String name = (String) i.next();
  54. addPath(name, handler);
  55. }
  56. if (trace.isTraceEnabled())
  57. trace.exit("<init>");
  58. }
  59. protected ClassPathManager() {
  60. }
  61. public void addPath(String name, IMessageHandler handler) {
  62. File f = new File(name);
  63. String lc = name.toLowerCase();
  64. if (!f.isDirectory()) {
  65. if (!f.isFile()) {
  66. if (!lc.endsWith(".jar") || lc.endsWith(".zip")) {
  67. // heuristic-only: ending with .jar or .zip means probably a zip file
  68. MessageUtil.info(handler, WeaverMessages.format(WeaverMessages.ZIPFILE_ENTRY_MISSING, name));
  69. } else {
  70. MessageUtil.info(handler, WeaverMessages.format(WeaverMessages.DIRECTORY_ENTRY_MISSING, name));
  71. }
  72. return;
  73. }
  74. try {
  75. entries.add(new ZipFileEntry(f));
  76. } catch (IOException ioe) {
  77. MessageUtil.warn(handler, WeaverMessages.format(WeaverMessages.ZIPFILE_ENTRY_INVALID, name, ioe.getMessage()));
  78. return;
  79. }
  80. } else {
  81. entries.add(new DirEntry(f));
  82. }
  83. }
  84. public ClassFile find(UnresolvedType type) {
  85. String name = type.getName();
  86. for (Iterator i = entries.iterator(); i.hasNext();) {
  87. Entry entry = (Entry) i.next();
  88. try {
  89. ClassFile ret = entry.find(name);
  90. if (ret != null)
  91. return ret;
  92. } catch (IOException ioe) {
  93. // this is NOT an error: it's valid to have missing classpath entries
  94. i.remove();
  95. }
  96. }
  97. return null;
  98. }
  99. public String toString() {
  100. StringBuffer buf = new StringBuffer();
  101. boolean start = true;
  102. for (Iterator i = entries.iterator(); i.hasNext();) {
  103. if (start) {
  104. start = false;
  105. } else {
  106. buf.append(File.pathSeparator);
  107. }
  108. buf.append(i.next());
  109. }
  110. return buf.toString();
  111. }
  112. // /**
  113. // * This method is extremely expensive and should only be called rarely
  114. // */
  115. // public List getAllClassFiles() {
  116. // List ret = new ArrayList();
  117. // for (Iterator i = entries.iterator(); i.hasNext(); ) {
  118. // Entry entry = (Entry)i.next();
  119. // try {
  120. // ret.addAll(entry.getAllClassFiles());
  121. // } catch (IOException e) {
  122. // i.remove();
  123. // }
  124. // }
  125. // return ret;
  126. // }
  127. //
  128. public abstract static class ClassFile {
  129. public abstract InputStream getInputStream() throws IOException;
  130. public abstract String getPath();
  131. public abstract void close();
  132. }
  133. public abstract static class Entry {
  134. public abstract ClassFile find(String name) throws IOException;
  135. // public abstract List getAllClassFiles() throws IOException;
  136. }
  137. private static class FileClassFile extends ClassFile {
  138. private File file;
  139. private FileInputStream fis;
  140. public FileClassFile(File file) {
  141. this.file = file;
  142. }
  143. public InputStream getInputStream() throws IOException {
  144. fis = new FileInputStream(file);
  145. return fis;
  146. }
  147. public void close() {
  148. try {
  149. if (fis != null)
  150. fis.close();
  151. } catch (IOException ioe) {
  152. throw new BCException("Can't close class file : " + file.getName(), ioe);
  153. } finally {
  154. fis = null;
  155. }
  156. }
  157. public String getPath() {
  158. return file.getPath();
  159. }
  160. }
  161. public class DirEntry extends Entry {
  162. private String dirPath;
  163. public DirEntry(File dir) {
  164. this.dirPath = dir.getPath();
  165. }
  166. public DirEntry(String dirPath) {
  167. this.dirPath = dirPath;
  168. }
  169. public ClassFile find(String name) {
  170. File f = new File(dirPath + File.separator + name.replace('.', File.separatorChar) + ".class");
  171. if (f.isFile())
  172. return new FileClassFile(f);
  173. else
  174. return null;
  175. }
  176. public List getAllClassFiles() {
  177. throw new RuntimeException("unimplemented");
  178. }
  179. public String toString() {
  180. return dirPath;
  181. }
  182. }
  183. private static class ZipEntryClassFile extends ClassFile {
  184. private ZipEntry entry;
  185. private ZipFileEntry zipFile;
  186. private InputStream is;
  187. public ZipEntryClassFile(ZipFileEntry zipFile, ZipEntry entry) {
  188. this.zipFile = zipFile;
  189. this.entry = entry;
  190. }
  191. public InputStream getInputStream() throws IOException {
  192. is = zipFile.getZipFile().getInputStream(entry);
  193. return is;
  194. }
  195. public void close() {
  196. try {
  197. if (is != null)
  198. is.close();
  199. } catch (IOException e) {
  200. e.printStackTrace();
  201. } finally {
  202. is = null;
  203. }
  204. }
  205. public String getPath() {
  206. return entry.getName();
  207. }
  208. }
  209. public class ZipFileEntry extends Entry {
  210. private File file;
  211. private ZipFile zipFile;
  212. public ZipFileEntry(File file) throws IOException {
  213. this.file = file;
  214. }
  215. public ZipFileEntry(ZipFile zipFile) {
  216. this.zipFile = zipFile;
  217. }
  218. public ZipFile getZipFile() {
  219. return zipFile;
  220. }
  221. public ClassFile find(String name) throws IOException {
  222. ensureOpen();
  223. String key = name.replace('.', '/') + ".class";
  224. ZipEntry entry = zipFile.getEntry(key);
  225. if (entry != null)
  226. return new ZipEntryClassFile(this, entry);
  227. else
  228. return null; // This zip will be closed when necessary...
  229. }
  230. public List getAllClassFiles() throws IOException {
  231. ensureOpen();
  232. List ret = new ArrayList();
  233. for (Enumeration e = zipFile.entries(); e.hasMoreElements();) {
  234. ZipEntry entry = (ZipEntry) e.nextElement();
  235. String name = entry.getName();
  236. if (hasClassExtension(name))
  237. ret.add(new ZipEntryClassFile(this, entry));
  238. }
  239. // if (ret.isEmpty()) close();
  240. return ret;
  241. }
  242. private void ensureOpen() throws IOException {
  243. if (zipFile != null && openArchives.contains(zipFile)) {
  244. if (isReallyOpen())
  245. return;
  246. }
  247. if (openArchives.size() >= maxOpenArchives) {
  248. closeSomeArchives(openArchives.size() / 10); // Close 10% of those open
  249. }
  250. zipFile = new ZipFile(file);
  251. if (!isReallyOpen()) {
  252. throw new FileNotFoundException("Can't open archive: " + file.getName() + " (size() check failed)");
  253. }
  254. openArchives.add(zipFile);
  255. }
  256. private boolean isReallyOpen() {
  257. try {
  258. zipFile.size(); // this will fail if the file has been closed for
  259. // some reason;
  260. return true;
  261. } catch (IllegalStateException ex) {
  262. // this means the zip file is closed...
  263. return false;
  264. }
  265. }
  266. public void closeSomeArchives(int n) {
  267. for (int i = n - 1; i >= 0; i--) {
  268. ZipFile zf = (ZipFile) openArchives.get(i);
  269. try {
  270. zf.close();
  271. } catch (IOException e) {
  272. e.printStackTrace();
  273. }
  274. openArchives.remove(i);
  275. }
  276. }
  277. public void close() {
  278. if (zipFile == null)
  279. return;
  280. try {
  281. openArchives.remove(zipFile);
  282. zipFile.close();
  283. } catch (IOException ioe) {
  284. throw new BCException("Can't close archive: " + file.getName(), ioe);
  285. } finally {
  286. zipFile = null;
  287. }
  288. }
  289. public String toString() {
  290. return file.getName();
  291. }
  292. }
  293. /* private */static boolean hasClassExtension(String name) {
  294. return name.toLowerCase().endsWith((".class"));
  295. }
  296. public void closeArchives() {
  297. for (Iterator i = entries.iterator(); i.hasNext();) {
  298. Entry entry = (Entry) i.next();
  299. if (entry instanceof ZipFileEntry) {
  300. ((ZipFileEntry) entry).close();
  301. }
  302. openArchives.clear();
  303. }
  304. }
  305. // Copes with the security manager
  306. private static String getSystemPropertyWithoutSecurityException(String aPropertyName, String aDefaultValue) {
  307. try {
  308. return System.getProperty(aPropertyName, aDefaultValue);
  309. } catch (SecurityException ex) {
  310. return aDefaultValue;
  311. }
  312. }
  313. }