org.rhq.enterprise.agent
Class EnvironmentScriptFileUpdate

java.lang.Object
  extended by org.rhq.enterprise.agent.EnvironmentScriptFileUpdate
Direct Known Subclasses:
JavaServiceWrapperConfigurationFileUpdate, JavaServiceWrapperEnvironmentScriptFileUpdate, UnixEnvironmentScriptFileUpdate, WindowsEnvironmentScriptFileUpdate

public abstract class EnvironmentScriptFileUpdate
extends Object

This utility helps update one or more environment script files without losing the ordering of existing variables or comment lines. An "environment script" is a script file that simply contains lines that set environment variables (i.e. "RHQ_AGENT_MY_VAR=some.value" or "set NAME=VALUE" for Windows scripts).

You can update changes to existing environment variables or add new ones.

Note that this utility only works on simple environment script files where each name=value pair exists on single lines (i.e. they do not span multiple lines). But it can handle commented lines (i.e. comments are preserved).

Author:
John Mazzitelli

Nested Class Summary
static class EnvironmentScriptFileUpdate.NameValuePair
           
 
Constructor Summary
EnvironmentScriptFileUpdate(String location)
          Constructor given the full path to script file.
 
Method Summary
 Properties convertNameValuePairListToProperties(List<EnvironmentScriptFileUpdate.NameValuePair> list)
           
 List<EnvironmentScriptFileUpdate.NameValuePair> convertPropertiesToNameValuePairList(Properties props)
           
static EnvironmentScriptFileUpdate create(String location)
          Factory method that creates an update object that is appropriate to update the given file.
protected abstract  String createEnvironmentVariableLine(EnvironmentScriptFileUpdate.NameValuePair nvp)
          Creates a line that defines an environment variable name and its value.
 List<EnvironmentScriptFileUpdate.NameValuePair> loadExisting()
          Loads and returns the properties that exist currently in the properties file.
protected abstract  EnvironmentScriptFileUpdate.NameValuePair parseEnvironmentVariableLine(String line)
          Parses the given string that is a line from a environment script file.
 void update(EnvironmentScriptFileUpdate.NameValuePair nvp)
          Updates the script file so it will contain the key with the value (where the key is the name of the environment variable).
 void update(List<EnvironmentScriptFileUpdate.NameValuePair> newValuesList, boolean deleteMissing)
          Updates the existing script file with the new name/value settings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EnvironmentScriptFileUpdate

public EnvironmentScriptFileUpdate(String location)
Constructor given the full path to script file.

Parameters:
location - location of the file
Method Detail

create

public static EnvironmentScriptFileUpdate create(String location)
Factory method that creates an update object that is appropriate to update the given file. This creates update options for files that have these extensions:

Parameters:
location - location of the script file
Returns:
the update object that is appropriate to update the file

update

public void update(EnvironmentScriptFileUpdate.NameValuePair nvp)
            throws IOException
Updates the script file so it will contain the key with the value (where the key is the name of the environment variable). If value is null, an empty string will be used in file. If the variable does not yet exist in the properties file, it will be appended to the end of the file.

Parameters:
key - the env var name whose value is to be updated
value - the new env var value
Throws:
IOException

update

public void update(List<EnvironmentScriptFileUpdate.NameValuePair> newValuesList,
                   boolean deleteMissing)
            throws IOException
Updates the existing script file with the new name/value settings. If an env var is in newValues that already exists in the file, the existing setting is updated in place. Any new setting found in newValues that does not yet exist in the file will be added. Currently existing settings in the script file that are not found in newValues will remain as-is.

Parameters:
newValuesList - environment variable settings that are added or updated in the file
deleteMissing - if true, any settings found in the existing file that are missing from the given newValues will be removed from the existing file. if false, then newValues is assumed to be only a subset of the settings that can go in the file and thus any settings found in the existing file but are missing from the new values will not be deleted.
Throws:
IOException

loadExisting

public List<EnvironmentScriptFileUpdate.NameValuePair> loadExisting()
                                                             throws IOException
Loads and returns the properties that exist currently in the properties file. If the file does not exist, an empty set of properties is returned.

Returns:
properties that exist in the properties file
Throws:
IOException

convertNameValuePairListToProperties

public Properties convertNameValuePairListToProperties(List<EnvironmentScriptFileUpdate.NameValuePair> list)

convertPropertiesToNameValuePairList

public List<EnvironmentScriptFileUpdate.NameValuePair> convertPropertiesToNameValuePairList(Properties props)

createEnvironmentVariableLine

protected abstract String createEnvironmentVariableLine(EnvironmentScriptFileUpdate.NameValuePair nvp)
Creates a line that defines an environment variable name and its value.

Parameters:
nvp - the environment variable definition
Returns:
the line that is to be used in the script file to define the environment variable

parseEnvironmentVariableLine

protected abstract EnvironmentScriptFileUpdate.NameValuePair parseEnvironmentVariableLine(String line)
Parses the given string that is a line from a environment script file. If this is not a line that defines an environment variable, null is returned, otherwise, the environment variable name and value is returned.

Parameters:
line - the line from the environment script file
Returns:
the name, value of the environment that is defined in the line, or null if the line doesn't define an env var.


Copyright © 2008-2011 Red Hat, Inc.. All Rights Reserved.