|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 * Trace project file manager |
|
17 * |
|
18 */ |
|
19 package com.nokia.tracecompiler.engine.project; |
|
20 |
|
21 import java.io.File; |
|
22 import java.io.IOException; |
|
23 import java.io.OutputStream; |
|
24 import java.util.Enumeration; |
|
25 |
|
26 import com.nokia.tracecompiler.engine.TraceCompilerEngineBase; |
|
27 import com.nokia.tracecompiler.engine.TraceCompilerEngineErrorCodes.TraceCompilerErrorCode; |
|
28 import com.nokia.tracecompiler.engine.plugin.TraceAPIPluginManager; |
|
29 import com.nokia.tracecompiler.file.FileUtils; |
|
30 import com.nokia.tracecompiler.model.TraceCompilerException; |
|
31 import com.nokia.tracecompiler.model.TraceModel; |
|
32 import com.nokia.tracecompiler.project.GroupNames; |
|
33 import com.nokia.tracecompiler.project.ProjectUtils; |
|
34 |
|
35 /** |
|
36 * Trace project engine |
|
37 * |
|
38 */ |
|
39 public final class ProjectEngine extends TraceCompilerEngineBase { |
|
40 |
|
41 /** |
|
42 * Parameters for create project |
|
43 */ |
|
44 private class CreateProjectParameters { |
|
45 |
|
46 /** |
|
47 * Trace project path |
|
48 */ |
|
49 String traceProjectPath; |
|
50 |
|
51 /** |
|
52 * Trace project name |
|
53 */ |
|
54 String traceProjectName; |
|
55 |
|
56 /** |
|
57 * Trace project ID |
|
58 */ |
|
59 int traceProjectID; |
|
60 |
|
61 } |
|
62 |
|
63 /** |
|
64 * Default project ID |
|
65 */ |
|
66 private static final int DEFAULT_PROJECT_ID = 0x0; // CodForChk_Dis_Magic |
|
67 |
|
68 /** |
|
69 * Trace model |
|
70 */ |
|
71 private TraceModel model; |
|
72 |
|
73 /** |
|
74 * Trace directory name |
|
75 */ |
|
76 public static String traceFolderName; |
|
77 |
|
78 /** |
|
79 * Constructor |
|
80 * |
|
81 * @param model |
|
82 * the trace model |
|
83 */ |
|
84 public ProjectEngine(TraceModel model) { |
|
85 this.model = model; |
|
86 } |
|
87 |
|
88 /** |
|
89 * Opens trace project |
|
90 * |
|
91 * @param projectPath |
|
92 * the path to the project to be opened |
|
93 * @param modelName |
|
94 * the name for the model or null to use directory name |
|
95 * @throws TraceCompilerException |
|
96 * if opening fails |
|
97 */ |
|
98 public void openTraceProject(String projectPath, String modelName) |
|
99 throws TraceCompilerException { |
|
100 if (projectPath != null) { |
|
101 CreateProjectParameters parameters = createParameters(projectPath, |
|
102 modelName); |
|
103 |
|
104 // Create empty project |
|
105 if (model.getExtension(TraceCompilerProject.class) == null) { |
|
106 createEmptyProjectFile(parameters); |
|
107 } |
|
108 } else { |
|
109 |
|
110 // If fileName is null, there's no open source files. In |
|
111 // that the project cannot be created |
|
112 throw new TraceCompilerException( |
|
113 TraceCompilerErrorCode.SOURCE_NOT_OPEN); |
|
114 } |
|
115 } |
|
116 |
|
117 /* |
|
118 * (non-Javadoc) |
|
119 * |
|
120 * @see com.nokia.tracecompiler.engine.TraceCompilerEngine#projectOpened() |
|
121 */ |
|
122 @Override |
|
123 public void projectOpened() { |
|
124 } |
|
125 |
|
126 /* |
|
127 * (non-Javadoc) |
|
128 * |
|
129 * @see com.nokia.tracecompiler.engine.TraceCompilerEngine#projectClosed() |
|
130 */ |
|
131 @Override |
|
132 public void projectClosed() { |
|
133 model.removeExtensions(TraceCompilerProject.class); |
|
134 model.removeExtensions(TraceIDCache.class); |
|
135 } |
|
136 |
|
137 /* |
|
138 * (non-Javadoc) |
|
139 * |
|
140 * @see com.nokia.tracecompiler.engine.TraceCompilerEngine#exportProject() |
|
141 */ |
|
142 @Override |
|
143 public void exportProject() { |
|
144 try { |
|
145 |
|
146 // The fixed id definitions file is updated even there are no traces |
|
147 // in case that old fixed id definitions file exist |
|
148 syncFixedIdDefinitionsFile(); |
|
149 } catch (TraceCompilerException e) { |
|
150 } |
|
151 } |
|
152 |
|
153 /** |
|
154 * Updated fixed id definitions file |
|
155 * |
|
156 * @throws TraceCompilerException |
|
157 * if update fails |
|
158 */ |
|
159 private void syncFixedIdDefinitionsFile() throws TraceCompilerException { |
|
160 TraceIDCache cache = model.getExtension(TraceIDCache.class); |
|
161 // Create trace Id cache if it does not exist |
|
162 if (cache == null) { |
|
163 String path = ProjectUtils.getLocationForFile(model, |
|
164 ProjectEngine.traceFolderName, |
|
165 ProjectConstants.FIXED_ID_DEFINITIONS_FILE_NAME, false); |
|
166 if (path != null) { |
|
167 cache = new TraceIDCache(new File(path).getParent()); |
|
168 model.addExtension(cache); |
|
169 } |
|
170 } |
|
171 if (cache != null) { |
|
172 File cacheFile = new File(cache.getAbsolutePath()); |
|
173 |
|
174 // The fixed id definitions file is updated in case that there are |
|
175 // traces in model |
|
176 if (model.hasTraces()) { |
|
177 // Try to save Ids to fixed Id definition file |
|
178 try { |
|
179 SortedProperties ids = new SortedProperties(); |
|
180 // Save Ids from model to fixed Id properties |
|
181 model.saveIDs(ids); |
|
182 |
|
183 // If there are some fixed Ids in that are not used anymore, |
|
184 // keep also those Ids in defininiton file, but mark those |
|
185 // ids as obsolete |
|
186 ids = handleObsoleteIds(ids); |
|
187 |
|
188 // Rewrites the trace Id cache file |
|
189 OutputStream fos = FileUtils.createOutputStream(cacheFile); |
|
190 ids.store(fos, |
|
191 ProjectConstants.FIXED_ID_DEFINITION_FILE_TITLE); |
|
192 fos.close(); |
|
193 cache.postFileWrittenEvent(cache.getAbsolutePath()); |
|
194 } catch (IOException e) { |
|
195 cacheFile.delete(); |
|
196 } |
|
197 } |
|
198 } |
|
199 } |
|
200 |
|
201 /** |
|
202 * Handle obsolete Ids |
|
203 * |
|
204 * @param ids |
|
205 * Ids |
|
206 */ |
|
207 private SortedProperties handleObsoleteIds(SortedProperties ids) { |
|
208 SortedProperties fixedIds = model.getFixedIds(); |
|
209 if (fixedIds != null) { |
|
210 Enumeration<Object> fixedIdKeys = fixedIds.keys(); |
|
211 while (fixedIdKeys.hasMoreElements()) { |
|
212 String fixedIdKey = (String) fixedIdKeys.nextElement(); |
|
213 if (!ids.containsKey(fixedIdKey)) { |
|
214 String value = fixedIds.getProperty(fixedIdKey); |
|
215 boolean markAsObsolete = true; |
|
216 // In case of groups we only mark user defined |
|
217 // groups as obsolete |
|
218 if (fixedIdKey.startsWith(model.GROUP_PROPERTY_PREFIX)) { |
|
219 int valueAsInt = 0; |
|
220 try { |
|
221 valueAsInt = Integer.decode(value).intValue(); |
|
222 } catch (NumberFormatException e) { |
|
223 // Corrupted. |
|
224 valueAsInt = 0; |
|
225 } |
|
226 if (valueAsInt < GroupNames.USER_GROUP_ID_FIRST) { |
|
227 markAsObsolete = false; |
|
228 } |
|
229 } |
|
230 if (markAsObsolete) { |
|
231 // If OBSOLETE tag text already exit, do not |
|
232 // add that again |
|
233 if (fixedIdKey |
|
234 .startsWith(model.OBSOLETE_PROPERTY_PREFIX)) { |
|
235 ids.setProperty(fixedIdKey, value); |
|
236 } else { |
|
237 ids.setProperty(model.OBSOLETE_PROPERTY_PREFIX |
|
238 + fixedIdKey, value); |
|
239 } |
|
240 } |
|
241 } |
|
242 } |
|
243 } |
|
244 return ids; |
|
245 } |
|
246 |
|
247 /** |
|
248 * Creates the parameters for new project |
|
249 * |
|
250 * @param projectPath |
|
251 * the project path |
|
252 * @param projectName |
|
253 * the name for the project |
|
254 * @return the parameters |
|
255 */ |
|
256 private CreateProjectParameters createParameters(String projectPath, |
|
257 String projectName) { |
|
258 CreateProjectParameters queryData = new CreateProjectParameters(); |
|
259 queryData.traceProjectPath = projectPath + File.separator |
|
260 + ProjectEngine.traceFolderName; |
|
261 queryData.traceProjectName = projectName; |
|
262 queryData.traceProjectID = DEFAULT_PROJECT_ID; |
|
263 return queryData; |
|
264 } |
|
265 |
|
266 /** |
|
267 * Creates the project file from query results |
|
268 * |
|
269 * @param queryData |
|
270 * the query result |
|
271 * @throws TraceCompilerException |
|
272 */ |
|
273 private void createEmptyProjectFile(CreateProjectParameters queryData) throws TraceCompilerException { |
|
274 model.setName(queryData.traceProjectName); |
|
275 model.setID(queryData.traceProjectID); |
|
276 String componentName = model.getName(); |
|
277 TraceCompilerProject file = new TraceCompilerProject( |
|
278 queryData.traceProjectPath, componentName); |
|
279 createAPI(file); |
|
280 } |
|
281 |
|
282 /** |
|
283 * Creates the project API |
|
284 * |
|
285 * @param file |
|
286 * the project file |
|
287 */ |
|
288 private void createAPI(TraceCompilerProject file) { |
|
289 model.addExtension(file); |
|
290 TraceAPIPluginManager plugin = model |
|
291 .getExtension(TraceAPIPluginManager.class); |
|
292 plugin.createDefaultAPI(); |
|
293 } |
|
294 } |