1
14
15package org.tp23.antinstaller.runtime.exe;
16
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.FileNotFoundException;
20import java.io.IOException;
21import java.util.ArrayList;
22import java.util.Collections;
23import java.util.Iterator;
24import java.util.List;
25import java.util.Properties;
26import java.util.ResourceBundle;
27import java.util.StringTokenizer;
28
29import org.tp23.antinstaller.InstallException;
30import org.tp23.antinstaller.Installer;
31import org.tp23.antinstaller.InstallerContext;
32import org.tp23.antinstaller.PropertiesFileRenderer;
33import org.tp23.antinstaller.input.ConditionalField;
34import org.tp23.antinstaller.input.InputField;
35import org.tp23.antinstaller.input.OutputField;
36import org.tp23.antinstaller.input.PasswordTextInput;
37import org.tp23.antinstaller.input.TargetInput;
38import org.tp23.antinstaller.input.TargetSelectInput;
39import org.tp23.antinstaller.page.Page;
40import org.tp23.antinstaller.runtime.VersionHelper;
41import org.tp23.antinstaller.runtime.exe.ExecuteRunnerFilter.AbortException;
42
43
44
60public class PropertyLoaderFilter implements ExecuteFilter {
61
62 private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
63
64 public static final String LOAD = "true";
65 public static final String PROMPT = "prompt";
66 public static final String PROMPT_AUTO = "prompt-auto";
67 public static final String FALSE = "false";
68 public static final String DEFAULT_PROPERTIES_FILE_PROPERTY = "antinstaller.properties";
69
70 private final String fileNameProperty;
71
72 private int definedPropertiesCount;
73
74
79 public PropertyLoaderFilter() {
80 this( DEFAULT_PROPERTIES_FILE_PROPERTY );
81 }
82
83
89 public PropertyLoaderFilter( final String fileNameProperty ) {
90 this.fileNameProperty = fileNameProperty;
91 }
92
93
01 public void exec(InstallerContext ctx) throws InstallException {
02
03 Installer installer = ctx.getInstaller();
04 String loadDefaults = installer.getLoadDefaults();
05 if(installer.isVerbose()) {
06 ctx.log("loadDefaults attribute:" + loadDefaults);
07 }
08 boolean load = false;
09 if(loadDefaults == null || FALSE.equals(loadDefaults)) {
10 if(installer.isVerbose()) {
11 ctx.log("Not loading defaults");
12 }
13 return;
14 }
15
16 ctx.log( "Checking for predefined properties");
17 Properties predefinedProps = loadPredefinedProperties( ctx, fileNameProperty );
18
19 definedPropertiesCount = predefinedProps.size();
20
21 boolean foundProps = false;
22 if( definedPropertiesCount == 0 ) {
23 ctx.log( "No predefined properties");
24 }
25 else{
26 foundProps = true;
27 }
28
29 if( foundProps && PROMPT.equals(loadDefaults) ) {
30 load = ctx.getMessageRenderer().prompt(res.getString("promptLoadDefaults"));
31 }
32 else if( foundProps && PROMPT_AUTO.equals(loadDefaults)) {
33 load = ctx.getMessageRenderer().prompt(res.getString("promptLoadDefaults"));
34 }
35 else if( foundProps && LOAD.equals(loadDefaults) ) {
36 load = true;
37 }
38
39 if( (!foundProps || !load) &&
40 ctx.isAutoBuild() &&
41 PROMPT.equals(loadDefaults) ) {
42 ctx.log( "Cant run -auto install without properties");
43 throw new AbortException("Install Aborted: cant load ant.install.properties");
44 }
45
46 if(load) {
47 if(installer.isVerbose()) {
48 ctx.log("Loading defaults");
49 }
50
51 String propertiesVersion = predefinedProps.getProperty(PropertiesFileRenderer.INSTALLER_VERSION_PROPERTY);
53 String configVersion = ctx.getInstaller().getVersion();
54 if(propertiesVersion != null) {
55 VersionHelper helper = new VersionHelper();
56 if( ( ! propertiesVersion.equals(configVersion)) &&
57 helper.equalOrHigher(configVersion , propertiesVersion) ) {
58
59 if( (! ctx.isAutoBuild()) && helper.majorVersionCompatible(configVersion , propertiesVersion) ){
61 if( ! ctx.getMessageRenderer().prompt(res.getString("propertiesVersionMismatch")) ){
62 throw new AbortException("Install Aborted: existing configuration is not compatible, config version: " + configVersion);
63 }
64 }
65 else {
66 throw new AbortException("Install Aborted: existing configuration is not compatible, config version: " + configVersion);
67 }
68 }
69
70 }
71 else {
72 throw new AbortException("Install Aborted: local ant.install.properties missing config version, must be equal or lower than: " + configVersion);
73 }
74
76 Page[] allPages = installer.getPages();
77 handleDefaults( ctx, allPages, predefinedProps );
78
79 }
80 }
81
82
85 private void handleDefaults( InstallerContext ctx, Page[] allPages, Properties props ) throws InstallException {
86 for( int i = 0; i < allPages.length; i++ ) {
87 OutputField[] fields = allPages[i].getOutputField();
88 setInputValues( ctx, allPages[i], fields, props );
89 }
90 }
91
92 private void setInputValues( InstallerContext ctx, Page page, OutputField[] outputFields, Properties props )
93 throws InstallException {
94 if( outputFields == null ) {
96 return;
97 }
98
99 String targets = props.getProperty(page.getName() + PropertiesFileRenderer.TARGETS_SUFFIX);
01 List targetsList = splitTargets(targets);
02
03 for (int j = 0; j < outputFields.length; j++) {
04 OutputField field = outputFields[j];
05
06 if( field instanceof ConditionalField ) {
07 ConditionalField condField = (ConditionalField) field;
08 setInputValues( ctx, page, condField.getFields(), props );
09 }
10 else if( field instanceof InputField ) {
11 InputField input = (InputField)field;
12 String propName = input.getProperty();
13 if( props.containsKey( propName ) ) {
14 String value = props.getProperty(propName);
15
16 if( ctx.getInstaller().isDebug() ) {
17 ctx.log( "Setting " + propName + "=" + value );
18 }
19
20 input.setDefaultValue(value); input.setInputResult(value);
22 input.setEditted( true );
23
24 if(field instanceof PasswordTextInput) {
25 if(value == null ){
26 ctx.getMessageRenderer().printMessage(res.getString("promptMissingDefaultPassword"));
27
28 }
29 }
30
31 if(field instanceof TargetInput) {
33 TargetInput tgtInput = (TargetInput)field;
35 page.removeTarget(tgtInput.getIdx());
36 if( ! InputField.isFalse(value)) {
38 page.addTarget(tgtInput.getIdx(), tgtInput.getTarget()); if( ! targetsList.contains(tgtInput.getTarget()) ){
41 ctx.log("Defaults error: targets list for page " + page.getName()
43 + " should contain a TargetInput that was true");
44 }
45 }
46 else {
47 if(InputField.isTrue( tgtInput.getForce()) ) {
48 String msg = "Defaults error: forced target for page " + page.getName()
49 + " has been removed";
50 ctx.log(msg);
51 throw new InstallException(msg);
52 }
53 }
54 }
55 if(field instanceof TargetSelectInput) {
56 TargetSelectInput tgtInput = (TargetSelectInput)field;
57 page.removeTarget(tgtInput.getIdx());
58 page.addTarget(tgtInput.getIdx(), value);
60 }
61 }
62 }
63 }
68
69 List pageTargets = page.getTargets(ctx);
71 Iterator iter = targetsList.iterator();
72 while (iter.hasNext()) {
73 String targetPerProps = (String) iter.next();
74 if( ! pageTargets.contains(targetPerProps)) {
75 ctx.log("Defaults warning: targets list for page " + page.getName()
76 + " should contain " + targetPerProps);
77 }
78 }
79
80 }
81
82
88 protected boolean isPropertiesLoaded() {
89 return (definedPropertiesCount > 0);
90 }
91
92
95 int getPropertiesFoundCount() {
96 return definedPropertiesCount;
97 }
98
99
17 private Properties loadPredefinedProperties( final InstallerContext context,
18 final String fileNamePropertyName )
19 throws InstallException {
20
21 Properties contextProps = InstallerContext.getEnvironment();
22 String propertiesFileName = null;
23 boolean failSilently = true;
24
25 if( fileNamePropertyName != null ) {
26 propertiesFileName = contextProps.getProperty( InstallerContext.ENV_PREFIX + fileNamePropertyName );
27
28 if( propertiesFileName == null ) {
29 propertiesFileName =
30 contextProps.getProperty( InstallerContext.JAVA_PREFIX + fileNamePropertyName );
31 }
32
33 if( propertiesFileName != null ) {
34 failSilently = false;
36 }
37 }
38
39 if( propertiesFileName == null ) {
40 propertiesFileName = PropertiesFileRenderer.PROPERTIES_FILE_NAME;
41 }
42
43 Properties definedProperties = new Properties( );
44
45 if( propertiesFileName != null ) {
46 File definedPropertiesFile = new File( propertiesFileName );
47 context.log( "Loading pre-defined properties from file "
48 + definedPropertiesFile.getAbsolutePath());
49
50 try {
52 FileInputStream istream = new FileInputStream( definedPropertiesFile );
53 definedProperties.load( istream );
54 istream.close();
55 }
56 catch( FileNotFoundException fnfExc ) {
57 if( !failSilently ) {
58 throw new InstallException( "Defined properties file "
59 + definedPropertiesFile.getAbsolutePath()
60 + " doesn't exist" );
61 }
62 }
63 catch( IOException ioExc ) {
64 if( !failSilently ) {
65 throw new InstallException( "Unable to read contents of defined properties file "
66 + definedPropertiesFile.getAbsolutePath(),
67 ioExc );
68 }
69 }
70
71 if( context.getInstaller().isDebug() ) {
72 logPropertiesLoaded( context, definedProperties, definedPropertiesFile );
73 }
74
75 }
76
77 return definedProperties;
78 }
79
80
81 private void logPropertiesLoaded( final InstallerContext context,
83 final Properties properties,
84 final File propertiesFile ) {
85 Iterator iterator = properties.keySet().iterator();
86 context.log( "Predefined properties ("
87 + definedPropertiesCount
88 + ") loaded from "
89 + propertiesFile.getAbsolutePath()
90 + "..." );
91 while( iterator.hasNext() ) {
92 String key = (String) iterator.next();
93 context.log( key + "=" + properties.getProperty( key ) );
94 }
95 }
96
97
02 private List splitTargets(String commaSeparated) {
03 if(commaSeparated == null) {
04 return Collections.EMPTY_LIST;
05 }
06 StringTokenizer st = new StringTokenizer(commaSeparated, ",");
07 List targets = new ArrayList();
08 while (st.hasMoreElements()) {
09 String element = st.nextToken();
10 if(element != null){
11 element = element.trim();
12 if(element.length() > 0){
13 targets.add(element.trim());
14 }
15 }
16 }
17 return targets;
18 }
19}
20