Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /* *******************************************************************
  2. * Copyright (c) 2004 IBM Corporation
  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. * Matthew Webster
  11. * ******************************************************************/
  12. package org.aspectj.weaver;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileOutputStream;
  16. import java.io.PrintStream;
  17. import java.text.DateFormat;
  18. import java.text.SimpleDateFormat;
  19. import java.util.Date;
  20. import java.util.HashMap;
  21. import java.util.HashSet;
  22. import java.util.Iterator;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.Properties;
  26. import java.util.Set;
  27. import org.aspectj.bridge.IMessage;
  28. import org.aspectj.bridge.IMessageHolder;
  29. import org.aspectj.bridge.Version;
  30. /**
  31. * @author websterm
  32. *
  33. * To change the template for this generated type comment go to
  34. * Window>Preferences>Java>Code Generation>Code and Comments
  35. */
  36. public class Dump {
  37. /* Format for unique filename based on date & time */
  38. private static final String FILENAME_PREFIX = "ajcore";
  39. private static final DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
  40. private static final DateFormat timeFormat = new SimpleDateFormat("HHmmss.SSS");
  41. private static final String FILENAME_SUFFIX = "txt";
  42. public static final String UNKNOWN_FILENAME = "Unknown";
  43. public static final String DUMP_EXCLUDED = "Excluded";
  44. public static final String NULL_OR_EMPTY = "Empty";
  45. private static Class exceptionClass;
  46. private static IMessage.Kind conditionKind = IMessage.ABORT;
  47. private String reason;
  48. private String fileName;
  49. private PrintStream print;
  50. private static String[] savedCommandLine;
  51. private static List savedFullClasspath;
  52. private static IMessageHolder savedMessageHolder;
  53. private static Map nodes = new HashMap();
  54. private static String lastDumpFileName = UNKNOWN_FILENAME;
  55. /*
  56. * Dump methods
  57. */
  58. public static String dump (String reason) {
  59. String fileName = UNKNOWN_FILENAME;
  60. Dump dump = null;
  61. try {
  62. dump = new Dump(reason);
  63. fileName = dump.getFileName();
  64. dump.dumpDefault();
  65. }
  66. finally {
  67. if (dump != null) dump.close();
  68. }
  69. return fileName;
  70. }
  71. public static String dumpWithException (Throwable th) {
  72. String fileName = UNKNOWN_FILENAME;
  73. Dump dump = null;
  74. try {
  75. dump = new Dump(th.getClass().getName());
  76. fileName = dump.getFileName();
  77. dump.dumpException(th);
  78. }
  79. finally {
  80. if (dump != null) dump.close();
  81. }
  82. return fileName;
  83. }
  84. public static String dumpOnExit () {
  85. if (!shouldDumpOnExit()) return DUMP_EXCLUDED;
  86. String fileName = UNKNOWN_FILENAME;
  87. Dump dump = null;
  88. try {
  89. dump = new Dump(conditionKind.toString());
  90. fileName = dump.getFileName();
  91. dump.dumpDefault();
  92. }
  93. finally {
  94. if (dump != null) dump.close();
  95. }
  96. return fileName;
  97. }
  98. private static boolean shouldDumpOnExit () {
  99. return (savedMessageHolder == null) || savedMessageHolder.hasAnyMessage(conditionKind,true);
  100. }
  101. /*
  102. * Dump configuration
  103. */
  104. public static void setDumpOnException (boolean b) {
  105. if (b) {
  106. exceptionClass = java.lang.Throwable.class;
  107. }
  108. else {
  109. exceptionClass = null;
  110. }
  111. }
  112. public static boolean getDumpOnException () {
  113. return (exceptionClass != null);
  114. }
  115. public static boolean setDumpOnExit (IMessage.Kind condition) {
  116. conditionKind = condition;
  117. return true;
  118. }
  119. public static boolean setDumpOnExit (String condition) {
  120. for (Iterator i = IMessage.KINDS.iterator(); i.hasNext();) {
  121. IMessage.Kind kind = (IMessage.Kind)i.next();
  122. if (kind.toString().equals(condition)) {
  123. return setDumpOnExit(kind);
  124. }
  125. }
  126. return false;
  127. }
  128. public static IMessage.Kind getDumpOnExit () {
  129. return conditionKind;
  130. }
  131. public static String getLastDumpFileName () {
  132. return lastDumpFileName;
  133. }
  134. /*
  135. * Dump registration
  136. */
  137. public static void saveCommandLine (String[] args) {
  138. savedCommandLine = new String[args.length];
  139. System.arraycopy(args,0,savedCommandLine,0,args.length);
  140. }
  141. public static void saveFullClasspath (List list) {
  142. savedFullClasspath = list;
  143. }
  144. public static void saveMessageHolder (IMessageHolder holder) {
  145. savedMessageHolder = holder;
  146. }
  147. public static void registerNode (Class module, INode newNode) {
  148. nodes.put(module,newNode);
  149. }
  150. /*
  151. * Dump methods
  152. */
  153. private Dump (String reason) {
  154. this.reason = reason;
  155. openDump();
  156. dumpAspectJProperties();
  157. dumpDumpConfiguration();
  158. }
  159. public String getFileName() {
  160. return fileName;
  161. }
  162. private void dumpDefault () {
  163. dumpSytemProperties();
  164. dumpCommandLine();
  165. dumpFullClasspath();
  166. dumpCompilerMessages();
  167. /*
  168. * Dump registered nodes
  169. */
  170. IVisitor dumpVisitor = new IVisitor() {
  171. public void visitString (String s) {
  172. println(s);
  173. }
  174. public void visitList (List list) {
  175. println(list);
  176. }
  177. };
  178. for (Iterator i = nodes.keySet().iterator(); i.hasNext();) {
  179. Class module = (Class)i.next();
  180. println("---- " + module.getName() + " ----");
  181. INode dumpNode = (INode)nodes.get(module);
  182. try {
  183. dumpNode.accept(dumpVisitor);
  184. }
  185. catch (Exception ex) {
  186. println(ex.toString());
  187. }
  188. }
  189. }
  190. private void dumpException (Throwable th) {
  191. println("---- Exception Information ---");
  192. println(th);
  193. dumpDefault();
  194. }
  195. private void dumpAspectJProperties () {
  196. println("---- AspectJ Properties ---");
  197. println("AspectJ Compiler " + Version.text + " built on " + Version.time_text);
  198. }
  199. private void dumpDumpConfiguration () {
  200. println("---- Dump Properties ---");
  201. println("Dump file: " + fileName);
  202. println("Dump reason: " + reason);
  203. println("Dump on exception: " + (exceptionClass != null));
  204. println("Dump at exit condition: " + conditionKind);
  205. }
  206. private void dumpFullClasspath () {
  207. println("---- Full Classpath ---");
  208. if (savedFullClasspath != null && savedFullClasspath.size() > 0) {
  209. for (Iterator iter = savedFullClasspath.iterator(); iter.hasNext(); ) {
  210. String fileName = (String)iter.next();
  211. File file = new File(fileName);
  212. println(file);
  213. }
  214. }
  215. else {
  216. println(NULL_OR_EMPTY);
  217. }
  218. }
  219. private void dumpSytemProperties () {
  220. println("---- System Properties ---");
  221. Properties props = System.getProperties();
  222. println(props);
  223. }
  224. private void dumpCommandLine () {
  225. println("---- Command Line ---");
  226. println(savedCommandLine);
  227. }
  228. private void dumpCompilerMessages () {
  229. println("---- Compiler Messages ---");
  230. if (savedMessageHolder != null) for (Iterator i = savedMessageHolder.getUnmodifiableListView().iterator(); i.hasNext(); ) {
  231. IMessage message = (IMessage)i.next();
  232. println(message.toString());
  233. }
  234. else {
  235. println(NULL_OR_EMPTY);
  236. }
  237. }
  238. /*
  239. * Dump output
  240. */
  241. private void openDump () {
  242. if (print != null) return;
  243. Date now = new Date();
  244. fileName = FILENAME_PREFIX + "."
  245. + dateFormat.format(now) + "."
  246. + timeFormat.format(now) + "."
  247. + FILENAME_SUFFIX;
  248. try {
  249. print = new PrintStream(new FileOutputStream(fileName),true);
  250. System.out.println("Dumping to " + fileName);
  251. }
  252. catch (FileNotFoundException ex) {
  253. print = System.err;
  254. System.out.println("Dumping to stderr");
  255. fileName = UNKNOWN_FILENAME;
  256. }
  257. lastDumpFileName = fileName;
  258. }
  259. public void close () {
  260. print.close();
  261. }
  262. private void println (String s) {
  263. print.println(s);
  264. }
  265. private void println (Object[] array) {
  266. if (array == null) {
  267. println(NULL_OR_EMPTY);
  268. return;
  269. }
  270. for (int i = 0; i < array.length; i++) {
  271. print.println(array[i]);
  272. }
  273. }
  274. private void println (Properties props) {
  275. Iterator iter = props.keySet().iterator();
  276. while (iter.hasNext()) {
  277. String key = (String)iter.next();
  278. String value = props.getProperty(key);
  279. print.println(key + "=" + value);
  280. }
  281. }
  282. private void println (Throwable th) {
  283. th.printStackTrace(print);
  284. }
  285. private void println (File file) {
  286. print.print(file.getAbsolutePath());
  287. if (!file.exists()) {
  288. println("(missing)");
  289. }
  290. else if (file.isDirectory()) {
  291. int count = file.listFiles().length;
  292. println("(" + count + " entries)");
  293. }
  294. else {
  295. println("(" + file.length() + " bytes)");
  296. }
  297. }
  298. private void println (List list) {
  299. if (list == null || list.isEmpty()) println(NULL_OR_EMPTY);
  300. else for (Iterator i = list.iterator(); i.hasNext();) {
  301. Object o = i.next();
  302. if (o instanceof Exception) {
  303. println((Exception)o);
  304. } else {
  305. println(i.next().toString());
  306. }
  307. }
  308. }
  309. static {
  310. String exceptionName = System.getProperty("org.aspectj.weaver.Dump.exception","true");
  311. if (!exceptionName.equals("false")) setDumpOnException(true);
  312. String conditionName = System.getProperty("org.aspectj.weaver.Dump.condition","true");
  313. setDumpOnExit(conditionName);
  314. }
  315. public interface INode {
  316. public void accept (IVisitor visior);
  317. }
  318. public interface IVisitor {
  319. public void visitString (String s);
  320. public void visitList (List list);
  321. }
  322. }