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

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

a bit more detail in the debug method

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