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.

Validator.java 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. * Xerox/PARC initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.testing.harness.bridge;
  13. import java.io.File;
  14. import java.util.ArrayList;
  15. import java.util.Arrays;
  16. import java.util.Collection;
  17. import java.util.Iterator;
  18. import java.util.ListIterator;
  19. import java.util.Stack;
  20. import org.aspectj.bridge.AbortException;
  21. import org.aspectj.bridge.IMessage;
  22. import org.aspectj.bridge.IMessageHandler;
  23. import org.aspectj.bridge.MessageUtil;
  24. import org.aspectj.util.FileUtil;
  25. import org.aspectj.util.LangUtil;
  26. /**
  27. * Check input and implement defaults.
  28. * This handles failure messaging and collecting temp directories
  29. * for later cleanup.
  30. * The default behavior is to send a fail message to the message
  31. * handler. Clients may instead:
  32. * <li>Toggle abortOnException to throw AbortException on failure</li>
  33. * <li>push their own message handler to redirect messages
  34. * (pop to remove if any pushed)</li>
  35. * <li>subclass this to reimplement <code>fail(String)</code>
  36. * <p>
  37. * A component given this to do validation should
  38. * not change the reporting scheme established by the caller,
  39. * so the caller may lock and unlock the error handling policy.
  40. * When the policy is locked, this silently ignores attempts
  41. * to toggle exceptions, or delete temporary files.
  42. * XXX callers cannot prevent others from pushing other error handlers.
  43. */
  44. public class Validator {
  45. /** stack of handlers */
  46. private final Stack<IMessageHandler> handlers;
  47. /** list of File registered for deletion on demand */
  48. private final ArrayList<File> tempFiles; // deleteTempFiles requires ListIterator.remove()
  49. /** list of Sandboxes registered for cleanup on demand */
  50. private final ArrayList<Sandbox> sandboxes;
  51. /** if true, throw AbortException on failure */
  52. boolean abortOnFailure;
  53. /** this object prevents any changes to error-handling policy */
  54. private Object locker;
  55. public Validator(IMessageHandler handler) {
  56. tempFiles = new ArrayList<>();
  57. sandboxes = new ArrayList<>();
  58. handlers = new Stack<>();
  59. pushHandler(handler);
  60. }
  61. /**
  62. * Push IMessageHandler onto stack,
  63. * so it will be used until the next push or pop
  64. * @param handler not null
  65. *
  66. */
  67. public void pushHandler(IMessageHandler handler) {
  68. LangUtil.throwIaxIfNull(handler, "handler");
  69. handlers.push(handler);
  70. }
  71. /** @throws IllegalStateException if handler is not on top */
  72. public void popHandler(IMessageHandler handler) {
  73. LangUtil.throwIaxIfNull(handler, "handler");
  74. if (handler != handlers.peek()) {
  75. throw new IllegalStateException("not current handler");
  76. }
  77. handlers.pop();
  78. }
  79. /** @return true if this requestor now has locked the error handling policy */
  80. public boolean lock(Object requestor) {
  81. if (null == locker) {
  82. locker = requestor;
  83. }
  84. return (locker == requestor);
  85. }
  86. /** @return true if the error handling policy is now unlocked */
  87. public boolean unlock(Object requestor) {
  88. if (requestor == locker) {
  89. locker = null;
  90. }
  91. return (locker == null);
  92. }
  93. public void setAbortOnFailure(boolean abortOnFailure) {
  94. if (null == locker) {
  95. if (this.abortOnFailure != abortOnFailure) {
  96. this.abortOnFailure = abortOnFailure;
  97. }
  98. }
  99. }
  100. /**
  101. * May fail with any of the messages
  102. * <li>{null check} array<li>
  103. * <li>{null check} {message}[#}<li>
  104. */
  105. public boolean nullcheck(Object[] ra, String message) {
  106. return ((nullcheck((Object) ra, message + " array"))
  107. && nullcheck(Arrays.asList(ra), message));
  108. }
  109. /**
  110. * Like nullcheck(Collection, message), except adding lower and upper bound
  111. * @param atLeast fail if list size is smaller than this
  112. * @param atMost fail if list size is greater than this
  113. */
  114. public boolean nullcheck(Collection list, int atLeast, int atMost, String message) {
  115. if (nullcheck(list, message)) {
  116. int size = list.size();
  117. if (size < atLeast) {
  118. fail(message + ": " + size + "<" + atLeast);
  119. } else if (size > atMost) {
  120. fail(message + ": " + size + ">" + atMost);
  121. } else {
  122. return true;
  123. }
  124. }
  125. return false;
  126. }
  127. /**
  128. * May fail with any of the messages
  129. * <li>{null check} list<li>
  130. * <li>{null check} {message}[#}<li>
  131. */
  132. public boolean nullcheck(Collection list, String message) {
  133. if (nullcheck((Object) list, message + " list")) {
  134. int i = 0;
  135. for (Iterator iter = list.iterator(); iter.hasNext();) {
  136. if (!nullcheck(iter.next(), message + "[" + i++ + "]")) {
  137. return false;
  138. }
  139. }
  140. return true;
  141. }
  142. return false;
  143. }
  144. /**
  145. * May fail with the message "null {message}"
  146. * if o and the def{ault} are null
  147. * @return o if not null or default otherwise
  148. */
  149. public Object nulldefault(Object o, String message, Object def) {
  150. if (null == o) {
  151. o = def;
  152. }
  153. nullcheck(o, message);
  154. return o;
  155. }
  156. /** may fail with the message "null {message}" */
  157. public boolean nullcheck(Object o, String message) {
  158. if (null == o) {
  159. if (null == message) message = "object";
  160. fail("null " + message);
  161. return false;
  162. }
  163. return true;
  164. }
  165. /**
  166. * Verify that all paths are readable relative to baseDir.
  167. * may fail with the message "cannot read {file}"
  168. */
  169. public boolean canRead(File baseDir, String[] paths, String message) {
  170. if (!canRead(baseDir, "baseDir - " + message)
  171. || !nullcheck(paths, "paths - " + message)) {
  172. return false;
  173. }
  174. final String dirPath = baseDir.getPath();
  175. File[] files = FileUtil.getBaseDirFiles(baseDir, paths);
  176. for (File file : files) {
  177. if (!canRead(file, "{" + dirPath + "} " + file)) {
  178. return false;
  179. }
  180. }
  181. return true;
  182. }
  183. /**
  184. * Verify that all paths are readable relative to baseDir.
  185. * may fail with the message "cannot read {file}"
  186. */
  187. public boolean canRead(File[] files, String message) {
  188. if (!nullcheck(files, message)) {
  189. return false;
  190. }
  191. for (File file : files) {
  192. if (!canRead(file, file.getPath())) {
  193. return false;
  194. }
  195. }
  196. return true;
  197. }
  198. /** may fail with the message "cannot read {file}" */
  199. public boolean canRead(File file, String message) {
  200. if (nullcheck(file, message)) {
  201. if (file.canRead()) {
  202. return true;
  203. } else {
  204. fail("cannot read " + file);
  205. }
  206. }
  207. return false;
  208. }
  209. /** may fail with the message "cannot write {file}" */
  210. public boolean canWrite(File file, String message) {
  211. if (nullcheck(file, message)) {
  212. if (file.canRead()) {
  213. return true;
  214. } else {
  215. fail("cannot write " + file);
  216. }
  217. }
  218. return false;
  219. }
  220. /** may fail with the message "not a directory {file}" */
  221. public boolean canReadDir(File file, String message) {
  222. if (canRead(file, message)) {
  223. if (file.isDirectory()) {
  224. return true;
  225. } else {
  226. fail("not a directory " + file);
  227. }
  228. }
  229. return false;
  230. }
  231. /** may fail with the message "not a directory {file}" */
  232. public boolean canWriteDir(File file, String message) {
  233. if (canWrite(file, message)) {
  234. if (file.isDirectory()) {
  235. return true;
  236. } else {
  237. fail("not a directory " + file);
  238. }
  239. }
  240. return false;
  241. }
  242. /**
  243. * May fail with any of the messages
  244. * <li>{null check} dir array<li>
  245. * <li>"#: not a File {file}"<li>
  246. * <li>{canRead} {message}[#}<li>
  247. */
  248. public boolean canReadFiles(Object[] dirs, String message) {
  249. return ((nullcheck((Object) dirs, message + " dir array"))
  250. && canReadFiles(Arrays.asList(dirs), message));
  251. }
  252. /**
  253. * May fail with any of the messages
  254. * <li>{null check} files<li>
  255. * <li>"#: not a File {file}"<li>
  256. * <li>{canRead} {message}[#}<li>
  257. */
  258. public boolean canReadFiles(Collection dirs, String message) {
  259. if (nullcheck((Object) dirs, message + " files")) {
  260. int i = 0;
  261. for (Iterator iter = dirs.iterator(); iter.hasNext();) {
  262. Object o = iter.next();
  263. if (! (o instanceof File)) {
  264. fail(i + ": not a file " + o);
  265. }
  266. if (!canRead((File) o, message + "[" + i++ + "]")) {
  267. return false;
  268. }
  269. }
  270. return true;
  271. }
  272. return false;
  273. }
  274. /**
  275. * May fail with any of the messages
  276. * <li>{null check} dir array<li>
  277. * <li>"#: not a File {file}"<li>
  278. * <li>{canReadDir} {message}[#}<li>
  279. */
  280. public boolean canReadDirs(Object[] dirs, String message) {
  281. return ((nullcheck((Object) dirs, message + " dir array"))
  282. && canReadDirs(Arrays.asList(dirs), message));
  283. }
  284. /**
  285. * May fail with any of the messages
  286. * <li>{null check} dirs<li>
  287. * <li>"#: not a File {file}"<li>
  288. * <li>{canReadDir} {message}[#}<li>
  289. */
  290. public boolean canReadDirs(Collection dirs, String message) {
  291. if (nullcheck((Object) dirs, message + " dirs")) {
  292. int i = 0;
  293. for (Iterator iter = dirs.iterator(); iter.hasNext();) {
  294. Object o = iter.next();
  295. if (! (o instanceof File)) {
  296. fail(i + ": not a file " + o);
  297. }
  298. if (!canReadDir((File) o, message + "[" + i++ + "]")) {
  299. return false;
  300. }
  301. }
  302. return true;
  303. }
  304. return false;
  305. }
  306. /**
  307. * May fail with any of the messages
  308. * <li>{null check} dir array<li>
  309. * <li>"#: not a File {file}"<li>
  310. * <li>{canWrite} {message}[#}<li>
  311. */
  312. public boolean canWriteFiles(Object[] dirs, String message) {
  313. return ((nullcheck((Object) dirs, message + " dir array"))
  314. && canWriteFiles(Arrays.asList(dirs), message));
  315. }
  316. /**
  317. * May fail with any of the messages
  318. * <li>{null check} files<li>
  319. * <li>"#: not a File {file}"<li>
  320. * <li>{canWrite} {message}[#}<li>
  321. */
  322. public boolean canWriteFiles(Collection dirs, String message) {
  323. if (nullcheck((Object) dirs, message + " files")) {
  324. int i = 0;
  325. for (Iterator iter = dirs.iterator(); iter.hasNext();) {
  326. Object o = iter.next();
  327. if (! (o instanceof File)) {
  328. fail(i + ": not a file " + o);
  329. }
  330. if (!canWrite((File) o, message + "[" + i++ + "]")) {
  331. return false;
  332. }
  333. }
  334. return true;
  335. }
  336. return false;
  337. }
  338. /**
  339. * May fail with any of the messages
  340. * <li>{null check} dir array<li>
  341. * <li>"#: not a File {file}"<li>
  342. * <li>{canWriteDir} {message}[#}<li>
  343. */
  344. public boolean canWriteDirs(Object[] dirs, String message) {
  345. return ((nullcheck((Object) dirs, message + " dir array"))
  346. && canWriteDirs(Arrays.asList(dirs), message));
  347. }
  348. /**
  349. * May fail with any of the messages
  350. * <li>{null check} dirs<li>
  351. * <li>"#: not a File {file}"<li>
  352. * <li>{canWriteDir} {message}[#}<li>
  353. */
  354. public boolean canWriteDirs(Collection dirs, String message) {
  355. if (nullcheck((Object) dirs, message + " dirs")) {
  356. int i = 0;
  357. for (Iterator iter = dirs.iterator(); iter.hasNext();) {
  358. Object o = iter.next();
  359. if (! (o instanceof File)) {
  360. fail(i + ": not a file " + o);
  361. }
  362. if (!canWriteDir((File) o, message + "[" + i++ + "]")) {
  363. return false;
  364. }
  365. }
  366. return true;
  367. }
  368. return false;
  369. }
  370. /**
  371. * Send an info message to any underlying handler
  372. * @param message ignored if null
  373. */
  374. public void info(String message) {
  375. if (null != message) {
  376. IMessageHandler handler = getHandler();
  377. MessageUtil.info(handler, message);
  378. }
  379. }
  380. /** Fail via message or AbortException */
  381. public void fail(String message) {
  382. fail(message, (Throwable) null);
  383. }
  384. /**
  385. * Fail via message or AbortException.
  386. * All failure messages go through here,
  387. * so subclasses may override to control
  388. * failure-handling.
  389. */
  390. public void fail(String message, Throwable thrown) {
  391. if ((null == message) && (null == thrown)) {
  392. message = "<Validator:no message>";
  393. }
  394. IMessage m = MessageUtil.fail(message, thrown);
  395. if (abortOnFailure) {
  396. throw new AbortException(m);
  397. } else {
  398. IMessageHandler handler = getHandler();
  399. handler.handleMessage(m);
  400. }
  401. }
  402. /**
  403. * Register a file temporary, i.e., to be
  404. * deleted on completion. The file need not
  405. * exist (yet or ever) and may be a duplicate
  406. * of existing files registered.
  407. */
  408. public void registerTempFile(File file) {
  409. if (null != file) {
  410. tempFiles.add(file);
  411. }
  412. }
  413. /**
  414. * Get a writable {possibly-empty} directory.
  415. * If the input dir is null, then try to create a temporary
  416. * directory using name.
  417. * If the input dir is not null, this tries to
  418. * create it if it does not exist.
  419. * Then if name is not null,
  420. * it tries to get a temporary directory
  421. * under this using name.
  422. * If name is null, "Validator" is used.
  423. * If deleteContents is true, this will try to delete
  424. * any existing contents. If this is unable to delete
  425. * the contents of the input directory, this may return
  426. * a new, empty temporary directory.
  427. * If register is true, then any directory returned is
  428. * saved for later deletion using <code>deleteTempDirs()</code>,
  429. * including the input directory.
  430. * When this is unable to create a result, if failMessage
  431. * is not null then this will fail; otherwise it returns false;
  432. */
  433. public File getWritableDir(File dir, String name,
  434. boolean deleteContents, boolean register, String failMessage) {
  435. // check dir
  436. if (null == dir) {
  437. if (null == name) {
  438. name = "Validator";
  439. }
  440. dir = FileUtil.getTempDir(name);
  441. } else {
  442. if (!dir.exists()) {
  443. dir.mkdirs();
  444. }
  445. }
  446. // fail if necessary
  447. if ((null == dir) || (!dir.exists())) {
  448. if (null != failMessage) {
  449. fail(failMessage + ": unable to get parent " + dir);
  450. }
  451. } else {
  452. FileUtil.makeNewChildDir(dir, name);
  453. if (deleteContents) {
  454. FileUtil.deleteContents(dir);
  455. }
  456. if (register) {
  457. tempFiles.add(dir);
  458. }
  459. }
  460. return dir;
  461. }
  462. /**
  463. * Delete any temp sandboxes, files or directories saved,
  464. * optionally reporting failures in the normal way.
  465. * This will be ignored unless the failure policy
  466. * is unlocked.
  467. */
  468. public void deleteTempFiles(boolean reportFailures) {
  469. if (null == locker) {
  470. for (ListIterator iter = tempFiles.listIterator(); iter.hasNext();) {
  471. if (deleteFile((File) iter.next(), reportFailures)) {
  472. iter.remove();
  473. }
  474. }
  475. for (ListIterator iter = sandboxes.listIterator(); iter.hasNext();) {
  476. Sandbox sandbox = (Sandbox) iter.next();
  477. // XXX assumes all dirs are in sandboxDir
  478. if (deleteFile(sandbox.sandboxDir, reportFailures)) {
  479. iter.remove();
  480. }
  481. }
  482. }
  483. }
  484. /**
  485. * Manage temp files and directories of registered sandboxes.
  486. * Note that a sandbox may register before it is initialized,
  487. * so this must do nothing other than save the reference.
  488. * @param sandbox the uninitialized Sandbox to track
  489. */
  490. public void registerSandbox(Sandbox sandbox) {
  491. sandboxes.add(sandbox);
  492. }
  493. private boolean deleteFile(File file, boolean reportFailures) {
  494. if (null == file) {
  495. if (reportFailures) {
  496. fail("unable to delete null file");
  497. }
  498. return true; // null file - skip
  499. }
  500. FileUtil.deleteContents(file);
  501. if (file.exists()) {
  502. file.delete();
  503. }
  504. if (!file.exists()) {
  505. return true;
  506. } else if (reportFailures) {
  507. fail("unable to delete " + file);
  508. }
  509. return false;
  510. }
  511. /** @throws IllegalStateException if handler is null */
  512. private IMessageHandler getHandler() {
  513. IMessageHandler handler = handlers.peek();
  514. if (null == handler) {
  515. throw new IllegalStateException("no handler");
  516. }
  517. return handler;
  518. }
  519. } // class Validator