]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/GenBuild/org/tianocore/build/global/PropertyManager.java
1bd7cf97da47da283e3b367e69beed843460ca37
[mirror_edk2.git] / Tools / Java / Source / GenBuild / org / tianocore / build / global / PropertyManager.java
1 /** @file
2 PropertyManager class.
3
4 PropertyManager class wraps Project.setProperty and tracks overrided properties.
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 package org.tianocore.build.global;
16
17 import java.util.HashMap;
18 import java.util.Hashtable;
19 import java.util.Iterator;
20 import java.util.Set;
21 import java.util.Stack;
22
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.PropertyHelper;
25
26 /**
27 PropertyManager uses a incremental way to to track overrided properties when
28 setProperty. This is useful for property recovery in nestly calling build files.
29 Another functionality of this class is to prevent warning message printed when
30 building with "verbose" mode.
31 **/
32 public class PropertyManager {
33 //
34 // Property table stack, keeps track the history of properties changes
35 //
36 private static Stack<HashMap<String, String>> propertyTableStack = new Stack<HashMap<String, String>>();
37 //
38 // The very original properties
39 //
40 private static HashMap<String, String> orgPropertyTable = null;
41 //
42 // The last changes of properties
43 //
44 private static HashMap<String, String> oldPropertyTable = null;
45 //
46 // The current changes of properties
47 //
48 private static HashMap<String, String> bakPropertyTable = null;
49 //
50 // The Project of tracking properties
51 //
52 private static Project prj = null;
53 //
54 // PropertyHelper of this project for setting property quietly
55 //
56 private static PropertyHelper ph = null;
57
58 /**
59 Backup properties that have been overrided onto the stack for later recovery.
60 **/
61 public static void save() {
62 //
63 // If this is the first time to save properties changes, keep all properties
64 // of this project as the original property table.
65 //
66 if (orgPropertyTable == null) {
67 Hashtable prjProperties = prj.getProperties();
68 orgPropertyTable = new HashMap<String, String>();
69
70 Set keys = prjProperties.keySet();
71 Iterator iter = keys.iterator();
72 while (iter.hasNext()) {
73 String item = (String)iter.next();
74 orgPropertyTable.put(item, (String)prjProperties.get(item));
75 }
76 }
77
78 //
79 // If there're already overrided properties, push it onto stack; otherwise
80 // prepare taking new overrided property by allocating space for it.
81 //
82 if (bakPropertyTable != null) {
83 propertyTableStack.push(bakPropertyTable);
84 oldPropertyTable = bakPropertyTable;
85 } else {
86 oldPropertyTable = orgPropertyTable;
87 }
88 bakPropertyTable = new HashMap<String, String>();
89 }
90
91 /**
92 Restore the properties backup
93 **/
94 public static void restore() {
95 if (bakPropertyTable == null) {
96 //
97 // No properties backup, do nothing
98 //
99 return;
100 }
101 Set keys = bakPropertyTable.keySet();
102
103 //
104 // Re-set properties in backup
105 //
106 Iterator iter = keys.iterator();
107 while (iter.hasNext()) {
108 String name = (String)iter.next();
109 String value = (String)bakPropertyTable.get(name);
110 ph.setProperty(null, name, value, false);
111 }
112
113 //
114 // If there's backup history, get top one for next recovery
115 //
116 if (propertyTableStack.size() > 0) {
117 bakPropertyTable = (HashMap<String, String>)propertyTableStack.pop();
118 } else {
119 bakPropertyTable = null; // no recovery any more
120 }
121
122 //
123 // Determine last overrided properties for incremental judgement
124 //
125 if (propertyTableStack.size() == 0) {
126 oldPropertyTable = orgPropertyTable;
127 } else {
128 oldPropertyTable = (HashMap<String, String>)propertyTableStack.peek();
129 }
130 }
131
132 /**
133 Set current Project for save() and restore() use.
134
135 @param prj
136 **/
137 public static void setProject(Project prj) {
138 PropertyManager.prj = prj;
139 PropertyManager.ph = PropertyHelper.getPropertyHelper(prj);
140 }
141
142 /**
143 Set a property for current project. It will also be put into property
144 history record if the record table has been setup.
145
146 @param name Property name
147 @param value Property value
148 **/
149 public static void setProperty(String name, String value) {
150 if (prj == null) {
151 return;
152 }
153
154 setProperty(prj, name, value);
155 }
156
157 /**
158 Set a property for current project. It will also be put into property
159 history record if the record table has been setup.
160
161 @param project The Project for which the property will be set
162 @param name Property name
163 @param value Property value
164 **/
165 public static void setProperty(Project project, String name, String value) {
166 if (project == null) {
167 if (prj == null) {
168 return; // a Project must be given; otherwise nothing can be set
169 }
170 project = prj;
171 }
172
173 //
174 // Using PropertyHelper to set a property can be quiet (no override
175 // warning preset).
176 //
177 PropertyHelper.getPropertyHelper(project).setProperty(null, name, value, false);
178
179 //
180 // If no property override history record is found, do nothing further
181 //
182 if (oldPropertyTable == null || bakPropertyTable == null) {
183 return;
184 }
185
186 //
187 // Put a copy of given property in history record.
188 //
189 String oldValue = oldPropertyTable.get(name);
190 if (oldValue == null) {
191 oldValue = value;
192 }
193 bakPropertyTable.put(name, oldValue);
194 }
195 }
196