]> source.dussan.org Git - aspectj.git/blob
aeac71571db9bf7bd1c183104585714a945182c2
[aspectj.git] /
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
14 import java.io.File;
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;
22 import java.util.Map;
23 import java.util.Set;
24
25 import org.aspectj.ajdt.internal.compiler.CompilationResultDestinationManager;
26 import org.aspectj.weaver.CompressingDataOutputStream;
27
28 /**
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
31  * 
32  * - records dependency/change info for particular classpaths > this will become what JDT keeps in its 'State' object when its
33  * finished
34  */
35 public class IncrementalStateManager {
36
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>();
43
44         public static void recordSuccessfulBuild(String buildConfig, AjState state) {
45                 if (!recordIncrementalStates) {
46                         return;
47                 }
48                 incrementalStates.put(buildConfig, state);
49                 // persist();
50         }
51
52         /**
53          * Store states on disk
54          */
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");
61                         try {
62                                 AjState state = (AjState) entry.getValue();
63                                 CompressingDataOutputStream dos = new CompressingDataOutputStream(new FileOutputStream(f));
64                                 state.write(dos);
65                                 dos.close();
66                         } catch (FileNotFoundException e) {
67                                 throw new RuntimeException(e);
68                         } catch (IOException e) {
69                                 throw new RuntimeException(e);
70                         }
71                 }
72         }
73
74         public static boolean removeIncrementalStateInformationFor(String buildConfig) {
75                 return incrementalStates.remove(buildConfig) != null;
76         }
77
78         public static void clearIncrementalStates() {
79                 for (AjState element : incrementalStates.values()) {
80                         element.wipeAllKnowledge();
81                 }
82                 incrementalStates.clear();
83                 // AsmManager.getDefault().createNewStructureModel(); // forget what you know...
84         }
85
86         public static Set getConfigFilesKnown() {
87                 return incrementalStates.keySet();
88         }
89
90         public static AjState retrieveStateFor(String configFile) {
91                 return (AjState) incrementalStates.get(configFile);
92         }
93
94         // now, managing changes to entries on a classpath
95
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");
101                 }
102                 for (AjState element : allStates) {
103                         AjBuildConfig ajbc = element.getBuildConfig();
104                         if (ajbc == null) {
105                                 // FIXME asc why can it ever be null?
106                                 if (debugIncrementalStates) {
107                                         System.err.println("  No build configuration for state " + element);
108                                 }
109                                 continue;
110                         }
111                         File outputDir = ajbc.getOutputDir();
112                         if (outputDir != null && outputDir.equals(location)) {
113                                 if (debugIncrementalStates) {
114                                         System.err.println("< findStateManagingOutputLocation(" + location + ") returning " + element);
115                                 }
116                                 return element;
117                         }
118                         CompilationResultDestinationManager outputManager = ajbc.getCompilationResultDestinationManager();
119                         if (outputManager != null) {
120                                 List outputDirs = outputManager.getAllOutputLocations();
121                                 for (Object o : outputDirs) {
122                                         File dir = (File) o;
123                                         if (dir.equals(location)) {
124                                                 if (debugIncrementalStates) {
125                                                         System.err.println("< findStateManagingOutputLocation(" + location + ") returning " + element);
126                                                 }
127                                                 return element;
128                                         }
129                                 }
130                         }
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");
135                                 }
136                                 continue;
137                         }
138
139                 }
140                 if (debugIncrementalStates) {
141                         System.err.println("< findStateManagingOutputLocation(" + location + ") returning null");
142                 }
143                 return null;
144         }
145
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?
148 }