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
getDynamicRecordArray() {
148 Token
[] tokenArray
= getRecordArray();
151 ArrayList al
= new ArrayList();
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 tokenArrayList
= getDynamicRecordArray();
174 List
<UsageInstance
> usageInstanceArray
= null;
175 UsageInstance usageInstance
= null;
177 //pei = new ArrayList<Token>();
178 //dxe = new ArrayList<Token>();
180 for (index
= 0; index
< tokenArrayList
.size(); index
++) {
181 boolean found
= false;
182 Token token
= (Token
) tokenArrayList
.get(index
);
183 if (token
.producers
!= null) {
184 usageInstanceArray
= token
.producers
;
185 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
186 usageInstance
= (UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
187 if (CommonDefinition
.isPeiPhaseComponent(usageInstance
.componentType
)) {
196 if (token
.consumers
!= null) {
197 usageInstanceArray
= token
.consumers
;
198 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
199 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
200 if (CommonDefinition
.isPeiPhaseComponent(usageInstance
.componentType
)) {
210 // If no PEI components reference the PCD entry, we insert it to DXE list
221 Get all PCD record for a module according to module's name.
223 @param moduleName the name of module.
225 @return all usage instance for this module in memory database.
227 public List
<UsageInstance
> getUsageInstanceArrayByModuleName(String moduleName
) {
228 Token
[] tokenArray
= null;
230 int usageInstanceIndex
= 0;
231 List
<UsageInstance
> usageInstanceArray
= null;
232 UsageInstance usageInstance
= null;
233 List
<UsageInstance
> returnArray
= new ArrayList
<UsageInstance
>();
235 tokenArray
= getRecordArray();
238 // Loop to find all PCD record related to current module
240 for (recordIndex
= 0; recordIndex
< getDBSize(); recordIndex
++) {
241 if (tokenArray
[recordIndex
].producers
!= null) {
242 usageInstanceArray
= tokenArray
[recordIndex
].producers
;
243 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
244 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
245 if (usageInstance
.moduleName
.equalsIgnoreCase(moduleName
)) {
246 returnArray
.add(usageInstance
);
251 if (tokenArray
[recordIndex
].consumers
!= null) {
252 usageInstanceArray
= tokenArray
[recordIndex
].consumers
;
253 for (usageInstanceIndex
= 0; usageInstanceIndex
< usageInstanceArray
.size(); usageInstanceIndex
++) {
254 usageInstance
=(UsageInstance
) usageInstanceArray
.get(usageInstanceIndex
);
255 if (usageInstance
.moduleName
.equalsIgnoreCase(moduleName
)) {
256 returnArray
.add(usageInstance
);
262 if (returnArray
.size() == 0) {
263 ActionMessage
.warning(this, "Can *not* find any usage instance for " + moduleName
+ " !");
270 Get all modules name who contains PCD information
272 @return Array for module name
274 public List
<String
> getAllModuleArray()
279 Token
[] tokenArray
= null;
280 List
<String
> moduleNames
= new ArrayList
<String
>();
281 UsageInstance usageInstance
= null;
282 boolean bFound
= false;
284 tokenArray
= this.getRecordArray();
286 // Find all producer usage instance for retrieving module's name
288 for (indexToken
= 0; indexToken
< getDBSize(); indexToken
++) {
289 for (usageIndex
= 0; usageIndex
< tokenArray
[indexToken
].producers
.size(); usageIndex
++) {
290 usageInstance
= tokenArray
[indexToken
].producers
.get(usageIndex
);
292 for (moduleIndex
= 0; moduleIndex
< moduleNames
.size(); moduleIndex
++) {
293 if (moduleNames
.get(moduleIndex
).equalsIgnoreCase(usageInstance
.moduleName
)) {
299 moduleNames
.add(usageInstance
.moduleName
);
305 // Find all consumer usage instance for retrieving module's name
307 for (indexToken
= 0; indexToken
< getDBSize(); indexToken
++) {
308 for (usageIndex
= 0; usageIndex
< tokenArray
[indexToken
].consumers
.size(); usageIndex
++) {
309 usageInstance
= tokenArray
[indexToken
].consumers
.get(usageIndex
);
311 for (moduleIndex
= 0; moduleIndex
< moduleNames
.size(); moduleIndex
++) {
312 if (moduleNames
.get(moduleIndex
).equalsIgnoreCase(usageInstance
.moduleName
)) {
318 moduleNames
.add(usageInstance
.moduleName
);
326 Dump all PCD record into file for reviewing.
328 public void DumpAllRecords() {
329 BufferedWriter bWriter
= null;
330 Object
[] tokenArray
= null;
331 Map
.Entry entry
= null;
335 UsageInstance usageInstance
= null;
336 String inheritString
= null;
337 String componentTypeName
= null;
340 bWriter
= new BufferedWriter(new FileWriter(new File(logFileName
)));
341 tokenArray
= memoryDatabase
.entrySet().toArray();
342 for (index
= 0; index
< memoryDatabase
.size(); index
++) {
343 entry
=(Map
.Entry
) tokenArray
[index
];
344 token
=(Token
) entry
.getValue();
345 bWriter
.write("****** token [" + Integer
.toString(index
) + "] ******\r\n");
346 bWriter
.write(" cName:" + token
.cName
+ "\r\n");
347 for (usageIndex
= 0; usageIndex
< token
.producers
.size(); usageIndex
++) {
348 usageInstance
=(UsageInstance
)token
.producers
.get(usageIndex
);
349 componentTypeName
= CommonDefinition
.getComponentTypeString(usageInstance
.componentType
);
351 if (usageInstance
.isInherit
) {
352 inheritString
= "Inherit";
356 bWriter
.write(String
.format(" (Producer)#%d: %s:%s Package:%s %s\r\n",
359 usageInstance
.moduleName
,
360 usageInstance
.packageName
,
365 for (usageIndex
= 0; usageIndex
< token
.consumers
.size(); usageIndex
++) {
366 usageInstance
=(UsageInstance
)token
.consumers
.get(usageIndex
);
367 componentTypeName
= CommonDefinition
.getComponentTypeString(usageInstance
.componentType
);
368 if (usageInstance
.isInherit
) {
369 inheritString
= "Inherit";
373 bWriter
.write(String
.format(" (Consumer)#%d: %s:%s Package:%s %s\r\n",
376 usageInstance
.moduleName
,
377 usageInstance
.packageName
,
384 } catch (IOException exp
) {
385 ActionMessage
.warning(this, "Failed to open database log file: " + logFileName
);