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

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

debugging lines added

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