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