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.

Util.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.internal.tools.build;
  14. import java.io.*;
  15. import java.util.ArrayList;
  16. import java.util.Collections;
  17. import java.util.List;
  18. import java.util.StringTokenizer;
  19. import java.util.jar.Attributes;
  20. import java.util.jar.Manifest;
  21. import java.util.jar.Attributes.Name;
  22. /**
  23. * Build-only utilities.
  24. * Many mirror utils module APIs.
  25. */
  26. public class Util {
  27. public static class Constants {
  28. public static final String TESTSRC = "testsrc";
  29. public static final String JAVA5_SRC = "java5-src";
  30. public static final String JAVA5_TESTSRC = "java5-testsrc";
  31. }
  32. // XXX quick hack for Java 5 support
  33. public static final boolean JAVA5_VM;
  34. static {
  35. boolean java5VM = false;
  36. try {
  37. java5VM = (null != Class.forName("java.lang.annotation.Annotation"));
  38. } catch (Throwable t) {
  39. // ignore
  40. }
  41. JAVA5_VM = java5VM;
  42. }
  43. /**
  44. * Map version in long form to short,
  45. * e.g., replacing "alpha" with "a"
  46. */
  47. public static String shortVersion(String version) {
  48. version = Util.replace(version, "alpha", "a");
  49. version = Util.replace(version, "beta", "b");
  50. version = Util.replace(version, "candidate", "rc");
  51. version = Util.replace(version, "development", "d");
  52. version = Util.replace(version, "dev", "d");
  53. return version;
  54. }
  55. /**
  56. * Replace any instances of {replace} in {input} with {with}.
  57. * @param input the String to search/replace
  58. * @param replace the String to search for in input
  59. * @param with the String to replace with in input
  60. * @return input if it has no replace, otherwise a new String
  61. */
  62. public static String replace(String input, String replace, String with) {
  63. int loc = input.indexOf(replace);
  64. if (-1 != loc) {
  65. String result = input.substring(0, loc);
  66. result += with;
  67. int start = loc + replace.length();
  68. if (start < input.length()) {
  69. result += input.substring(start);
  70. }
  71. input = result;
  72. }
  73. return input;
  74. }
  75. /** @return false if filter returned false for any file in baseDir subtree */
  76. public static boolean visitFiles(File baseDir, FileFilter filter) {
  77. Util.iaxIfNotCanReadDir(baseDir, "baseDir");
  78. Util.iaxIfNull(filter, "filter");
  79. File[] files = baseDir.listFiles();
  80. boolean passed = true;
  81. for (int i = 0; passed && (i < files.length); i++) {
  82. passed = files[i].isDirectory()
  83. ? visitFiles(files[i], filter)
  84. : filter.accept(files[i]);
  85. }
  86. return passed;
  87. }
  88. /** @throws IllegalArgumentException if cannot read dir */
  89. public static void iaxIfNotCanReadDir(File dir, String name) {
  90. if (!canReadDir(dir)) {
  91. throw new IllegalArgumentException(name + " dir not readable: " + dir);
  92. }
  93. }
  94. /** @throws IllegalArgumentException if cannot read file */
  95. public static void iaxIfNotCanReadFile(File file, String name) {
  96. if (!canReadFile(file)) {
  97. throw new IllegalArgumentException(name + " file not readable: " + file);
  98. }
  99. }
  100. /** @throws IllegalArgumentException if cannot write dir */
  101. public static void iaxIfNotCanWriteDir(File dir, String name) {
  102. if (!canWriteDir(dir)) {
  103. throw new IllegalArgumentException(name + " dir not writeable: " + dir);
  104. }
  105. }
  106. /** @throws IllegalArgumentException if input is null */
  107. public static void iaxIfNull(Object input, String name) {
  108. if (null == input) {
  109. throw new IllegalArgumentException("null " + name);
  110. }
  111. }
  112. /** render exception to String */
  113. public static String renderException(Throwable thrown) {
  114. if (null == thrown) {
  115. return "(Throwable) null";
  116. }
  117. StringWriter sw = new StringWriter();
  118. PrintWriter pw = new PrintWriter(sw, true);
  119. pw.println(thrown.getMessage());
  120. thrown.printStackTrace(pw);
  121. pw.flush();
  122. return sw.getBuffer().toString();
  123. }
  124. /** @return true if dir is a writable directory */
  125. public static boolean canWriteDir(File dir) {
  126. return (null != dir) && dir.canWrite() && dir.isDirectory();
  127. }
  128. public static String path(String first, String second) {
  129. return first + File.separator + second;
  130. }
  131. public static String path(String[] segments) {
  132. StringBuffer sb = new StringBuffer();
  133. if ((null != segments)) {
  134. for (int i = 0; i < segments.length; i++) {
  135. if (0 < i) {
  136. sb.append(File.separator);
  137. }
  138. sb.append(segments[i]);
  139. }
  140. }
  141. return sb.toString();
  142. }
  143. /** @return true if dir is a readable directory */
  144. public static boolean canReadDir(File dir) {
  145. return (null != dir) && dir.canRead() && dir.isDirectory();
  146. }
  147. /** @return true if dir is a readable file */
  148. public static boolean canReadFile(File file) {
  149. return (null != file) && file.canRead() && file.isFile();
  150. }
  151. /**
  152. * Delete file or directory.
  153. * @param dir the File file or directory to delete.
  154. * @return true if all contents of dir were deleted
  155. */
  156. public static boolean delete(File dir) {
  157. return deleteContents(dir) && dir.delete();
  158. }
  159. /**
  160. * Delete contents of directory.
  161. * The directory itself is not deleted.
  162. * @param dir the File directory whose contents should be deleted.
  163. * @return true if all contents of dir were deleted
  164. */
  165. public static boolean deleteContents(File dir) {
  166. if ((null == dir) || !dir.canWrite()) {
  167. return false;
  168. } else if (dir.isDirectory()) {
  169. File[] files = dir.listFiles();
  170. for (int i = 0; i < files.length; i++) {
  171. if (!deleteContents(files[i]) || !files[i].delete()) {
  172. return false;
  173. }
  174. }
  175. }
  176. return true;
  177. }
  178. /** @return File temporary directory with the given prefix */
  179. public static File makeTempDir(String prefix) {
  180. if (null == prefix) {
  181. prefix = "tempDir";
  182. }
  183. File tempFile = null;
  184. for (int i = 0; i < 10; i++) {
  185. try {
  186. tempFile = File.createTempFile(prefix,"tmp");
  187. tempFile.delete();
  188. if (tempFile.mkdirs()) {
  189. break;
  190. }
  191. tempFile = null;
  192. } catch (IOException e) {
  193. }
  194. }
  195. return tempFile;
  196. }
  197. /**
  198. * Close stream with the usual checks.
  199. * @param stream the InputStream to close - ignored if null
  200. * @return null if closed without IOException, message otherwise
  201. */
  202. public static String close(Writer stream) {
  203. String result = null;
  204. if (null != stream) {
  205. try {
  206. stream.close();
  207. } catch(IOException e) {
  208. result = e.getMessage();
  209. }
  210. }
  211. return result;
  212. }
  213. /**
  214. * @param list the Object[] to test
  215. * @return true if list is null or empty
  216. */
  217. public static boolean isEmpty(Object[] list) {
  218. return ((null == list) || (0 == list.length));
  219. }
  220. public static void closeSilently(InputStream in) {
  221. if (null != in) {
  222. try {
  223. in.close();
  224. } catch (IOException e) {
  225. // do nothing
  226. }
  227. }
  228. }
  229. public static void closeSilently(Reader in) {
  230. if (null != in) {
  231. try {
  232. in.close();
  233. } catch (IOException e) {
  234. // do nothing
  235. }
  236. }
  237. }
  238. /**
  239. * Report whether actual has different members than expected
  240. * @param expected the String[] of expected members (none null)
  241. * @param actual the String[] of actual members
  242. * @param sb StringBuffer sink for any differences in membership
  243. * @return true if any diffs found and sink updated
  244. */
  245. public static final boolean reportMemberDiffs(String[] expected, String[] actual, StringBuffer sb) {
  246. expected = copy(expected);
  247. actual = copy(actual);
  248. int hits = 0;
  249. for (int i = 0; i < expected.length; i++) {
  250. int curHit = hits;
  251. for (int j = 0; (curHit == hits) && (j < actual.length); j++) {
  252. if (null == expected[i]) {
  253. throw new IllegalArgumentException("null at " + i);
  254. }
  255. if (expected[i].equals(actual[j])) {
  256. expected[i] = null;
  257. actual[j] = null;
  258. hits++;
  259. }
  260. }
  261. }
  262. if ((hits != expected.length) || (hits != actual.length)) {
  263. sb.append("unexpected [");
  264. String prefix = "";
  265. for (int i = 0; i < actual.length; i++) {
  266. if (null != actual[i]) {
  267. sb.append(prefix);
  268. prefix = ", ";
  269. sb.append("\"");
  270. sb.append(actual[i]);
  271. sb.append("\"");
  272. }
  273. }
  274. sb.append("] missing [");
  275. prefix = "";
  276. for (int i = 0; i < expected.length; i++) {
  277. if (null != expected[i]) {
  278. sb.append(prefix);
  279. prefix = ", ";
  280. sb.append("\"");
  281. sb.append(expected[i]);
  282. sb.append("\"");
  283. }
  284. }
  285. sb.append("]");
  286. return true;
  287. }
  288. return false;
  289. }
  290. private static final String[] copy(String[] ra) {
  291. if (null == ra) {
  292. return new String[0];
  293. }
  294. String[] result = new String[ra.length];
  295. System.arraycopy(ra, 0, result, 0, ra.length);
  296. return result;
  297. }
  298. /**
  299. * Support for OSGI bundles read from manifest files.
  300. * Currently very limited, and will only support the subset of
  301. * features that we use.
  302. * sources:
  303. * http://www-128.ibm.com/developerworks/library/os-ecl-osgi/index.html
  304. * http://help.eclipse.org/help30/index.jsp?topic=/org.eclipse.platform.doc.isv/reference/osgi/org/osgi/framework/Constants.html
  305. */
  306. public static class OSGIBundle {
  307. public static final Name BUNDLE_NAME = new Name("Bundle-Name");
  308. public static final Name BUNDLE_SYMBOLIC_NAME = new Name(
  309. "Bundle-SymbolicName");
  310. public static final Name BUNDLE_VERSION = new Name("Bundle-Version");
  311. public static final Name BUNDLE_ACTIVATOR = new Name("Bundle-Activator");
  312. public static final Name BUNDLE_VENDOR = new Name("Bundle-Vendor");
  313. public static final Name REQUIRE_BUNDLE = new Name("Require-Bundle");
  314. public static final Name IMPORT_PACKAGE = new Name("Import-Package");
  315. public static final Name BUNDLE_CLASSPATH = new Name("Bundle-ClassPath");
  316. /** unmodifiable list of all valid OSGIBundle Name's */
  317. public static final List NAMES;
  318. static {
  319. ArrayList names = new ArrayList();
  320. names.add(BUNDLE_NAME);
  321. names.add(BUNDLE_SYMBOLIC_NAME);
  322. names.add(BUNDLE_VERSION);
  323. names.add(BUNDLE_ACTIVATOR);
  324. names.add(BUNDLE_VENDOR);
  325. names.add(REQUIRE_BUNDLE);
  326. names.add(IMPORT_PACKAGE);
  327. names.add(BUNDLE_CLASSPATH);
  328. NAMES = Collections.unmodifiableList(names);
  329. }
  330. private final Manifest manifest;
  331. private final Attributes attributes;
  332. /**
  333. *
  334. * @param manifestInputStream
  335. * the InputStream of the manifest.mf - will be closed.
  336. * @throws IOException
  337. * if unable to read or close the manifest input stream.
  338. */
  339. public OSGIBundle(InputStream manifestInputStream) throws IOException {
  340. manifest = new Manifest();
  341. manifest.read(manifestInputStream);
  342. manifestInputStream.close();
  343. attributes = manifest.getMainAttributes();
  344. }
  345. public String getAttribute(Name attributeName) {
  346. return attributes.getValue(attributeName);
  347. }
  348. public String[] getClasspath() {
  349. String cp = getAttribute(OSGIBundle.BUNDLE_CLASSPATH);
  350. if (null == cp) {
  351. return new String[0];
  352. }
  353. StringTokenizer st = new StringTokenizer(cp, " ,");
  354. String[] result = new String[st.countTokens()];
  355. int i = 0;
  356. while (st.hasMoreTokens()) {
  357. result[i++] = st.nextToken();
  358. }
  359. return result;
  360. }
  361. /**
  362. * XXX ugly/weak hack only handles a single version comma
  363. * {name};bundle-version="[1.5.0,1.5.5]";resolution:=optional
  364. * @return
  365. */
  366. public RequiredBundle[] getRequiredBundles() {
  367. String value = getAttribute(OSGIBundle.REQUIRE_BUNDLE);
  368. if (null == value) {
  369. return new RequiredBundle[0];
  370. }
  371. StringTokenizer st = new StringTokenizer(value, " ,");
  372. RequiredBundle[] result = new RequiredBundle[st.countTokens()];
  373. int i = 0;
  374. int skips = 0;
  375. while (st.hasMoreTokens()) {
  376. String token = st.nextToken();
  377. int first = token.indexOf("\"");
  378. if (-1 != first) {
  379. if (!st.hasMoreTokens()) {
  380. throw new IllegalArgumentException(token);
  381. }
  382. // just assume only one quoted "," for version?
  383. token += "," + st.nextToken();
  384. skips++;
  385. }
  386. result[i++] = new RequiredBundle(token);
  387. }
  388. if (skips > 0) {
  389. RequiredBundle[] patch = new RequiredBundle[result.length-skips];
  390. System.arraycopy(result, 0, patch, 0, patch.length);
  391. result = patch;
  392. }
  393. return result;
  394. }
  395. /**
  396. * Wrap each dependency on another bundle
  397. */
  398. public static class RequiredBundle {
  399. /** unparsed entry text, for debugging */
  400. final String text;
  401. /** Symbolic name of the required bundle */
  402. final String name;
  403. /** if not null, then start/end versions of required bundle
  404. * in the format of the corresponding manifest entry
  405. */
  406. final String versions;
  407. /** if true, then required bundle is optional */
  408. final boolean optional;
  409. private RequiredBundle(String entry) {
  410. text = entry;
  411. StringTokenizer st = new StringTokenizer(entry, ";");
  412. name = st.nextToken();
  413. String vers = null;
  414. String opt = null;
  415. // bundle-version="[1.5.0,1.5.5]";resolution:=optiona
  416. final String RESOLUTION = "resolution:=";
  417. final String VERSION = "bundle-version=\"";
  418. while (st.hasMoreTokens()) {
  419. String token = st.nextToken();
  420. if (token.startsWith(VERSION)) {
  421. int start = VERSION.length();
  422. int end = token.lastIndexOf("\"");
  423. vers = token.substring(start, end);
  424. // e.g., [1.5.0,1.5.5)
  425. } else if (token.startsWith(RESOLUTION)) {
  426. int start = RESOLUTION.length();
  427. int end = token.length();
  428. opt = token.substring(start, end);
  429. }
  430. }
  431. versions = vers;
  432. optional = "optional".equals(opt);
  433. }
  434. }
  435. }
  436. }