]> source.dussan.org Git - aspectj.git/blob
e255761b6082b4fbc7f77478428430d7ec1f51db
[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 (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");
62                         try {
63                                 AjState state = (AjState) entry.getValue();
64                                 CompressingDataOutputStream dos = new CompressingDataOutputStream(new FileOutputStream(f));
65                                 state.write(dos);
66                                 dos.close();
67                         } catch (FileNotFoundException e) {
68                                 throw new RuntimeException(e);
69                         } catch (IOException e) {
70                                 throw new RuntimeException(e);
71                         }
72                 }
73         }
74
75         public static boolean removeIncrementalStateInformationFor(String buildConfig) {
76                 return incrementalStates.remove(buildConfig) != null;
77         }
78
79         public static void clearIncrementalStates() {
80                 for (Iterator iter = incrementalStates.values().iterator(); iter.hasNext();) {
81                         AjState element = (AjState) iter.next();
82                         element.wipeAllKnowledge();
83                 }
84                 incrementalStates.clear();
85                 // AsmManager.getDefault().createNewStructureModel(); // forget what you know...
86         }
87
88         public static Set getConfigFilesKnown() {
89                 return incrementalStates.keySet();
90         }
91
92         public static AjState retrieveStateFor(String configFile) {
93                 return (AjState) incrementalStates.get(configFile);
94         }
95
96         // now, managing changes to entries on a classpath
97
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");
103                 }
104                 for (Iterator<AjState> iter = allStates.iterator(); iter.hasNext();) {
105                         AjState element = iter.next();
106                         AjBuildConfig ajbc = element.getBuildConfig();
107                         if (ajbc == null) {
108                                 // FIXME asc why can it ever be null?
109                                 if (debugIncrementalStates) {
110                                         System.err.println("  No build configuration for state " + element);
111                                 }
112                                 continue;
113                         }
114                         File outputDir = ajbc.getOutputDir();
115                         if (outputDir != null && outputDir.equals(location)) {
116                                 if (debugIncrementalStates) {
117                                         System.err.println("< findStateManagingOutputLocation(" + location + ") returning " + element);
118                                 }
119                                 return element;
120                         }
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);
129                                                 }
130                                                 return element;
131                                         }
132                                 }
133                         }
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");
138                                 }
139                                 continue;
140                         }
141
142                 }
143                 if (debugIncrementalStates) {
144                         System.err.println("< findStateManagingOutputLocation(" + location + ") returning null");
145                 }
146                 return null;
147         }
148
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?
151 }