From e938f333a84dd5422da8e2d8b0ad8ef7476f3cf0 Mon Sep 17 00:00:00 2001 From: Kaloyan Enimanev Date: Thu, 8 Sep 2011 13:05:15 +0200 Subject: [PATCH 09/10] allow headless installers to run. Note that panels can only be specified with their full classnames in the Izpack XML. --- .../izpack/api/data/AutomatedInstallData.java | 19 +++- .../izpack/installer/console/ConsoleInstaller.java | 110 +++++++++++++++++-- .../container/impl/InstallerContainer.java | 39 ++++--- .../panels/install/InstallPanelConsoleHelper.java | 27 +++-- 4 files changed, 155 insertions(+), 40 deletions(-) diff --git a/izpack-api/src/main/java/com/izforge/izpack/api/data/AutomatedInstallData.java b/izpack-api/src/main/java/com/izforge/izpack/api/data/AutomatedInstallData.java index 658b42d..32d239b 100644 --- a/izpack-api/src/main/java/com/izforge/izpack/api/data/AutomatedInstallData.java +++ b/izpack-api/src/main/java/com/izforge/izpack/api/data/AutomatedInstallData.java @@ -19,15 +19,20 @@ package com.izforge.izpack.api.data; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Properties; + import com.izforge.izpack.api.adaptator.IXMLElement; import com.izforge.izpack.api.adaptator.impl.XMLElementImpl; import com.izforge.izpack.api.event.InstallerListener; import com.izforge.izpack.api.rules.RulesEngine; import com.izforge.izpack.api.substitutor.VariableSubstitutor; -import java.io.Serializable; -import java.util.*; - /** * Encloses information about the install process. This implementation is not thread safe. * @@ -145,6 +150,8 @@ public abstract class AutomatedInstallData implements Serializable */ private Map attributes; + private VariableSubstitutor variableSubstitutor; + /** * This class should be a singleton. Therefore * the one possible object will be stored in this @@ -203,6 +210,7 @@ public abstract class AutomatedInstallData implements Serializable setVariables(variables); setAttributes(new HashMap()); setCustomData(new HashMap()); + this.variableSubstitutor = variableSubstitutor; self = this; } @@ -540,4 +548,9 @@ public abstract class AutomatedInstallData implements Serializable { this.installerListener = installerListener; } + + public VariableSubstitutor getVariableSubstitutor() + { + return this.variableSubstitutor; + } } diff --git a/izpack-installer/src/main/java/com/izforge/izpack/installer/console/ConsoleInstaller.java b/izpack-installer/src/main/java/com/izforge/izpack/installer/console/ConsoleInstaller.java index 480ec1b..8f79cad 100644 --- a/izpack-installer/src/main/java/com/izforge/izpack/installer/console/ConsoleInstaller.java +++ b/izpack-installer/src/main/java/com/izforge/izpack/installer/console/ConsoleInstaller.java @@ -21,13 +21,27 @@ package com.izforge.izpack.installer.console; -import com.izforge.izpack.api.data.*; +import java.io.FileInputStream; +import java.io.InputStream; +import java.io.PrintWriter; +import java.util.Enumeration; +import java.util.List; +import java.util.Properties; + +import com.izforge.izpack.api.data.AutomatedInstallData; +import com.izforge.izpack.api.data.DynamicInstallerRequirementValidator; +import com.izforge.izpack.api.data.Info; +import com.izforge.izpack.api.data.LocaleDatabase; +import com.izforge.izpack.api.data.Panel; +import com.izforge.izpack.api.data.ResourceManager; +import com.izforge.izpack.api.data.ScriptParserConstant; import com.izforge.izpack.api.exception.InstallerException; import com.izforge.izpack.api.installer.DataValidator; import com.izforge.izpack.api.installer.DataValidator.Status; import com.izforge.izpack.api.rules.RulesEngine; import com.izforge.izpack.api.substitutor.VariableSubstitutor; import com.izforge.izpack.core.substitutor.VariableSubstitutorImpl; +import com.izforge.izpack.data.PanelAction; import com.izforge.izpack.installer.base.InstallerBase; import com.izforge.izpack.installer.bootstrap.Installer; import com.izforge.izpack.installer.language.ConditionCheck; @@ -36,13 +50,6 @@ import com.izforge.izpack.util.Debug; import com.izforge.izpack.util.Housekeeper; import com.izforge.izpack.util.OsConstraintHelper; -import java.io.FileInputStream; -import java.io.InputStream; -import java.io.PrintWriter; -import java.util.Enumeration; -import java.util.List; -import java.util.Properties; - /** * Runs the console installer * @@ -80,10 +87,7 @@ public class ConsoleInstaller extends InstallerBase this.installdata.setLangpack(new LocaleDatabase(in)); this.installdata.setVariable(ScriptParserConstant.ISO3_LANG, this.installdata.getLocaleISO3()); resourceManager.setLocale(this.installdata.getLocaleISO3()); - if (!checkCondition.checkInstallerRequirements(this)) - { - variableSubstitutor = new VariableSubstitutorImpl(this.installdata.getVariables()); - } + variableSubstitutor = new VariableSubstitutorImpl(this.installdata.getVariables()); this.rules = this.installdata.getRules(); } @@ -102,6 +106,10 @@ public class ConsoleInstaller extends InstallerBase System.out.println("[ Console installation FAILED! ]"); return; } + if (System.getProperty("merge.props.to.installation.vars") != null) + { + mergePropertiesToVariables(); + } Debug.log("[ Starting console installation ] " + strAction); try @@ -238,6 +246,28 @@ public class ConsoleInstaller extends InstallerBase } + private void mergePropertiesToVariables() + { + for (String propName : properties.stringPropertyNames()) + { + String propValue = properties.getProperty(propName); + try + { + propValue = variableSubstitutor.substitute(propValue); + } catch (Exception ex) + { + String error = + String.format("Failed to evaluate variable '%s' : %s", propName, + ex.getMessage()); + System.out.println(error); + } + + installdata.setVariable(propName, propValue); + variableSubstitutor = new VariableSubstitutorImpl(this.installdata.getVariables()); + } + + } + protected void doInstall() throws Exception { try @@ -396,6 +426,7 @@ public class ConsoleInstaller extends InstallerBase } } + executeActions(p.getPreValidationActions(), installdata); // Evaluate panel condition String dataValidator = p.getValidator(); if (dataValidator != null) @@ -418,9 +449,64 @@ public class ConsoleInstaller extends InstallerBase } } } + executeActions(p.getPostValidationActions(), installdata); return true; } + private void executeActions(List actions, AutomatedInstallData idata) + { + if (actions == null || actions.isEmpty()) + { + return; + } + + for (String actionClassName : actions) { + PanelAction action = createActionFromClassName(actionClassName); + if (action != null) + { + executeAction(idata, actionClassName, action); + } + } + + + } + + private PanelAction createActionFromClassName(String actionClassName) + { + try + { + Class clazz = getClass().getClassLoader().loadClass(actionClassName); + if (clazz != null) + { + return (PanelAction) clazz.newInstance(); + } + } catch (Exception ex) + { + String error = + String.format("Error instantiating action '%s' : %s", actionClassName, + ex.getMessage()); + System.out.println(error); + } + return null; + } + + private void executeAction(AutomatedInstallData idata, String actionClassName, + PanelAction action) + { + try + { + action.initialize(null); + action.executeAction(idata, null); + } catch (Exception ex) + { + String error = + String.format("Error executing action '%s' : %s", actionClassName, + ex.getMessage()); + System.out.println(error); + + } + } + private void mergeAndOverwriteFromSysProperties() { Properties systemProperties = System.getProperties(); diff --git a/izpack-installer/src/main/java/com/izforge/izpack/installer/container/impl/InstallerContainer.java b/izpack-installer/src/main/java/com/izforge/izpack/installer/container/impl/InstallerContainer.java index 569bf27..92dd3e6 100644 --- a/izpack-installer/src/main/java/com/izforge/izpack/installer/container/impl/InstallerContainer.java +++ b/izpack-installer/src/main/java/com/izforge/izpack/installer/container/impl/InstallerContainer.java @@ -1,5 +1,17 @@ package com.izforge.izpack.installer.container.impl; +import java.awt.Dimension; +import java.awt.GraphicsEnvironment; +import java.awt.Toolkit; +import java.util.Properties; + +import javax.swing.ImageIcon; +import javax.swing.JFrame; + +import org.picocontainer.Characteristics; +import org.picocontainer.MutablePicoContainer; +import org.picocontainer.injectors.ProviderAdapter; + import com.izforge.izpack.api.container.BindeableContainer; import com.izforge.izpack.api.data.AutomatedInstallData; import com.izforge.izpack.api.data.ResourceManager; @@ -25,13 +37,6 @@ import com.izforge.izpack.installer.language.LanguageDialog; import com.izforge.izpack.installer.manager.PanelManager; import com.izforge.izpack.installer.unpacker.IUnpacker; import com.izforge.izpack.merge.MergeManagerImpl; -import org.picocontainer.Characteristics; -import org.picocontainer.MutablePicoContainer; -import org.picocontainer.injectors.ProviderAdapter; - -import javax.swing.*; -import java.awt.*; -import java.util.Properties; /** * Application Component.
@@ -81,15 +86,17 @@ public class InstallerContainer extends AbstractContainer { throw new IzPackException(e); } - pico - // Configuration of title parameter in InstallerFrame - .addConfig("title", getTitle(installdata, substitutor)) - // Configuration of frame parameter in languageDialog - .addConfig("frame", initFrame()); - pico - .addComponent(IUnpacker.class, unpackerclass) - .addComponent(InstallerFrame.class) - .as(Characteristics.USE_NAMES).addComponent(LanguageDialog.class); + + if (!GraphicsEnvironment.getLocalGraphicsEnvironment().isHeadless()) + { + pico + // Configuration of title parameter in InstallerFrame + .addConfig("title", getTitle(installdata, substitutor)) + // Configuration of frame parameter in languageDialog + .addConfig("frame", initFrame()); + pico.addComponent(IUnpacker.class, unpackerclass).addComponent(InstallerFrame.class) + .as(Characteristics.USE_NAMES).addComponent(LanguageDialog.class); + } // Load custom data in last position pico.getComponent(EventFiller.class).loadCustomData(); diff --git a/izpack-panel/src/main/java/com/izforge/izpack/panels/install/InstallPanelConsoleHelper.java b/izpack-panel/src/main/java/com/izforge/izpack/panels/install/InstallPanelConsoleHelper.java index 71f406f..e0d4a3e 100644 --- a/izpack-panel/src/main/java/com/izforge/izpack/panels/install/InstallPanelConsoleHelper.java +++ b/izpack-panel/src/main/java/com/izforge/izpack/panels/install/InstallPanelConsoleHelper.java @@ -21,15 +21,19 @@ package com.izforge.izpack.panels.install; +import java.io.PrintWriter; +import java.util.Properties; + import com.izforge.izpack.api.data.AutomatedInstallData; +import com.izforge.izpack.api.data.ResourceManager; import com.izforge.izpack.api.handler.AbstractUIHandler; import com.izforge.izpack.api.handler.AbstractUIProgressHandler; +import com.izforge.izpack.api.substitutor.VariableSubstitutor; +import com.izforge.izpack.core.substitutor.VariableSubstitutorImpl; import com.izforge.izpack.installer.console.PanelConsole; import com.izforge.izpack.installer.console.PanelConsoleHelper; -import com.izforge.izpack.installer.unpacker.IUnpacker; - -import java.io.PrintWriter; -import java.util.Properties; +import com.izforge.izpack.installer.data.UninstallData; +import com.izforge.izpack.installer.unpacker.Unpacker; /** * Install Panel console helper @@ -55,13 +59,18 @@ public class InstallPanelConsoleHelper extends PanelConsoleHelper implements Pan public boolean runConsole(AutomatedInstallData idata) { + VariableSubstitutor variableSubstitutor = new VariableSubstitutorImpl(idata.getVariables()); + Unpacker unpacker = + new Unpacker(idata, ResourceManager.getInstance(), idata.getRules(), + variableSubstitutor, new UninstallData()); + unpacker.setHandler(this); + unpacker.setRules(idata.getRules()); - //REFACTOR : Use container to get unpacker -// IUnpacker unpacker = UnpackerFactory.getUnpacker(idata.getInfo().getUnpackerClassName(), idata, this); -// this); - IUnpacker unpacker = null; + // InstallerContainer container = new InstallerContainer(); + // container.initBindings(); + // IUnpacker unpacker = container.getComponent(IUnpacker.class); + Thread unpackerthread = new Thread(unpacker, "IzPack - Unpacker thread"); - unpacker.setRules(idata.getRules()); unpackerthread.start(); boolean done = false; while (!done && unpackerthread.isAlive()) -- 1.7.4.1