Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 937f164d0a05f8b89f4f5d269b953f1ed9d16beb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*******************************************************************************
 * Copyright (c) 2007 Intel Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Intel Corporation - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.managedbuilder.internal.buildmodel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.eclipse.cdt.core.settings.model.util.CDataUtil;
import org.eclipse.cdt.managedbuilder.core.ManagedBuilderCorePlugin;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;

public class ProjectBuildState implements IProjectBuildState {
	private Properties fCfgIdToFileNameProps;
	private Map<String, ConfigurationBuildState> fCfgIdToStateMap = new HashMap<String, ConfigurationBuildState>();
	private IProject fProject;
	private boolean fIsMapInfoDirty;

	public ProjectBuildState(IProject project){
		fProject = project;
	}

	void setProject(IProject project){
		fProject = project;
		Collection<ConfigurationBuildState> cbStates = fCfgIdToStateMap.values();
		for (ConfigurationBuildState cbs : cbStates) {
			cbs.setProject(project);
		}
	}

	@Override
	public IConfigurationBuildState getConfigurationBuildState(String id, boolean create) {
		ConfigurationBuildState state = fCfgIdToStateMap.get(id);
		if(state == null){
			state = loadState(id, create);
			if(state.exists() || create)
				fCfgIdToStateMap.put(id, state);
			else
				state = null;
		}
		return state;
	}

	private ConfigurationBuildState loadState(String id, boolean create){
		File file = getFileForCfg(id, create);
		ConfigurationBuildState bs = new ConfigurationBuildState(fProject, id);
		if(file != null && file.exists()){
			try {
				InputStream iStream = new FileInputStream(file);
				bs.load(iStream);
				iStream.close();
			} catch (FileNotFoundException e) {
				ManagedBuilderCorePlugin.log(e);
			} catch (IOException e) {
				ManagedBuilderCorePlugin.log(e);
			}
		}
		return bs;
	}

	@Override
	public IConfigurationBuildState[] getConfigurationBuildStates() {
		Properties props = getIdToNameProperties();
		List<IConfigurationBuildState> list = new ArrayList<IConfigurationBuildState>(props.size());
		Set<Object> keySet = props.keySet();
		for (Object key : keySet) {
			String id = (String)key;
			IConfigurationBuildState state = getConfigurationBuildState(id, false);
			if(state != null)
				list.add(state);
		}
		return list.toArray(new ConfigurationBuildState[list.size()]);
	}

	@Override
	public void removeConfigurationBuildState(String id) {
		ConfigurationBuildState cbs = (ConfigurationBuildState)getConfigurationBuildState(id, false);
		if(cbs != null){
			cbs.setState(IRebuildState.NEED_REBUILD);
		}
	}

	@Override
	public int getState() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void setState(int state) {
		// TODO Auto-generated method stub
	}
	private static final int OP_CREATE = 1;
	private static final int OP_REMOVE = 2;

	private String getFileName(String id, int op){
		Properties props = getIdToNameProperties();
		String name = props.getProperty(id);
		if(name == null){
			if(op == OP_CREATE){
				name = new Integer(CDataUtil.genRandomNumber()).toString();
				props.setProperty(id, name);
				fIsMapInfoDirty = true;
	//			saveMapFile();
			}
		} else if (op == OP_REMOVE){
			props.remove(id);
			fIsMapInfoDirty = true;
		}
		return name;
	}

	private File getFileForCfg(String id, boolean create){
		String name = getFileName(id, create ? OP_CREATE : 0);
		if(name == null)
			return null;

		IPath path = BuildStateManager.getInstance().getPrefsDirPath(fProject);
		path = path.append(name);
		return path.toFile();
	}

	private void saveMapFile(){
		if(fCfgIdToFileNameProps == null)
			return;

		File file = getMapFile();
		File parent = file.getParentFile();
		if(!parent.exists())
			parent.mkdirs();

		try {
			OutputStream oStream = new FileOutputStream(file);
			fCfgIdToFileNameProps.store(oStream, ""); //$NON-NLS-1$
			oStream.close();
		} catch (FileNotFoundException e) {
			ManagedBuilderCorePlugin.log(e);
		} catch (IOException e) {
			ManagedBuilderCorePlugin.log(e);
		}
	}

	private File getMapFile(){
		IPath path = BuildStateManager.getInstance().getPrefsDirPath(fProject);
		path = path.append(getProjFileName());
		File file = path.toFile();
		return file;
	}

	private Properties getIdToNameProperties(){
		if(fCfgIdToFileNameProps == null){
			fCfgIdToFileNameProps = new Properties();
			File file = getMapFile();
			if(file.exists()){
				try {
					InputStream iStream = new FileInputStream(file);
					fCfgIdToFileNameProps.load(iStream);
					iStream.close();
				} catch (FileNotFoundException e) {
					ManagedBuilderCorePlugin.log(e);
				} catch (IOException e) {
					ManagedBuilderCorePlugin.log(e);
				}
			}
		}
		return fCfgIdToFileNameProps;
	}

	private String getProjFileName(){
		return fProject.getName();
	}

	@Override
	public IProject getProject() {
		return fProject;
	}

	void serialize(){

		Collection<ConfigurationBuildState> cbStates = fCfgIdToStateMap.values();
		for (ConfigurationBuildState s : cbStates) {
			String id = s.getConfigurationId();
			if(!s.exists()){
				File file = getFileForCfg(id, false);
				if(file != null && file.exists()){
					file.delete();
					getFileName(id, OP_REMOVE);
				}
			} else {
				File file = getFileForCfg(id, true);
				File parent = file.getParentFile();
				if(!parent.exists())
					parent.mkdirs();

				try {
					FileOutputStream oStream = new FileOutputStream(file);
					s.store(oStream);
					oStream.close();
				} catch (FileNotFoundException e) {
					ManagedBuilderCorePlugin.log(e);
				} catch (IOException e) {
					ManagedBuilderCorePlugin.log(e);
				}
			}
		}

		if(fIsMapInfoDirty)
			saveMapFile();
	}
}

Back to the top