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.

IncrementalStateManager.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* *******************************************************************
  2. * Copyright (c) 2005 Contributors.
  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://eclipse.org/legal/epl-v10.html
  8. *
  9. * Contributors:
  10. * Andy Clement initial implementation
  11. * ******************************************************************/
  12. package org.aspectj.ajdt.internal.core.builder;
  13. import java.io.File;
  14. import java.io.FileNotFoundException;
  15. import java.io.FileOutputStream;
  16. import java.io.IOException;
  17. import java.util.Collection;
  18. import java.util.Hashtable;
  19. import java.util.Iterator;
  20. import java.util.List;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import org.aspectj.ajdt.internal.compiler.CompilationResultDestinationManager;
  24. import org.aspectj.weaver.CompressingDataOutputStream;
  25. /**
  26. * Central point for all things incremental... - keeps track of the state recorded for each different config file - allows limited
  27. * interaction with these states
  28. *
  29. * - records dependency/change info for particular classpaths > this will become what JDT keeps in its 'State' object when its
  30. * finished
  31. */
  32. public class IncrementalStateManager {
  33. // FIXME asc needs an API through Ajde for trashing its contents
  34. // FIXME asc needs some memory mgmt (softrefs?) to recover memory
  35. // SECRETAPI will consume more memory, so turn on at your own risk ;) Set to 'true' when memory usage is understood
  36. public static boolean recordIncrementalStates = false;
  37. public static boolean debugIncrementalStates = false;
  38. private static Hashtable<String, AjState> incrementalStates = new Hashtable<String, AjState>();
  39. public static void recordSuccessfulBuild(String buildConfig, AjState state) {
  40. if (!recordIncrementalStates) {
  41. return;
  42. }
  43. incrementalStates.put(buildConfig, state);
  44. // persist();
  45. }
  46. /**
  47. * Store states on disk
  48. */
  49. public static void persist() {
  50. // check serialization works
  51. Set<Map.Entry<String, AjState>> entries = incrementalStates.entrySet();
  52. for (Iterator<Map.Entry<String, AjState>> iterator = entries.iterator(); iterator.hasNext();) {
  53. Map.Entry<String, AjState> entry = iterator.next();
  54. System.out.println("Name " + entry.getKey());
  55. File f = new File("n:/temp/foo.ajstate");
  56. try {
  57. AjState state = (AjState) entry.getValue();
  58. CompressingDataOutputStream dos = new CompressingDataOutputStream(new FileOutputStream(f));
  59. state.write(dos);
  60. dos.close();
  61. } catch (FileNotFoundException e) {
  62. throw new RuntimeException(e);
  63. } catch (IOException e) {
  64. throw new RuntimeException(e);
  65. }
  66. }
  67. }
  68. public static boolean removeIncrementalStateInformationFor(String buildConfig) {
  69. return incrementalStates.remove(buildConfig) != null;
  70. }
  71. public static void clearIncrementalStates() {
  72. for (Iterator iter = incrementalStates.values().iterator(); iter.hasNext();) {
  73. AjState element = (AjState) iter.next();
  74. element.wipeAllKnowledge();
  75. }
  76. incrementalStates.clear();
  77. // AsmManager.getDefault().createNewStructureModel(); // forget what you know...
  78. }
  79. public static Set getConfigFilesKnown() {
  80. return incrementalStates.keySet();
  81. }
  82. public static AjState retrieveStateFor(String configFile) {
  83. return (AjState) incrementalStates.get(configFile);
  84. }
  85. // now, managing changes to entries on a classpath
  86. public static AjState findStateManagingOutputLocation(File location) {
  87. Collection<AjState> allStates = incrementalStates.values();
  88. if (debugIncrementalStates) {
  89. System.err.println("> findStateManagingOutputLocation(" + location + ") has " + allStates.size()
  90. + " states to look through");
  91. }
  92. for (Iterator<AjState> iter = allStates.iterator(); iter.hasNext();) {
  93. AjState element = iter.next();
  94. AjBuildConfig ajbc = element.getBuildConfig();
  95. if (ajbc == null) {
  96. // FIXME asc why can it ever be null?
  97. if (debugIncrementalStates) {
  98. System.err.println(" No build configuration for state " + element);
  99. }
  100. continue;
  101. }
  102. File outputDir = ajbc.getOutputDir();
  103. if (outputDir != null && outputDir.equals(location)) {
  104. if (debugIncrementalStates) {
  105. System.err.println("< findStateManagingOutputLocation(" + location + ") returning " + element);
  106. }
  107. return element;
  108. }
  109. CompilationResultDestinationManager outputManager = ajbc.getCompilationResultDestinationManager();
  110. if (outputManager != null) {
  111. List outputDirs = outputManager.getAllOutputLocations();
  112. for (Iterator iterator = outputDirs.iterator(); iterator.hasNext();) {
  113. File dir = (File) iterator.next();
  114. if (dir.equals(location)) {
  115. if (debugIncrementalStates) {
  116. System.err.println("< findStateManagingOutputLocation(" + location + ") returning " + element);
  117. }
  118. return element;
  119. }
  120. }
  121. }
  122. if (outputDir == null && outputManager == null) {
  123. // FIXME why can it ever be null? due to using outjar?
  124. if (debugIncrementalStates) {
  125. System.err.println(" output directory and output location manager for " + ajbc + " are null");
  126. }
  127. continue;
  128. }
  129. }
  130. if (debugIncrementalStates) {
  131. System.err.println("< findStateManagingOutputLocation(" + location + ") returning null");
  132. }
  133. return null;
  134. }
  135. // FIXME asc needs a persistence mechanism for storing/loading all state info
  136. // FIXME asc needs to understand two config files might point at the same output dir... what to do about this?
  137. }