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 8.0KB

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