source: release-kits/shared/ant-installer/src/org/tp23/antinstaller/runtime/ConfigurationLoader.java@ 15330

Last change on this file since 15330 was 15330, checked in by oranfry, 16 years ago

more output so for error diagnosis

File size: 12.3 KB
Line 
1/*
2 * Copyright 2005 Paul Hinds
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.runtime;
17
18import java.io.File;
19import java.io.IOException;
20import java.util.ArrayList;
21import java.util.HashSet;
22import java.util.Iterator;
23import java.util.Set;
24
25import org.tp23.antinstaller.InstallException;
26import org.tp23.antinstaller.Installer;
27import org.tp23.antinstaller.PropertiesFileRenderer;
28import org.tp23.antinstaller.input.ConditionalField;
29import org.tp23.antinstaller.input.InputField;
30import org.tp23.antinstaller.input.OutputField;
31import org.tp23.antinstaller.input.ResultContainer;
32import org.tp23.antinstaller.input.SelectInput;
33import org.tp23.antinstaller.input.TargetInput;
34import org.tp23.antinstaller.input.TargetSelectInput;
35import org.tp23.antinstaller.page.Page;
36import org.tp23.antinstaller.page.ProgressPage;
37import org.tp23.antinstaller.page.SimpleInputPage;
38import org.tp23.antinstaller.renderer.swing.plaf.LookAndFeelFactory;
39import org.tp23.antinstaller.runtime.exe.LoadConfigFilter;
40import org.tp23.antinstaller.runtime.exe.PropertyLoaderFilter;
41import org.tp23.antinstaller.runtime.logic.ExpressionBuilder;
42/**
43 *
44 * <p>Loads the configuration file into memory as an Installer object. </p>
45 * <p>This class also contains the main() method to check the config files for common errors </p>
46 * <p>Copyright: Copyright (c) 2004</p>
47 * <p>Company: tp23</p>
48 * @todo this should be an interface not a class
49 * @author Paul Hinds
50 * @version $Id: ConfigurationLoader.java,v 1.15 2007/01/28 08:44:43 teknopaul Exp $
51 */
52public class ConfigurationLoader extends LoadConfigFilter{
53
54 /**
55 * Command line config checker
56 * @param args String[]
57 * @throws InstallException
58 */
59 public static void main(String[] args) {
60 ConfigurationLoader configurationLoader = new ConfigurationLoader();
61 String configFile = INSTALLER_CONFIG_FILE;
62 if(args.length > 1 && args[1].endsWith(".xml")){
63 configFile = args[1];
64 }
65 int ret = 1;
66 try {
67 configurationLoader.readConfig(new File(args[0]), configFile);
68 ret = configurationLoader.validate();
69
70 if(ret > 0){
71 System.out.println("VALIDATION FAILED");
72 }
73 }
74 catch (ConfigurationException ex) {
75 ex.printStackTrace();
76 System.exit(ret);
77 }
78 catch (IOException ex) {
79 ex.printStackTrace();
80 System.exit(ret);
81 } catch (InstallException ex) {
82 // probably ifProperty syntax wrong
83 ex.printStackTrace();
84 System.exit(ret);
85 }
86 }
87
88 /**
89 * This method is not valid until super.readConfig() has been run
90 * @return
91 */
92 public Installer getInstaller(){
93 return installer;
94 }
95
96 public int validate() throws IOException, ConfigurationException, InstallException{
97 System.out.println( "get pages" );
98 Page[] pages = installer.getPages();
99 boolean foundErrors = false;
100 Set pageNames = new HashSet();
101 Set targets = new HashSet();
102 Set propertyNames = new HashSet();
103 Set pagePropertyNames = null;
104
105 System.out.println( "validate attributes" );
106 if( validateInstallerAttributes() ){
107 System.out.println( "(validate attributes returned true)" );
108 foundErrors = true;
109 }
110
111 for (int p = 0; p < pages.length; p++) {
112 System.out.println("Checking page: " + pages[p].getName() );
113 if(pageNames.contains(pages[p].getName())){
114 System.out.println("Error: page name '"
115 + pages[p].getName()
116 + "' repeated - auto loading of configuration will fail");
117 foundErrors = true;
118 }
119 pageNames.add(pages[p].getName());
120
121 //TODO check page requirements
122 //test ifProperty syntax
123 // TODO passes validation even if nothing will evaluate
124 if (pages[p] instanceof SimpleInputPage) {
125 SimpleInputPage sPage = (SimpleInputPage)pages[p];
126 if(sPage.getIfProperty() != null){
127 try {
128 ResultContainer mock = new ResultContainer();
129 ExpressionBuilder.parseLogicalExpressions( mock,
130 sPage.getIfProperty() );
131 }
132 catch( ConfigurationException configExc ){
133 System.out.println("Error: loading ifProperty," + sPage.getIfProperty() + " ,page: " + pages[p].getName() );
134 foundErrors = true;
135 }
136 }
137 }
138
139
140 pagePropertyNames = new HashSet();
141
142 OutputField[] fields = pages[p].getOutputField();
143 for (int f = 0; f < fields.length; f++) {
144 if(!fields[f].validateObject()){
145 foundErrors = true;
146 System.out.println("Error in page:" + pages[p].getName());
147 }
148 if(fields[f] instanceof TargetInput){
149 TargetInput tgtInput = (TargetInput)fields[f];
150 targets.add(tgtInput.getTarget());
151 }
152 if(fields[f] instanceof InputField && !(fields[f] instanceof ConditionalField) ){
153 InputField genericInput = (InputField)fields[f];
154 if(genericInput.getProperty().endsWith(PropertiesFileRenderer.TARGETS_SUFFIX)){
155 System.out.println("Error: invalid property name:" + genericInput.getProperty());
156 System.out.println("InputField names must not end with -targets");
157 }
158 String propertyName = genericInput.getProperty();
159 //System.out.println("Checking page.property: " + pages[p].getName() + "," + propertyName );
160 if(propertyNames.contains(propertyName)){
161 //foundErrors = true;
162 System.out.println("Repeated property name:" + propertyName);
163 System.out.println("Loading defaults from file will probably not work:" + propertyName);
164 }
165 else{
166 propertyNames.add(propertyName);
167 }
168 // repeated properties on the same page are an error always
169 if(pagePropertyNames.contains(propertyName)){
170 foundErrors = true;
171 System.out.println("Repeated property name: page=" +
172 pages[p].getName() + ", property=" + propertyName);
173 }
174 else{
175 pagePropertyNames.add(propertyName);
176 }
177
178 }
179 }
180
181 }
182 System.out.println("Finished checking config inputs");
183 // check page structure
184 if(!(pages[pages.length-1] instanceof ProgressPage)){
185 foundErrors = true;
186 System.out.println("Last Page should be a progress page");
187 }
188 else{
189 if (pages[pages.length-1].getPostDisplayTarget() != null){
190 foundErrors = true;
191 System.out.println("Progress pages do not support postDisplayTarget");
192 }
193 }
194 // check for targets
195 int numOfPageTargets = 0;
196 for (int p = 0; p < pages.length; p++) {
197 numOfPageTargets += pages[p].getAllTargets().size();
198 }
199 if(numOfPageTargets == 0){
200 System.out.println("Warning: No Page Targets (not a problem if there are target input types)");
201 }
202
203 Iterator iter = targets.iterator();
204 while (iter.hasNext()) {
205 String tgt = (String) iter.next();
206 if(tgt.endsWith(PropertiesFileRenderer.TARGETS_SUFFIX)){
207 System.out.println("Error: invalid target name:" + tgt);
208 System.out.println("Target names must not end with -targets");
209 foundErrors = true;
210 }
211 }
212
213 //@todo check targets exist in build.xml remember OSSpecific could be tricky to validate
214
215 int numOfTargetInputs = 0;
216 // check ifTargets
217 ArrayList targetsSoFar = new ArrayList();
218 for (int p = 0; p < pages.length; p++) {
219 if(pages[p] instanceof SimpleInputPage){
220 SimpleInputPage simple = (SimpleInputPage)pages[p];
221 String ifTarget = simple.getIfTarget();
222 if(ifTarget != null && !targetsSoFar.contains(ifTarget)){
223 System.out.println("ifTarget=" + ifTarget);
224 System.out.println("ifTarget will never test true, no prior target in page:"+pages[p].getName());
225 // disabled due to bug 1412658 could be reinstated with proper test and OSSpecific handling
226 //foundErrors = true;
227 }
228 }
229 // add after to ensure testing previous pages
230 targetsSoFar.addAll(pages[p].getAllTargets());
231 OutputField[] fields = pages[p].getOutputField();
232 for (int f = 0; f < fields.length; f++) {
233 if(fields[f] instanceof TargetInput){
234 if(numOfTargetInputs == 0){
235 System.out.println("Found target input type");
236 }
237 numOfTargetInputs++;
238 TargetInput ti = (TargetInput)fields[f];
239 targetsSoFar.add(ti.getTarget());
240 }
241 if(fields[f] instanceof TargetSelectInput){
242 if(numOfTargetInputs == 0){
243 System.out.println("Found target input type");
244 }
245 numOfTargetInputs++;
246 TargetSelectInput ti = (TargetSelectInput)fields[f];
247 SelectInput.Option[] options = ti.getOptions();
248 for (int i = 0; i < options.length; i++) {
249 SelectInput.Option option = options[i];
250 targetsSoFar.add(option.value);
251 }
252 }
253 }
254 }
255 if(numOfPageTargets == 0 && numOfTargetInputs == 0){
256 System.out.println("Warning: No targets found, installer may do nothing.");
257 }
258// if(targetsSoFar.contains("default")){
259// System.out.println("Target:target can not be \"default\"");
260// foundErrors = true;
261// }
262
263
264 System.out.println("Finished checking config");
265 if(!foundErrors){
266 return 0;
267 }
268 return 1;
269 }
270
271 private boolean validateInstallerAttributes(){
272
273 System.out.println("Checking installer: " + installer.getName() );
274 boolean foundErrors = false;
275
276 String[] validBooleanValues = {"true", "false"};
277 foundErrors |= validateValue("antialiased", installer.getAntialiased(), true, validBooleanValues);
278
279 // done in DTD
280 //foundErrors |= validateValue("verbose", installer.isVerbose(), true, validBooleanValues);
281 //foundErrors |= validateValue("debug", installer.isDebug(), true, validBooleanValues);
282
283 String[] validLAFValues = {LookAndFeelFactory.DEFAULT_LAF,
284 LookAndFeelFactory.GREYMETAL_LAF,
285 LookAndFeelFactory.NATIVE_LAF,
286 LookAndFeelFactory.JGOODIES_LAF,
287 LookAndFeelFactory.NULL_LAF };
288 if( validateValue("lookAndFeel", installer.getLookAndFeel(), true, validLAFValues) ){
289 System.out.println("Warning: non standard LookAndFeel ensure the correct classes are on the classpath at runtime:" + installer.getLookAndFeel());
290 }
291
292 if (installer.getName() == null){
293 System.out.println("Error: installer element attribute does not exist: name");
294 foundErrors = true;
295 }
296
297 try {
298 String wide = installer.getWide();
299 if(wide != null){
300 installer.parseWideValue(wide);
301 }
302 } catch (Exception e) {
303 System.out.println("Error: installer element attribute incorrect format (e.g. 600:275): wide");
304 foundErrors = true;
305 }
306
307 String[] validLoadDefaultValues = {PropertyLoaderFilter.FALSE,
308 PropertyLoaderFilter.LOAD,
309 PropertyLoaderFilter.PROMPT,
310 PropertyLoaderFilter.PROMPT_AUTO};
311
312 boolean loadDefaultsNull = true;
313 if( installer.supportsAutoBuild() ){
314 loadDefaultsNull = false;
315 }
316 foundErrors |= validateValue("loadDefaults", installer.getLoadDefaults(), loadDefaultsNull, validLoadDefaultValues);
317
318 VersionHelper vHelper = new VersionHelper();
319 if( installer.supportsAutoBuild() ){
320 if( ! vHelper.isValid(installer.getVersion()) ){
321 System.out.println("Error: invalid version attribute, required for -auto builds:" + installer.getVersion());
322 foundErrors = true;
323 }
324 }
325 if(installer.getVersion() != null){
326 if( ! vHelper.isValid(installer.getVersion()) ){
327 System.out.println("Error: invalid version attribute format examples 1.2.0 , 0.2beta:" + installer.getVersion());
328 foundErrors = true;
329 }
330 }
331
332 return foundErrors;
333 }
334 /**
335 * @return foundErrors (true if there was an error)
336 */
337 private boolean validateValue(String att, String value, boolean allowsNull, String[] validValues){
338 if(value == null){
339 if(!allowsNull){
340 System.out.println("Error: installer element attribute does not exist: " + att);
341 return true;
342 }
343 return false;
344 }
345 else {
346 for (int i = 0; i < validValues.length; i++) {
347 if(validValues[i].equals(value)){
348 return false;
349 }
350 }
351 System.out.println("Error: installer element attribute not valid value: " + att);
352 return true;
353 }
354 }
355
356}
Note: See TracBrowser for help on using the repository browser.