2 MemoryDatabaseManager class.
4 Database hold all PCD information comes from SPD, MSA, FPD file in memory.
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
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.
16 package org
.tianocore
.build
.pcd
.entity
;
18 import java
.io
.BufferedWriter
;
20 import java
.io
.FileWriter
;
21 import java
.io
.IOException
;
22 import java
.util
.ArrayList
;
23 import java
.util
.Collections
;
24 import java
.util
.Comparator
;
25 import java
.util
.HashMap
;
26 import java
.util
.Hashtable
;
27 import java
.util
.List
;
30 import org
.tianocore
.build
.autogen
.CommonDefinition
;
31 import org
.tianocore
.build
.pcd
.action
.ActionMessage
;
33 /** Database hold all PCD information comes from SPD, MSA, FPD file in memory.
35 public class MemoryDatabaseManager
{
37 /// Memory database. The string "cName + SpaceNameGuid" is primary key.
38 /// memory database is in global scope, and it will be used for others PCD tools.
40 private static Map
<String
, Token
> memoryDatabase
= null;
42 /// The log file name for dumping memory database.
44 private static String logFileName
= null;
46 public static String PcdPeimHString
= "";
47 public static String PcdPeimCString
= "";
48 public static String PcdDxeHString
= "";
49 public static String PcdDxeCString
= "";
54 public MemoryDatabaseManager() {
56 // Allocate memory for new database in global scope.
58 if (memoryDatabase
== null) {
59 memoryDatabase
= new HashMap
<String
, Token
>();
64 Get the log file name.
66 public String
getLogFileName() {
71 Set parameter log file name.
73 @param fileName log file name parameter.
75 public void setLogFileName(String fileName
) {
76 logFileName
= fileName
;
80 Judege whether token exists in memory database
82 @param primaryKey the primaryKey for searching token
84 @retval TRUE - token already exist in database.
85 @retval FALSE - token does not exist in database.
87 public boolean isTokenInDatabase(String primaryKey
) {
88 return (memoryDatabase
.get(primaryKey
) != null);
92 Add a pcd token into memory database.
94 @param primaryKey the primary key for searching token
95 @param token token instance
97 public void addTokenToDatabase(String primaryKey
, Token token
) {
98 memoryDatabase
.put(primaryKey
, token
);
102 Get a token instance from memory database with primary key.
104 @param primaryKey the primary key for searching token
106 @return token instance.
108 public Token
getTokenByKey(String primaryKey
) {
109 return memoryDatabase
.get(primaryKey
);
113 Get the number of PCD token record in memory database.
115 @return the number of PCD token record in memory database.
117 public int getDBSize() {
118 return memoryDatabase
.size();
122 Get the token record array contained all PCD token in memory database.
124 @return the token record array contained all PCD token in memory database.
126 public Token
[] getRecordArray() {
127 Token
[] tokenArray
= null;
128 Object
[] dataArray
= null;
129 Map
.Entry entry
= null;
132 if (memoryDatabase
== null) {
136 dataArray
= memoryDatabase
.entrySet().toArray();
137 tokenArray
= new Token
[memoryDatabase
.size()];
138 for (index
= 0; index
< memoryDatabase
.size(); index
++) {
139 entry
=(Map
.Entry
) dataArray
[index
];
140 tokenArray
[index
] =(Token
) entry
.getValue();
147 private ArrayList
<Token
> getDynamicRecordArray() {
148 Token
[] tokenArray
= getRecordArray();
151 ArrayList
<Token
> al
= new ArrayList
<Token
>();
153 for (index
= 0; index
< tokenArray
.length
; index
++) {
154 if (tokenArray
[index
].pcdType
== Token
.PCD_TYPE
.DYNAMIC
||
155 tokenArray
[index
].pcdType
== Token
.PCD_TYPE
.DYNAMIC_EX
) {
156 al
.add(tokenArray
[index
]);
165 Get the token record array contained all PCD token referenced by PEI phase.
166 The output array is sorted based on descending order of the size of alignment for each feilds.
168 @return the token record array contained all PCD token referenced in PEI phase.
170 public void getTwoPhaseDynamicRecordArray(ArrayList
<Token
> pei
, ArrayList
<Token
> dxe
) {
171 int usageInstanceIndex
= 0;
173 ArrayList
<Token
> tokenArrayList
= getDynamicRecordArray();
174 List
<UsageInstance
> usageInstanceArray
= null;
175 UsageInstance usageInstance
= null;
177 for (index
= 0; index
< tokenArrayList
.size(); index
++) {
178 boolean found
= false;
179 Token token
= (Token
) tokenArrayList
.get(index
);
180 if (token
.producers
!= null) {
181 usageInstanceArray
= token
.producers
;
182 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
183 usageInstance
= (UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
184 if (CommonDefinition
.isPeiPhaseComponent(usageInstance
.componentType
)) {
193 if (token
.consumers
!= null) {
194 usageInstanceArray
= token
.consumers
;
195 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
196 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
197 if (CommonDefinition
.isPeiPhaseComponent(usageInstance
.componentType
)) {
207 // If no PEI components reference the PCD entry, we insert it to DXE list
218 Get all PCD record for a module according to module's name.
220 @param moduleName the name of module.
222 @return all usage instance for this module in memory database.
224 public List
<UsageInstance
> getUsageInstanceArrayByModuleName(String moduleName
) {
225 Token
[] tokenArray
= null;
227 int usageInstanceIndex
= 0;
228 List
<UsageInstance
> usageInstanceArray
= null;
229 UsageInstance usageInstance
= null;
230 List
<UsageInstance
> returnArray
= new ArrayList
<UsageInstance
>();
232 tokenArray
= getRecordArray();
235 // Loop to find all PCD record related to current module
237 for (recordIndex
= 0; recordIndex
< getDBSize(); recordIndex
++) {
238 if (tokenArray
[recordIndex
].producers
!= null) {
239 usageInstanceArray
= tokenArray
[recordIndex
].producers
;
240 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
241 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
242 if (usageInstance
.moduleName
.equalsIgnoreCase(moduleName
)) {
243 returnArray
.add(usageInstance
);
248 if (tokenArray
[recordIndex
].consumers
!= null) {
249 usageInstanceArray
= tokenArray
[recordIndex
].consumers
;
250 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
251 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
252 if (usageInstance
.moduleName
.equalsIgnoreCase(moduleName
)) {
253 returnArray
.add(usageInstance
);
259 if (returnArray
.size() == 0) {
260 ActionMessage
.warning(this, "Can *not* find any usage instance for " + moduleName
+ " !");
267 Get all modules name who contains PCD information
269 @return Array for module name
271 public List
<String
> getAllModuleArray()
276 Token
[] tokenArray
= null;
277 List
<String
> moduleNames
= new ArrayList
<String
>();
278 UsageInstance usageInstance
= null;
279 boolean bFound
= false;
281 tokenArray
= this.getRecordArray();
283 // Find all producer usage instance for retrieving module's name
285 for (indexToken
= 0; indexToken
< getDBSize(); indexToken
++) {
286 for (usageIndex
= 0; usageIndex
< tokenArray
[indexToken
].producers
.size(); usageIndex
++) {
287 usageInstance
= tokenArray
[indexToken
].producers
.get(usageIndex
);
289 for (moduleIndex
= 0; moduleIndex
< moduleNames
.size(); moduleIndex
++) {
290 if (moduleNames
.get(moduleIndex
).equalsIgnoreCase(usageInstance
.moduleName
)) {
296 moduleNames
.add(usageInstance
.moduleName
);
302 // Find all consumer usage instance for retrieving module's name
304 for (indexToken
= 0; indexToken
< getDBSize(); indexToken
++) {
305 for (usageIndex
= 0; usageIndex
< tokenArray
[indexToken
].consumers
.size(); usageIndex
++) {
306 usageInstance
= tokenArray
[indexToken
].consumers
.get(usageIndex
);
308 for (moduleIndex
= 0; moduleIndex
< moduleNames
.size(); moduleIndex
++) {
309 if (moduleNames
.get(moduleIndex
).equalsIgnoreCase(usageInstance
.moduleName
)) {
315 moduleNames
.add(usageInstance
.moduleName
);
323 Dump all PCD record into file for reviewing.
325 public void DumpAllRecords() {
326 BufferedWriter bWriter
= null;
327 Object
[] tokenArray
= null;
328 Map
.Entry entry
= null;
332 UsageInstance usageInstance
= null;
333 String inheritString
= null;
334 String componentTypeName
= null;
337 bWriter
= new BufferedWriter(new FileWriter(new File(logFileName
)));
338 tokenArray
= memoryDatabase
.entrySet().toArray();
339 for (index
= 0; index
< memoryDatabase
.size(); index
++) {
340 entry
=(Map
.Entry
) tokenArray
[index
];
341 token
=(Token
) entry
.getValue();
342 bWriter
.write("****** token [" + Integer
.toString(index
) + "] ******\r\n");
343 bWriter
.write(" cName:" + token
.cName
+ "\r\n");
344 for (usageIndex
= 0; usageIndex
< token
.producers
.size(); usageIndex
++) {
345 usageInstance
=(UsageInstance
)token
.producers
.get(usageIndex
);
346 componentTypeName
= CommonDefinition
.getComponentTypeString(usageInstance
.componentType
);
348 if (usageInstance
.isInherit
) {
349 inheritString
= "Inherit";
353 bWriter
.write(String
.format(" (Producer)#%d: %s:%s Package:%s %s\r\n",
356 usageInstance
.moduleName
,
357 usageInstance
.packageName
,
362 for (usageIndex
= 0; usageIndex
< token
.consumers
.size(); usageIndex
++) {
363 usageInstance
=(UsageInstance
)token
.consumers
.get(usageIndex
);
364 componentTypeName
= CommonDefinition
.getComponentTypeString(usageInstance
.componentType
);
365 if (usageInstance
.isInherit
) {
366 inheritString
= "Inherit";
370 bWriter
.write(String
.format(" (Consumer)#%d: %s:%s Package:%s %s\r\n",
373 usageInstance
.moduleName
,
374 usageInstance
.packageName
,
381 } catch (IOException exp
) {
382 ActionMessage
.warning(this, "Failed to open database log file: " + logFileName
);