]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/GenBuild/org/tianocore/build/pcd/entity/MemoryDatabaseManager.java
Initial import.
[mirror_edk2.git] / Tools / Source / GenBuild / org / tianocore / build / pcd / entity / MemoryDatabaseManager.java
1 /** @file
2 MemoryDatabaseManager class.
3
4 Database hold all PCD information comes from SPD, MSA, FPD file in memory.
5
6 Copyright (c) 2006, Intel Corporation
7 All rights reserved. This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16 package org.tianocore.build.pcd.entity;
17
18 import java.io.BufferedWriter;
19 import java.io.File;
20 import java.io.FileWriter;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.tianocore.build.autogen.CommonDefinition;
28 import org.tianocore.build.pcd.action.ActionMessage;
29
30 /** Database hold all PCD information comes from SPD, MSA, FPD file in memory.
31 **/
32 public class MemoryDatabaseManager {
33 ///
34 /// Memory database. The string "cName + SpaceNameGuid" is primary key.
35 /// memory database is in global scope, and it will be used for others PCD tools.
36 ///
37 private static Map<String, Token> memoryDatabase = null;
38 ///
39 /// The log file name for dumping memory database.
40 ///
41 private static String logFileName = null;
42
43 /**
44 Constructure function
45 **/
46 public MemoryDatabaseManager() {
47 //
48 // Allocate memory for new database in global scope.
49 //
50 if (memoryDatabase == null) {
51 memoryDatabase = new HashMap<String, Token>();
52 }
53 }
54
55 /**
56 Get the log file name.
57 **/
58 public String getLogFileName() {
59 return logFileName;
60 }
61
62 /**
63 Set parameter log file name.
64
65 @param fileName log file name parameter.
66 **/
67 public void setLogFileName(String fileName) {
68 logFileName = fileName;
69 }
70
71 /**
72 Judege whether token exists in memory database
73
74 @param primaryKey the primaryKey for searching token
75
76 @retval TRUE - token already exist in database.
77 @retval FALSE - token does not exist in database.
78 **/
79 public boolean isTokenInDatabase(String primaryKey) {
80 return (memoryDatabase.get(primaryKey) != null);
81 }
82
83 /**
84 Add a pcd token into memory database.
85
86 @param primaryKey the primary key for searching token
87 @param token token instance
88 **/
89 public void addTokenToDatabase(String primaryKey, Token token) {
90 memoryDatabase.put(primaryKey, token);
91 }
92
93 /**
94 Get a token instance from memory database with primary key.
95
96 @param primaryKey the primary key for searching token
97
98 @return token instance.
99 **/
100 public Token getTokenByKey(String primaryKey) {
101 return memoryDatabase.get(primaryKey);
102 }
103
104 /**
105 Get the number of PCD token record in memory database.
106
107 @return the number of PCD token record in memory database.
108 **/
109 public int getDBSize() {
110 return memoryDatabase.size();
111 }
112
113 /**
114 Get the token record array contained all PCD token in memory database.
115
116 @return the token record array contained all PCD token in memory database.
117 **/
118 public Token[] getRecordArray() {
119 Token[] tokenArray = null;
120 Object[] dataArray = null;
121 Map.Entry entry = null;
122 int index = 0;
123
124 if (memoryDatabase == null) {
125 return null;
126 }
127
128 dataArray = memoryDatabase.entrySet().toArray();
129 tokenArray = new Token[memoryDatabase.size()];
130 for (index = 0; index < memoryDatabase.size(); index ++) {
131 entry =(Map.Entry) dataArray [index];
132 tokenArray[index] =(Token) entry.getValue();
133 }
134
135 return tokenArray;
136 }
137
138 /**
139 Get all PCD record for a module according to module's name.
140
141 @param moduleName the name of module.
142
143 @return all usage instance for this module in memory database.
144 **/
145 public List<UsageInstance> getUsageInstanceArrayByModuleName(String moduleName) {
146 Token[] tokenArray = null;
147 int recordIndex = 0;
148 int usageInstanceIndex = 0;
149 List<UsageInstance> usageInstanceArray = null;
150 UsageInstance usageInstance = null;
151 List<UsageInstance> returnArray = new ArrayList<UsageInstance>();
152
153 tokenArray = getRecordArray();
154
155 //
156 // Loop to find all PCD record related to current module
157 //
158 for (recordIndex = 0; recordIndex < getDBSize(); recordIndex ++) {
159 if (tokenArray[recordIndex].producers != null) {
160 usageInstanceArray = tokenArray[recordIndex].producers;
161 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex ++) {
162 usageInstance =(UsageInstance) usageInstanceArray.get(usageInstanceIndex);
163 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
164 returnArray.add(usageInstance);
165 }
166 }
167 }
168
169 if (tokenArray[recordIndex].consumers != null) {
170 usageInstanceArray = tokenArray[recordIndex].consumers;
171 for (usageInstanceIndex = 0; usageInstanceIndex < usageInstanceArray.size(); usageInstanceIndex ++) {
172 usageInstance =(UsageInstance) usageInstanceArray.get(usageInstanceIndex);
173 if (usageInstance.moduleName.equalsIgnoreCase(moduleName)) {
174 returnArray.add(usageInstance);
175 }
176 }
177 }
178 }
179
180 if (returnArray.size() == 0) {
181 ActionMessage.warning(this, "Can *not* find any usage instance for " + moduleName + " !");
182 }
183
184 return returnArray;
185 }
186
187 /**
188 Get all modules name who contains PCD information
189
190 @return Array for module name
191 **/
192 public List<String> getAllModuleArray()
193 {
194 int indexToken = 0;
195 int usageIndex = 0;
196 int moduleIndex = 0;
197 Token[] tokenArray = null;
198 List<String> moduleNames = new ArrayList<String>();
199 UsageInstance usageInstance = null;
200 boolean bFound = false;
201
202 tokenArray = this.getRecordArray();
203 //
204 // Find all producer usage instance for retrieving module's name
205 //
206 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {
207 for (usageIndex = 0; usageIndex < tokenArray[indexToken].producers.size(); usageIndex ++) {
208 usageInstance = tokenArray[indexToken].producers.get(usageIndex);
209 bFound = false;
210 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {
211 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.moduleName)) {
212 bFound = true;
213 break;
214 }
215 }
216 if (!bFound) {
217 moduleNames.add(usageInstance.moduleName);
218 }
219 }
220 }
221
222 //
223 // Find all consumer usage instance for retrieving module's name
224 //
225 for (indexToken = 0; indexToken < getDBSize(); indexToken ++) {
226 for (usageIndex = 0; usageIndex < tokenArray[indexToken].consumers.size(); usageIndex ++) {
227 usageInstance = tokenArray[indexToken].consumers.get(usageIndex);
228 bFound = false;
229 for (moduleIndex = 0; moduleIndex < moduleNames.size(); moduleIndex ++) {
230 if (moduleNames.get(moduleIndex).equalsIgnoreCase(usageInstance.moduleName)) {
231 bFound = true;
232 break;
233 }
234 }
235 if (!bFound) {
236 moduleNames.add(usageInstance.moduleName);
237 }
238 }
239 }
240 return moduleNames;
241 }
242
243 /**
244 Dump all PCD record into file for reviewing.
245 **/
246 public void DumpAllRecords() {
247 BufferedWriter bWriter = null;
248 Object[] tokenArray = null;
249 Map.Entry entry = null;
250 Token token = null;
251 int index = 0;
252 int usageIndex = 0;
253 UsageInstance usageInstance = null;
254 String inheritString = null;
255 String componentTypeName = null;
256
257 try {
258 bWriter = new BufferedWriter(new FileWriter(new File(logFileName)));
259 tokenArray = memoryDatabase.entrySet().toArray();
260 for (index = 0; index < memoryDatabase.size(); index ++) {
261 entry =(Map.Entry) tokenArray [index];
262 token =(Token) entry.getValue();
263 bWriter.write("****** token [" + Integer.toString(index) + "] ******\r\n");
264 bWriter.write(" cName:" + token.cName + "\r\n");
265 for (usageIndex = 0; usageIndex < token.producers.size(); usageIndex ++) {
266 usageInstance =(UsageInstance)token.producers.get(usageIndex);
267 componentTypeName = CommonDefinition.getComponentTypeString(usageInstance.componentType);
268
269 if (usageInstance.isInherit) {
270 inheritString = "Inherit";
271 } else {
272 inheritString = "";
273 }
274 bWriter.write(String.format(" (Producer)#%d: %s:%s Package:%s %s\r\n",
275 usageIndex,
276 componentTypeName,
277 usageInstance.moduleName,
278 usageInstance.packageName,
279 inheritString
280 )
281 );
282 }
283 for (usageIndex = 0; usageIndex < token.consumers.size(); usageIndex ++) {
284 usageInstance =(UsageInstance)token.consumers.get(usageIndex);
285 componentTypeName = CommonDefinition.getComponentTypeString(usageInstance.componentType);
286 if (usageInstance.isInherit) {
287 inheritString = "Inherit";
288 } else {
289 inheritString = "";
290 }
291 bWriter.write(String.format(" (Consumer)#%d: %s:%s Package:%s %s\r\n",
292 usageIndex,
293 componentTypeName,
294 usageInstance.moduleName,
295 usageInstance.packageName,
296 inheritString
297 )
298 );
299 }
300 }
301 bWriter.close();
302 } catch (IOException exp) {
303 ActionMessage.warning(this, "Failed to open database log file: " + logFileName);
304 }
305 }
306 }