Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

ClassPathManager.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. /* *******************************************************************
  2. * Copyright (c) 2002, 2017 Contributors
  3. * All rights reserved.
  4. * This program and the accompanying materials are made available
  5. * under the terms of the Eclipse Public License v 2.0
  6. * which accompanies this distribution and is available at
  7. * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
  8. *
  9. * Contributors:
  10. * Palo Alto Research Center, Incorporated (PARC).
  11. * ******************************************************************/
  12. package org.aspectj.weaver.bcel;
  13. import java.io.ByteArrayInputStream;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileNotFoundException;
  17. import java.io.IOException;
  18. import java.io.InputStream;
  19. import java.net.MalformedURLException;
  20. import java.net.URI;
  21. import java.net.URL;
  22. import java.net.URLClassLoader;
  23. import java.nio.file.FileSystem;
  24. import java.nio.file.FileSystems;
  25. import java.nio.file.FileVisitResult;
  26. import java.nio.file.Files;
  27. import java.nio.file.Path;
  28. import java.nio.file.SimpleFileVisitor;
  29. import java.nio.file.attribute.BasicFileAttributes;
  30. import java.util.ArrayList;
  31. import java.util.Enumeration;
  32. import java.util.HashMap;
  33. import java.util.HashSet;
  34. import java.util.Iterator;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.Set;
  38. import java.util.zip.ZipEntry;
  39. import java.util.zip.ZipFile;
  40. import org.aspectj.bridge.IMessageHandler;
  41. import org.aspectj.bridge.MessageUtil;
  42. import org.aspectj.util.LangUtil;
  43. import org.aspectj.util.SoftHashMap;
  44. import org.aspectj.weaver.BCException;
  45. import org.aspectj.weaver.UnresolvedType;
  46. import org.aspectj.weaver.WeaverMessages;
  47. import org.aspectj.weaver.tools.Trace;
  48. import org.aspectj.weaver.tools.TraceFactory;
  49. /**
  50. * @author Andy Clement
  51. * @author Mario Ivankovits
  52. */
  53. public class ClassPathManager {
  54. private static Trace trace = TraceFactory.getTraceFactory().getTrace(ClassPathManager.class);
  55. private static int maxOpenArchives = -1;
  56. private static URI JRT_URI = URI.create("jrt:/"); //$NON-NLS-1$
  57. private static final int MAXOPEN_DEFAULT = 1000;
  58. private final List<Entry> entries;
  59. private final Set<String> notFound = new HashSet<>(100);
  60. // In order to control how many open files we have, we maintain a list.
  61. // The max number is configured through the property:
  62. // org.aspectj.weaver.openarchives
  63. // and it defaults to 1000
  64. private final List<ZipFile> openArchives = new ArrayList<>();
  65. static {
  66. String openzipsString = getSystemPropertyWithoutSecurityException("org.aspectj.weaver.openarchives",
  67. Integer.toString(MAXOPEN_DEFAULT));
  68. maxOpenArchives = Integer.parseInt(openzipsString);
  69. if (maxOpenArchives < 20) {
  70. maxOpenArchives = MAXOPEN_DEFAULT;
  71. }
  72. }
  73. public ClassPathManager(List<String> classpath, IMessageHandler handler) {
  74. if (trace.isTraceEnabled()) {
  75. trace.enter("<init>", this, new Object[] { classpath==null?"null":classpath.toString(), handler });
  76. }
  77. entries = new ArrayList<>(classpath == null ? 1 : classpath.size());
  78. for (String classpathEntry: classpath) {
  79. addPath(classpathEntry,handler);
  80. }
  81. if (trace.isTraceEnabled()) {
  82. trace.exit("<init>");
  83. }
  84. }
  85. protected ClassPathManager() {
  86. entries = null;
  87. }
  88. public void addPath(String name, IMessageHandler handler) {
  89. File f = new File(name);
  90. if (!f.isDirectory()) {
  91. if (!f.isFile()) {
  92. if (!name.toLowerCase().endsWith(".jar") || name.toLowerCase().endsWith(".zip")) {
  93. // heuristic-only: ending with .jar or .zip means probably a zip file
  94. MessageUtil.info(handler, WeaverMessages.format(WeaverMessages.ZIPFILE_ENTRY_MISSING, name));
  95. } else {
  96. MessageUtil.info(handler, WeaverMessages.format(WeaverMessages.DIRECTORY_ENTRY_MISSING, name));
  97. }
  98. return;
  99. }
  100. try {
  101. if (name.toLowerCase().endsWith(LangUtil.JRT_FS)) { // Java9+
  102. entries.add(new JImageEntry(name));
  103. } else {
  104. entries.add(new ZipFileEntry(f));
  105. }
  106. } catch (IOException ioe) {
  107. MessageUtil.warn(handler,
  108. WeaverMessages.format(WeaverMessages.ZIPFILE_ENTRY_INVALID, name, ioe.getMessage()));
  109. return;
  110. }
  111. } else {
  112. entries.add(new DirEntry(f));
  113. }
  114. }
  115. public ClassFile find(UnresolvedType type) {
  116. if (trace.isTraceEnabled()) {
  117. trace.enter("find", this, type);
  118. }
  119. String name = type.getName();
  120. if (notFound.contains(name)) {
  121. return null;
  122. }
  123. for (Iterator<Entry> i = entries.iterator(); i.hasNext();) {
  124. Entry entry = i.next();
  125. try {
  126. ClassFile ret = entry.find(name);
  127. if (trace.isTraceEnabled()) {
  128. trace.event("searching for "+type+" in "+entry.toString());
  129. }
  130. if (ret != null) {
  131. if (trace.isTraceEnabled()) {
  132. trace.exit("find", ret);
  133. }
  134. return ret;
  135. }
  136. } catch (IOException ioe) {
  137. // this is NOT an error: it's valid to have missing classpath entries
  138. if (trace.isTraceEnabled()) {
  139. trace.error("Removing classpath entry for "+entry,ioe);
  140. }
  141. i.remove();
  142. }
  143. }
  144. if (trace.isTraceEnabled()) {
  145. trace.exit("find", null);
  146. }
  147. notFound.add(name);
  148. return null;
  149. }
  150. @Override
  151. public String toString() {
  152. StringBuilder buf = new StringBuilder();
  153. boolean start = true;
  154. for (Entry entry : entries) {
  155. if (start) {
  156. start = false;
  157. } else {
  158. buf.append(File.pathSeparator);
  159. }
  160. buf.append(entry);
  161. }
  162. return buf.toString();
  163. }
  164. public abstract static class ClassFile {
  165. public abstract InputStream getInputStream() throws IOException;
  166. public abstract String getPath();
  167. public abstract void close();
  168. }
  169. abstract static class Entry {
  170. public abstract ClassFile find(String name) throws IOException;
  171. }
  172. static class ByteBasedClassFile extends ClassFile {
  173. private final byte[] bytes;
  174. private ByteArrayInputStream bais;
  175. private final String path;
  176. public ByteBasedClassFile(byte[] bytes, String path) {
  177. this.bytes = bytes;
  178. this.path = path;
  179. }
  180. @Override
  181. public InputStream getInputStream() throws IOException {
  182. this.bais = new ByteArrayInputStream(bytes);
  183. return this.bais;
  184. }
  185. @Override
  186. public String getPath() {
  187. return this.path;
  188. }
  189. @Override
  190. public void close() {
  191. if (this.bais!=null) {
  192. try {
  193. this.bais.close();
  194. } catch (IOException e) {
  195. }
  196. this.bais = null;
  197. }
  198. }
  199. }
  200. static class FileClassFile extends ClassFile {
  201. private final File file;
  202. private FileInputStream fis;
  203. public FileClassFile(File file) {
  204. this.file = file;
  205. }
  206. @Override
  207. public InputStream getInputStream() throws IOException {
  208. fis = new FileInputStream(file);
  209. return fis;
  210. }
  211. @Override
  212. public void close() {
  213. try {
  214. if (fis != null)
  215. fis.close();
  216. } catch (IOException ioe) {
  217. throw new BCException("Can't close class file : " + file.getName(), ioe);
  218. } finally {
  219. fis = null;
  220. }
  221. }
  222. @Override
  223. public String getPath() {
  224. return file.getPath();
  225. }
  226. }
  227. class DirEntry extends Entry {
  228. private final String dirPath;
  229. public DirEntry(File dir) {
  230. this.dirPath = dir.getPath();
  231. }
  232. public DirEntry(String dirPath) {
  233. this.dirPath = dirPath;
  234. }
  235. @Override
  236. public ClassFile find(String name) {
  237. File f = new File(dirPath + File.separator + name.replace('.', File.separatorChar) + ".class");
  238. if (f.isFile())
  239. return new FileClassFile(f);
  240. else
  241. return null;
  242. }
  243. @Override
  244. public String toString() {
  245. return dirPath;
  246. }
  247. }
  248. static class ZipEntryClassFile extends ClassFile {
  249. private final ZipEntry entry;
  250. private final ZipFileEntry zipFile;
  251. private InputStream is;
  252. public ZipEntryClassFile(ZipFileEntry zipFile, ZipEntry entry) {
  253. this.zipFile = zipFile;
  254. this.entry = entry;
  255. }
  256. @Override
  257. public InputStream getInputStream() throws IOException {
  258. is = zipFile.getZipFile().getInputStream(entry);
  259. return is;
  260. }
  261. @Override
  262. public void close() {
  263. try {
  264. if (is != null)
  265. is.close();
  266. } catch (IOException e) {
  267. e.printStackTrace();
  268. } finally {
  269. is = null;
  270. }
  271. }
  272. @Override
  273. public String getPath() {
  274. return entry.getName();
  275. }
  276. }
  277. /**
  278. * Maintains a shared package cache for java runtime image. This maps packages (for example:
  279. * java/lang) to a starting root position in the filesystem (for example: /modules/java.base/java/lang).
  280. * When searching for a type we work out the package name, use it to find where in the filesystem
  281. * to start looking then run from there. Once found we do cache what we learn to make subsequent
  282. * lookups of that type even faster. Maintaining just a package cache rather than complete type cache
  283. * helps reduce memory usage but still gives reasonably fast lookup performance.
  284. */
  285. static class JImageEntry extends Entry {
  286. // Map from a JRT-FS file to the cache state for that file
  287. private static Map<String, JImageState> states = new HashMap<>();
  288. private JImageState state;
  289. // TODO memory management here - is it held onto too long when LTW?
  290. static class JImageState {
  291. private final String jrtFsPath;
  292. private final FileSystem fs;
  293. Map<String,Path> fileCache = new SoftHashMap<>();
  294. boolean packageCacheInitialized = false;
  295. Map<String,Path> packageCache = new HashMap<>();
  296. public JImageState(String jrtFsPath, FileSystem fs) {
  297. this.jrtFsPath = jrtFsPath;
  298. this.fs = fs;
  299. }
  300. }
  301. public JImageEntry(String jrtFsPath) {
  302. state = states.get(jrtFsPath);
  303. if (state == null) {
  304. synchronized (states) {
  305. if (state == null) {
  306. URL jrtPath = null;
  307. try {
  308. jrtPath = new File(jrtFsPath).toPath().toUri().toURL();
  309. } catch (MalformedURLException e) {
  310. System.out.println("Unexpected problem processing "+jrtFsPath+" bad classpath entry? skipping:"+e.getMessage());
  311. return;
  312. }
  313. String jdkHome = new File(jrtFsPath).getParentFile().getParent();
  314. FileSystem fs = null;
  315. try {
  316. if (LangUtil.isVMGreaterOrEqual(9)) {
  317. Map<String, String> env = new HashMap<>();
  318. env.put("java.home", jdkHome);
  319. fs = FileSystems.newFileSystem(JRT_URI, env);
  320. } else {
  321. URLClassLoader loader = new URLClassLoader(new URL[] { jrtPath });
  322. Map<String, ?> env = new HashMap<>();
  323. fs = FileSystems.newFileSystem(JRT_URI, env, loader);
  324. }
  325. state = new JImageState(jrtFsPath, fs);
  326. states.put(jrtFsPath, state);
  327. buildPackageMap();
  328. } catch (Throwable t) {
  329. throw new IllegalStateException("Unexpectedly unable to initialize a JRT filesystem", t);
  330. }
  331. }
  332. }
  333. }
  334. }
  335. class PackageCacheBuilderVisitor extends SimpleFileVisitor<Path> {
  336. @Override
  337. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  338. if (file.getNameCount() > 3 && file.toString().endsWith(".class")) {
  339. int fnc = file.getNameCount();
  340. if (fnc > 3) { // There is a package name - e.g. /modules/java.base/java/lang/Object.class
  341. Path packagePath = file.subpath(2, fnc-1); // e.g. java/lang
  342. String packagePathString = packagePath.toString();
  343. state.packageCache.put(packagePathString, file.subpath(0, fnc-1)); // java/lang -> /modules/java.base/java/lang
  344. }
  345. }
  346. return FileVisitResult.CONTINUE;
  347. }
  348. }
  349. /**
  350. * Create a map from package names to the specific directory of the package members in the filesystem.
  351. */
  352. private synchronized void buildPackageMap() {
  353. if (!state.packageCacheInitialized) {
  354. state.packageCacheInitialized = true;
  355. Iterable<java.nio.file.Path> roots = state.fs.getRootDirectories();
  356. PackageCacheBuilderVisitor visitor = new PackageCacheBuilderVisitor();
  357. try {
  358. for (java.nio.file.Path path : roots) {
  359. Files.walkFileTree(path, visitor);
  360. }
  361. } catch (IOException e) {
  362. throw new RuntimeException(e);
  363. }
  364. }
  365. }
  366. class TypeIdentifier extends SimpleFileVisitor<Path> {
  367. // What are we looking for?
  368. private final String name;
  369. // If set, where did we find it?
  370. public Path found;
  371. // Basic metric count of how many files we checked before finding it
  372. public int filesSearchedCount;
  373. public TypeIdentifier(String name) {
  374. this.name = name;
  375. }
  376. @Override
  377. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  378. filesSearchedCount++;
  379. if (file.getNameCount() > 2 && file.toString().endsWith(".class")) {
  380. int fnc = file.getNameCount();
  381. Path filePath = file.subpath(2, fnc);
  382. String filePathString = filePath.toString();
  383. if (filePathString.equals(name)) {
  384. state.fileCache.put(filePathString, file);
  385. found = file;
  386. return FileVisitResult.TERMINATE;
  387. }
  388. }
  389. return FileVisitResult.CONTINUE;
  390. }
  391. }
  392. private Path searchForFileAndCache(final Path startPath, final String name) {
  393. TypeIdentifier locator = new TypeIdentifier(name);
  394. try {
  395. Files.walkFileTree(startPath, locator);
  396. } catch (IOException e) {
  397. throw new RuntimeException(e);
  398. }
  399. return locator.found;
  400. }
  401. @Override
  402. public ClassFile find(String name) throws IOException {
  403. String fileName = name.replace('.', '/') + ".class";
  404. Path file = state.fileCache.get(fileName);
  405. if (file == null) {
  406. // Check the packages map to see if we know about this package
  407. int idx = fileName.lastIndexOf('/');
  408. if (idx == -1) {
  409. // Package not here
  410. return null;
  411. }
  412. Path packageStart = null;
  413. String packageName = null;
  414. if (idx !=-1 ) {
  415. packageName = fileName.substring(0, idx);
  416. packageStart = state.packageCache.get(packageName);
  417. if (packageStart != null) {
  418. file = searchForFileAndCache(packageStart, fileName);
  419. }
  420. }
  421. }
  422. if (file == null) {
  423. return null;
  424. }
  425. byte[] bs = Files.readAllBytes(file);
  426. ClassFile cf = new ByteBasedClassFile(bs, fileName);
  427. return cf;
  428. }
  429. Map<String, Path> getPackageCache() {
  430. return state.packageCache;
  431. }
  432. Map<String, Path> getFileCache() {
  433. return state.fileCache;
  434. }
  435. }
  436. class ZipFileEntry extends Entry {
  437. private File file;
  438. private ZipFile zipFile;
  439. public ZipFileEntry(File file) throws IOException {
  440. this.file = file;
  441. }
  442. public ZipFileEntry(ZipFile zipFile) {
  443. this.zipFile = zipFile;
  444. }
  445. public ZipFile getZipFile() {
  446. return zipFile;
  447. }
  448. @Override
  449. public ClassFile find(String name) throws IOException {
  450. ensureOpen();
  451. String key = name.replace('.', '/') + ".class";
  452. ZipEntry entry = zipFile.getEntry(key);
  453. if (entry != null)
  454. return new ZipEntryClassFile(this, entry);
  455. else
  456. return null; // This zip will be closed when necessary...
  457. }
  458. public List<ZipEntryClassFile> getAllClassFiles() throws IOException {
  459. ensureOpen();
  460. List<ZipEntryClassFile> ret = new ArrayList<>();
  461. for (Enumeration<? extends ZipEntry> e = zipFile.entries(); e.hasMoreElements();) {
  462. ZipEntry entry = e.nextElement();
  463. String name = entry.getName();
  464. if (hasClassExtension(name))
  465. ret.add(new ZipEntryClassFile(this, entry));
  466. }
  467. // if (ret.isEmpty()) close();
  468. return ret;
  469. }
  470. private void ensureOpen() throws IOException {
  471. if (zipFile != null && openArchives.contains(zipFile)) {
  472. if (isReallyOpen())
  473. return;
  474. }
  475. if (openArchives.size() >= maxOpenArchives) {
  476. closeSomeArchives(openArchives.size() / 10); // Close 10% of
  477. // those open
  478. }
  479. zipFile = new ZipFile(file);
  480. if (!isReallyOpen()) {
  481. throw new FileNotFoundException("Can't open archive: " + file.getName() + " (size() check failed)");
  482. }
  483. openArchives.add(zipFile);
  484. }
  485. private boolean isReallyOpen() {
  486. try {
  487. zipFile.size(); // this will fail if the file has been closed
  488. // for
  489. // some reason;
  490. return true;
  491. } catch (IllegalStateException ex) {
  492. // this means the zip file is closed...
  493. return false;
  494. }
  495. }
  496. public void closeSomeArchives(int n) {
  497. for (int i = n - 1; i >= 0; i--) {
  498. ZipFile zf = openArchives.get(i);
  499. try {
  500. zf.close();
  501. } catch (IOException e) {
  502. e.printStackTrace();
  503. }
  504. openArchives.remove(i);
  505. }
  506. }
  507. public void close() {
  508. if (zipFile == null)
  509. return;
  510. try {
  511. openArchives.remove(zipFile);
  512. zipFile.close();
  513. } catch (IOException ioe) {
  514. throw new BCException("Can't close archive: " + file.getName(), ioe);
  515. } finally {
  516. zipFile = null;
  517. }
  518. }
  519. @Override
  520. public String toString() {
  521. return file.getName();
  522. }
  523. }
  524. /* private */static boolean hasClassExtension(String name) {
  525. return name.toLowerCase().endsWith(".class");
  526. }
  527. public void closeArchives() {
  528. for (Entry entry : entries) {
  529. if (entry instanceof ZipFileEntry) {
  530. ((ZipFileEntry) entry).close();
  531. }
  532. openArchives.clear();
  533. }
  534. }
  535. // Copes with the security manager
  536. private static String getSystemPropertyWithoutSecurityException(String aPropertyName, String aDefaultValue) {
  537. try {
  538. return System.getProperty(aPropertyName, aDefaultValue);
  539. } catch (SecurityException ex) {
  540. return aDefaultValue;
  541. }
  542. }
  543. // Mainly exposed for testing
  544. public List<Entry> getEntries() {
  545. return entries;
  546. }
  547. }