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
.HashMap
;
24 import java
.util
.List
;
27 import org
.tianocore
.build
.autogen
.CommonDefinition
;
28 import org
.tianocore
.build
.pcd
.action
.ActionMessage
;
30 /** Database hold all PCD information comes from SPD, MSA, FPD file in memory.
32 public class MemoryDatabaseManager
{
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.
37 private static Map
<String
, Token
> memoryDatabase
= null;
39 /// The log file name for dumping memory database.
41 private static String logFileName
= null;
46 public MemoryDatabaseManager() {
48 // Allocate memory for new database in global scope.
50 if (memoryDatabase
== null) {
51 memoryDatabase
= new HashMap
<String
, Token
>();
56 Get the log file name.
58 public String
getLogFileName() {
63 Set parameter log file name.
65 @param fileName log file name parameter.
67 public void setLogFileName(String fileName
) {
68 logFileName
= fileName
;
72 Judege whether token exists in memory database
74 @param primaryKey the primaryKey for searching token
76 @retval TRUE - token already exist in database.
77 @retval FALSE - token does not exist in database.
79 public boolean isTokenInDatabase(String primaryKey
) {
80 return (memoryDatabase
.get(primaryKey
) != null);
84 Add a pcd token into memory database.
86 @param primaryKey the primary key for searching token
87 @param token token instance
89 public void addTokenToDatabase(String primaryKey
, Token token
) {
90 memoryDatabase
.put(primaryKey
, token
);
94 Get a token instance from memory database with primary key.
96 @param primaryKey the primary key for searching token
98 @return token instance.
100 public Token
getTokenByKey(String primaryKey
) {
101 return memoryDatabase
.get(primaryKey
);
105 Get the number of PCD token record in memory database.
107 @return the number of PCD token record in memory database.
109 public int getDBSize() {
110 return memoryDatabase
.size();
114 Get the token record array contained all PCD token in memory database.
116 @return the token record array contained all PCD token in memory database.
118 public Token
[] getRecordArray() {
119 Token
[] tokenArray
= null;
120 Object
[] dataArray
= null;
121 Map
.Entry entry
= null;
124 if (memoryDatabase
== null) {
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();
139 Get all PCD record for a module according to module's name.
141 @param moduleName the name of module.
143 @return all usage instance for this module in memory database.
145 public List
<UsageInstance
> getUsageInstanceArrayByModuleName(String moduleName
) {
146 Token
[] tokenArray
= null;
148 int usageInstanceIndex
= 0;
149 List
<UsageInstance
> usageInstanceArray
= null;
150 UsageInstance usageInstance
= null;
151 List
<UsageInstance
> returnArray
= new ArrayList
<UsageInstance
>();
153 tokenArray
= getRecordArray();
156 // Loop to find all PCD record related to current module
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
);
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
);
180 if (returnArray
.size() == 0) {
181 ActionMessage
.warning(this, "Can *not* find any usage instance for " + moduleName
+ " !");
188 Get all modules name who contains PCD information
190 @return Array for module name
192 public List
<String
> getAllModuleArray()
197 Token
[] tokenArray
= null;
198 List
<String
> moduleNames
= new ArrayList
<String
>();
199 UsageInstance usageInstance
= null;
200 boolean bFound
= false;
202 tokenArray
= this.getRecordArray();
204 // Find all producer usage instance for retrieving module's name
206 for (indexToken
= 0; indexToken
< getDBSize(); indexToken
++) {
207 for (usageIndex
= 0; usageIndex
< tokenArray
[indexToken
].producers
.size(); usageIndex
++) {
208 usageInstance
= tokenArray
[indexToken
].producers
.get(usageIndex
);
210 for (moduleIndex
= 0; moduleIndex
< moduleNames
.size(); moduleIndex
++) {
211 if (moduleNames
.get(moduleIndex
).equalsIgnoreCase(usageInstance
.moduleName
)) {
217 moduleNames
.add(usageInstance
.moduleName
);
223 // Find all consumer usage instance for retrieving module's name
225 for (indexToken
= 0; indexToken
< getDBSize(); indexToken
++) {
226 for (usageIndex
= 0; usageIndex
< tokenArray
[indexToken
].consumers
.size(); usageIndex
++) {
227 usageInstance
= tokenArray
[indexToken
].consumers
.get(usageIndex
);
229 for (moduleIndex
= 0; moduleIndex
< moduleNames
.size(); moduleIndex
++) {
230 if (moduleNames
.get(moduleIndex
).equalsIgnoreCase(usageInstance
.moduleName
)) {
236 moduleNames
.add(usageInstance
.moduleName
);
244 Dump all PCD record into file for reviewing.
246 public void DumpAllRecords() {
247 BufferedWriter bWriter
= null;
248 Object
[] tokenArray
= null;
249 Map
.Entry entry
= null;
253 UsageInstance usageInstance
= null;
254 String inheritString
= null;
255 String componentTypeName
= null;
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
);
269 if (usageInstance
.isInherit
) {
270 inheritString
= "Inherit";
274 bWriter
.write(String
.format(" (Producer)#%d: %s:%s Package:%s %s\r\n",
277 usageInstance
.moduleName
,
278 usageInstance
.packageName
,
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";
291 bWriter
.write(String
.format(" (Consumer)#%d: %s:%s Package:%s %s\r\n",
294 usageInstance
.moduleName
,
295 usageInstance
.packageName
,
302 } catch (IOException exp
) {
303 ActionMessage
.warning(this, "Failed to open database log file: " + logFileName
);