Created
February 10, 2012 11:52
-
-
Save db6edr/1789030 to your computer and use it in GitHub Desktop.
IzPack 4.3.x Windows RegistryWriterAction and RegistryReaderAction; provided with permission for reuse in IzPack installers - use at your own risk; we do not make any guarantees on the below code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.opentext.installer.actions; | |
public final class Constants { | |
public static final String REGISTRY_RESOURCE_NAME = "ConfigurationFile"; | |
public static final String REGISTRY_ACTION_CONFIGURATION_ELEMENT = "Action"; | |
public static final String REGISTRY_ROOT = "Root"; | |
public static final String REGISTRY_KEY = "Key"; | |
public static final String REGISTRY_NAME = "Name"; | |
public static final String READER_SET_VARIABLE = "SetVariable"; | |
public static final String READER_APPEND = "AppendToValue"; | |
public static final String READER_DEFAULT_VALUE = "DefaultValue"; | |
public static final String WRITER_VALUE = "Value"; | |
public static final String WRITER_MODE = "Mode"; | |
public static final String WRITER_CREATE_IF_NEW = "CreateIfNew"; | |
public static final String FAIL_IF_CONDITION_IS_FALSE_CONDITION_ID = "Condition"; | |
public static final String CLEAR_SELECTED_PACKS_IF_CONDITION_IS_FALSE_CONDITION_ID = "Condition"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<panel classname="TargetPanel" id="target.win"> | |
<os family="windows" /> | |
<actions> | |
<action stage="preactivate" classname="com.opentext.installer.actions.RegistryReaderAction"> | |
<param> | |
<key>ConfigurationFile</key> | |
<value>RegistryReaderConfiguration.xml</value> | |
</param> | |
</action> | |
</actions> | |
</panel> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.opentext.installer.actions; | |
import java.io.InputStream; | |
import java.util.Properties; | |
import com.izforge.izpack.adaptator.IXMLElement; | |
import com.izforge.izpack.adaptator.IXMLParser; | |
import com.izforge.izpack.adaptator.impl.XMLParser; | |
import com.izforge.izpack.installer.ResourceManager; | |
import com.izforge.izpack.util.Debug; | |
import com.izforge.izpack.util.OsVersion; | |
import com.izforge.izpack.util.VariableSubstitutor; | |
public final class RegistryActionsHelper { | |
/** | |
* Validates whether the operating system is MS Windows and all required | |
* properties have been set. | |
* | |
* @return true, if the action is able to execute. false else. | |
*/ | |
protected static boolean validate(String configurationFileName, String actionName) { | |
boolean result = true; | |
if (result && (!OsVersion.IS_WINDOWS)) { | |
Debug.trace(actionName + " can only be executed on MS Windows. Aborting."); | |
result = false; | |
} | |
if (result && (configurationFileName == null || "".equals(configurationFileName.trim()))) { | |
Debug.trace(actionName + ": Configuration file not defined. Aborting."); | |
result = false; | |
} | |
return result; | |
} | |
/** | |
* Loads the resource defined in this.configurationFileName and parses it into | |
* an IXMLElement. | |
* | |
* @return | |
* @throws Throwable | |
*/ | |
protected static IXMLElement loadConfiguration(String configurationFileName) throws Throwable { | |
InputStream inXML = ResourceManager.getInstance().getInputStream(configurationFileName); | |
IXMLParser parser = new XMLParser(); | |
IXMLElement result = parser.parse(inXML); | |
return result; | |
} | |
/** | |
* @param configuration | |
* @param name | |
* @param properties | |
* @return | |
*/ | |
protected static String getConfigurationValue(IXMLElement configuration, String name, Properties properties) { | |
String result = configuration.getFirstChildNamed(name).getAttribute("value"); | |
result = doVariableSubstitution(properties, result); | |
return result; | |
} | |
/** | |
* Calls IzPack's variable substitutor to process any variable in input. | |
* | |
* @param properties | |
* @param input | |
* @return | |
*/ | |
private static String doVariableSubstitution(Properties properties, String input) { | |
VariableSubstitutor vs = new VariableSubstitutor(properties); | |
String output = vs.substitute(input, null); | |
return output; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.opentext.installer.actions; | |
import java.util.Properties; | |
import com.coi.tools.os.win.NativeLibException; | |
import com.coi.tools.os.win.RegDataContainer; | |
import com.izforge.izpack.adaptator.IXMLElement; | |
import com.izforge.izpack.installer.AutomatedInstallData; | |
import com.izforge.izpack.installer.PanelAction; | |
import com.izforge.izpack.installer.PanelActionConfiguration; | |
import com.izforge.izpack.util.AbstractUIHandler; | |
import com.izforge.izpack.util.Debug; | |
import com.izforge.izpack.util.os.RegistryHandler; | |
import com.izforge.izpack.util.os.Win_RegistryHandler; | |
/** | |
* @author Dirk Raeder <[email protected]> | |
*/ | |
public class RegistryReaderAction implements PanelAction { | |
private final String empty = ""; | |
private String configurationFileName = null; | |
private AutomatedInstallData adata = null; | |
private String root = null; | |
private String keypath = null; | |
private String name = null; | |
private String target = null; | |
private String append = null; | |
private String defaultValue = null; | |
private Properties properties = null; | |
private int registryHandlerOldRoot; | |
private RegistryHandler registryHandler; | |
public RegistryReaderAction() { | |
super(); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* com.izforge.izpack.installer.PanelAction#executeAction(com.izforge.izpack | |
* .installer.AutomatedInstallData, com.izforge.izpack.util.AbstractUIHandler) | |
*/ | |
@Override | |
public void executeAction(AutomatedInstallData adata, AbstractUIHandler handler) { | |
Debug.trace("RegistryReaderAction: Starting."); | |
if (!RegistryActionsHelper.validate(this.configurationFileName, "RegistryReaderAction")) { | |
// log information already written in validate(); | |
return; | |
} | |
this.properties = adata.getVariables(); | |
this.adata = adata; | |
try { | |
loadAndExecuteActions(); | |
} catch (Exception e) { | |
Debug.trace(e); | |
} | |
} | |
private void loadAndExecuteActions() throws Exception { | |
Debug.trace("RegistryReaderAction: Preparing registry handler."); | |
prepareRegistryHandler(); | |
Debug.trace("RegistryReaderAction: Loading configuration."); | |
IXMLElement configuration = loadConfiguration(); | |
for (IXMLElement action : configuration.getChildrenNamed(Constants.REGISTRY_ACTION_CONFIGURATION_ELEMENT)) { | |
initializeAsCurrentAction(action); | |
executeCurrentAction(); | |
} | |
disposeOfRegistryHandler(); | |
Debug.trace("RegistryReaderAction: Finished."); | |
} | |
private IXMLElement loadConfiguration() throws Exception { | |
IXMLElement configuration = null; | |
try { | |
configuration = RegistryActionsHelper.loadConfiguration(this.configurationFileName); | |
} catch (Throwable exception) { | |
throw new Exception("RegistryReaderAction: Resource " + this.configurationFileName + " not defined. Aborting.", exception); | |
} | |
return configuration; | |
} | |
private void disposeOfRegistryHandler() throws NativeLibException { | |
this.registryHandler.setRoot(this.registryHandlerOldRoot); | |
this.registryHandler = null; | |
} | |
private void prepareRegistryHandler() throws Exception { | |
this.registryHandler = new Win_RegistryHandler(); | |
if (this.registryHandler == null) { | |
throw new Exception("RegistryReaderAction: Could not get RegistryHandler. Aborting."); | |
} | |
if (this.registryHandler.getDefaultHandler() != null) { | |
this.registryHandler = this.registryHandler.getDefaultHandler(); | |
} | |
this.registryHandler.verify(this.adata); | |
this.registryHandlerOldRoot = this.registryHandler.getRoot(); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* com.izforge.izpack.installer.PanelAction#initialize(com.izforge.izpack. | |
* installer.PanelActionConfiguration) | |
*/ | |
@Override | |
public void initialize(PanelActionConfiguration configuration) { | |
Debug.trace("RegistryReaderAction: Initializing."); | |
if (configuration == null) { | |
Debug.trace("RegistryReaderAction: Initialization failed."); | |
} else { | |
this.configurationFileName = configuration.getProperty(Constants.REGISTRY_RESOURCE_NAME, null).trim(); | |
Debug.trace(String.format("RegistryReaderAction: Initialization completed: Using resource %1$s", | |
this.configurationFileName)); | |
} | |
} | |
private void executeCurrentAction() { | |
Debug.trace(String.format("RegistryReaderAction: Executing. Reading %1$S\\%2$s\\%3$s", this.root, this.keypath, this.name)); | |
try { | |
this.registryHandler.setRoot(RegistryHandler.ROOT_KEY_MAP.get(this.root)); | |
RegDataContainer rdc = this.registryHandler.getValue(this.keypath, this.name, null); | |
if (rdc == null) { | |
setTargetToDefaultValue(); | |
} else { | |
setTargetToRegistryValue(rdc); | |
} | |
} catch (Exception e) { | |
Debug.trace(e); | |
setTargetToDefaultValue(); | |
} | |
} | |
private void initializeAsCurrentAction(IXMLElement action) { | |
this.root = RegistryActionsHelper.getConfigurationValue(action, Constants.REGISTRY_ROOT, this.properties); | |
this.keypath = RegistryActionsHelper.getConfigurationValue(action, Constants.REGISTRY_KEY, this.properties); | |
this.name = RegistryActionsHelper.getConfigurationValue(action, Constants.REGISTRY_NAME, this.properties); | |
this.target = RegistryActionsHelper.getConfigurationValue(action, Constants.READER_SET_VARIABLE, this.properties); | |
this.append = RegistryActionsHelper.getConfigurationValue(action, Constants.READER_APPEND, this.properties); | |
this.defaultValue = RegistryActionsHelper.getConfigurationValue(action, Constants.READER_DEFAULT_VALUE, this.properties); | |
} | |
private void setTargetToDefaultValue() { | |
if (this.defaultValue == null) { | |
Debug.trace("RegistryReaderAction: Registry entry not found; no default set. Skipping."); | |
} else { | |
Debug.trace("RegistryReaderAction: Registry entry not found. Using default value."); | |
this.adata.setVariable(this.target, this.defaultValue); | |
} | |
} | |
private void setTargetToRegistryValue(RegDataContainer rdc) { | |
String registryValue = rdc.getStringData(); | |
if (this.append != null && !this.empty.equals(this.append.trim())) { | |
registryValue += this.append; | |
} | |
Debug.trace(String.format("RegistryReaderAction: Setting variable %1$s to %2$s", this.target, registryValue)); | |
this.adata.setVariable(this.target, registryValue); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="ISO-8859-1" ?> | |
<Actions> | |
<Action> | |
<Root value="HKLM" /> | |
<Key value="SOFTWARE\Microsoft\Windows\CurrentVersion" /> | |
<Name value="InstallLocation" /> | |
<SetVariable value="INSTALL_PATH" /> | |
<AppendToValue value="\tomcat\webapps\cps" /> | |
<DefaultValue value="" /> | |
</Action> | |
</Actions> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.opentext.installer.actions; | |
import java.util.Properties; | |
import com.coi.tools.os.win.NativeLibException; | |
import com.coi.tools.os.win.RegDataContainer; | |
import com.izforge.izpack.adaptator.IXMLElement; | |
import com.izforge.izpack.installer.AutomatedInstallData; | |
import com.izforge.izpack.installer.PanelAction; | |
import com.izforge.izpack.installer.PanelActionConfiguration; | |
import com.izforge.izpack.util.AbstractUIHandler; | |
import com.izforge.izpack.util.Debug; | |
import com.izforge.izpack.util.os.RegistryHandler; | |
import com.izforge.izpack.util.os.Win_RegistryHandler; | |
/** | |
* @author Dirk Raeder <[email protected]> | |
* | |
*/ | |
public class RegistryWriterAction implements PanelAction { | |
private String configurationFileName = ""; | |
private enum ActionModeEnum { | |
append, prepend, overwrite, NOVALUE; | |
protected static ActionModeEnum parse(String value) { | |
try { | |
return ActionModeEnum.valueOf(value.toLowerCase()); | |
} catch (Exception e) { | |
return NOVALUE; | |
} | |
} | |
} | |
/** | |
* | |
*/ | |
public RegistryWriterAction() { | |
super(); | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* com.izforge.izpack.installer.PanelAction#executeAction(com.izforge.izpack | |
* .installer.AutomatedInstallData, com.izforge.izpack.util.AbstractUIHandler) | |
*/ | |
@Override | |
public void executeAction(AutomatedInstallData adata, AbstractUIHandler handler) { | |
Debug.trace("RegistryWriterAction: Executing."); | |
if (!RegistryActionsHelper.validate(this.configurationFileName, "RegistryWriterAction")) { | |
return; | |
} | |
try { | |
Properties properties = adata.getVariables(); | |
RegistryHandler rh = new Win_RegistryHandler(); | |
IXMLElement configuration = null; | |
if (rh == null) { | |
Debug.trace("RegistryWriterAction: Could not get RegistryHandler. Aborting."); | |
return; | |
} | |
try { | |
configuration = RegistryActionsHelper.loadConfiguration(this.configurationFileName); | |
} catch (Throwable exception) { | |
Debug.trace("RegistryWriterAction: Resource " + this.configurationFileName + " not defined. Aborting."); | |
return; | |
} | |
if (rh.getDefaultHandler() != null) { | |
rh = rh.getDefaultHandler(); | |
} | |
rh.verify(adata); | |
int oldRoot = rh.getRoot(); | |
for (IXMLElement action : configuration.getChildrenNamed(Constants.REGISTRY_ACTION_CONFIGURATION_ELEMENT)) { | |
execute(action, properties, rh); | |
} | |
rh.setRoot(oldRoot); | |
Debug.trace("RegistryWriterAction: Finished."); | |
} catch (Exception e) { | |
Debug.trace(e); | |
} | |
} | |
/* | |
* (non-Javadoc) | |
* | |
* @see | |
* com.izforge.izpack.installer.PanelAction#initialize(com.izforge.izpack. | |
* installer.PanelActionConfiguration) | |
*/ | |
@Override | |
public void initialize(PanelActionConfiguration configuration) { | |
Debug.trace("RegistryWriterAction: Initializing."); | |
if (configuration == null) { | |
Debug.trace("RegistryWriterAction: Initialization failed."); | |
} else { | |
this.configurationFileName = configuration.getProperty(Constants.REGISTRY_RESOURCE_NAME, null).trim(); | |
Debug.trace(String.format("RegistryWriterAction: Initialization completed: Using resource %1$s", | |
this.configurationFileName)); | |
} | |
} | |
private void execute(IXMLElement action, Properties properties, RegistryHandler rh) { | |
String root = RegistryActionsHelper.getConfigurationValue(action, Constants.REGISTRY_ROOT, properties); | |
String key = RegistryActionsHelper.getConfigurationValue(action, Constants.REGISTRY_KEY, properties); | |
String name = RegistryActionsHelper.getConfigurationValue(action, Constants.REGISTRY_NAME, properties); | |
String value = RegistryActionsHelper.getConfigurationValue(action, Constants.WRITER_VALUE, properties); | |
ActionModeEnum mode = ActionModeEnum.parse(RegistryActionsHelper.getConfigurationValue(action, | |
Constants.WRITER_MODE, properties)); | |
boolean createIfNew = Boolean.parseBoolean(RegistryActionsHelper.getConfigurationValue(action, | |
Constants.WRITER_CREATE_IF_NEW, properties)); | |
Debug.trace(String.format("RegistryWriterAction: Accessing system registry. %1$s\\%2$s\\%3$s", root, key, name)); | |
try { | |
rh.setRoot(RegistryHandler.ROOT_KEY_MAP.get(root)); | |
String currentValue = null; | |
RegDataContainer rdc = rh.getValue(key, name, null); | |
if (rdc == null) { | |
if (createIfNew) { | |
Debug.trace("RegistryWriterAction: Registry entry not found. Creating it."); | |
currentValue = value; | |
} else { | |
Debug.trace("RegistryWriterAction: Registry entry not found. Aborting."); | |
return; | |
} | |
} else { | |
currentValue = rdc.getStringData(); | |
Debug.trace("RegistryWriterAction: Registry entry found. Current value: " + currentValue); | |
switch (mode) { | |
case append: | |
currentValue = currentValue + value; | |
break; | |
case prepend: | |
currentValue = value + currentValue; | |
break; | |
case overwrite: | |
currentValue = value; | |
break; | |
} | |
} | |
Debug.trace("RegistryWriterAction: Setting value to " + currentValue); | |
rh.setValue(key, name, currentValue); | |
} catch (NativeLibException nle) { | |
Debug.trace("RegistryWriterAction: Accessing system registry failed!"); | |
Debug.trace(nle); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment