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