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 (Map.Entry<String, AjState> entry : entries) {
59 System.out.println("Name " + entry.getKey());
60 File f = new File("n:/temp/foo.ajstate");
62 AjState state = (AjState) entry.getValue();
63 CompressingDataOutputStream dos = new CompressingDataOutputStream(new FileOutputStream(f));
66 } catch (FileNotFoundException e) {
67 throw new RuntimeException(e);
68 } catch (IOException e) {
69 throw new RuntimeException(e);
74 public static boolean removeIncrementalStateInformationFor(String buildConfig) {
75 return incrementalStates.remove(buildConfig) != null;
78 public static void clearIncrementalStates() {
79 for (AjState element : incrementalStates.values()) {
80 element.wipeAllKnowledge();
82 incrementalStates.clear();
83 // AsmManager.getDefault().createNewStructureModel(); // forget what you know...
86 public static Set getConfigFilesKnown() {
87 return incrementalStates.keySet();
90 public static AjState retrieveStateFor(String configFile) {
91 return (AjState) incrementalStates.get(configFile);
94 // now, managing changes to entries on a classpath
96 public static AjState findStateManagingOutputLocation(File location) {
97 Collection<AjState> allStates = incrementalStates.values();
98 if (debugIncrementalStates) {
99 System.err.println("> findStateManagingOutputLocation(" + location + ") has " + allStates.size()
100 + " states to look through");
102 for (AjState element : allStates) {
103 AjBuildConfig ajbc = element.getBuildConfig();
105 // FIXME asc why can it ever be null?
106 if (debugIncrementalStates) {
107 System.err.println(" No build configuration for state " + element);
111 File outputDir = ajbc.getOutputDir();
112 if (outputDir != null && outputDir.equals(location)) {
113 if (debugIncrementalStates) {
114 System.err.println("< findStateManagingOutputLocation(" + location + ") returning " + element);
118 CompilationResultDestinationManager outputManager = ajbc.getCompilationResultDestinationManager();
119 if (outputManager != null) {
120 List outputDirs = outputManager.getAllOutputLocations();
121 for (Object o : outputDirs) {
123 if (dir.equals(location)) {
124 if (debugIncrementalStates) {
125 System.err.println("< findStateManagingOutputLocation(" + location + ") returning " + element);
131 if (outputDir == null && outputManager == null) {
132 // FIXME why can it ever be null? due to using outjar?
133 if (debugIncrementalStates) {
134 System.err.println(" output directory and output location manager for " + ajbc + " are null");
140 if (debugIncrementalStates) {
141 System.err.println("< findStateManagingOutputLocation(" + location + ") returning null");
146 // FIXME asc needs a persistence mechanism for storing/loading all state info
147 // FIXME asc needs to understand two config files might point at the same output dir... what to do about this?