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