FileDocCategorySizeDatePackage
CommandEnvironment.javaAPI DocGlassfish v2 API8168Fri May 04 22:25:20 BST 2007com.sun.enterprise.cli.framework

CommandEnvironment

public class CommandEnvironment extends Object implements ICommandEnvironment
This class creates an environment for the options

Fields Summary
private static CommandEnvironment
commandEnvironment
private static final String
ENVIRONMENT_PREFIX
private HashMap
environments
Constructors Summary
protected CommandEnvironment()
Default constructor.


    	     
     
    
	    environments = (HashMap)getSystemEnvironment();
    
Methods Summary
public java.lang.ObjectgetEnvironmentValue(java.lang.String key)
returns the envrionment value by the given key

        return environments.get(key);
    
public java.util.HashMapgetEnvironments()
Returns an iterator over collection of Option objects.

        return environments;
    
public static com.sun.enterprise.cli.framework.CommandEnvironmentgetInstance()
returns the instance of the CommandEnvironment

        if (commandEnvironment == null)
        {
            commandEnvironment = new CommandEnvironment();
        }
        return commandEnvironment;
    
private java.lang.StringgetLocalizedString(java.lang.String key)
returns the localized string from framework's LocalStrings.properties. Calls the LocalStringsManagerFactory.getFrameworkLocalStringsManager() method, returns "Key not found" if it cannot find the key

param
key, the string to be localized

        try
        {
            final LocalStringsManager lsm = 
            LocalStringsManagerFactory.getFrameworkLocalStringsManager();
            return lsm.getString(key);
        }
        catch (CommandValidationException cve)
        {
            return LocalStringsManager.DEFAULT_STRING_VALUE;
        }
    
public intgetNumEnvironments()
Returns the number of environments

        return environments.size();
    
private java.util.MapgetSystemEnvironment()
This method uses System.getenv() (resurrected in jdk 1.5) to gets os environments.

return
Map of the system environment.

        //get environment prefix from CLI Descriptor file.
        final CLIDescriptorsReader cdr = CLIDescriptorsReader.getInstance();
        String sPrefix = cdr.getEnvironmentPrefix();
        //if not specified in descriptor file, then use the default value
        sPrefix = sPrefix==null?ENVIRONMENT_PREFIX:sPrefix;

        final Map<String, String> mEnv = System.getenv();
        HashMap<String, String> hmEnv = new HashMap<String, String>();
        final Iterator iterEnv = mEnv.keySet().iterator();

        while( iterEnv.hasNext() )
        {
            final String sEnvKey = ( String )iterEnv.next();
            if (sEnvKey.startsWith(sPrefix)) {
                    //extract the option name and convert it to lower case.
                final String sOptionName = sEnvKey.substring(sPrefix.length()).toLowerCase();
                    //replace occurrences of '_' to '-' since option name can only contain '-'
                    //this is a CLIP requirement.
                hmEnv.put(sOptionName.replace('_",'-"), (String)mEnv.get(sEnvKey));
            }
        }
        return hmEnv;
    
private java.util.MapgetSystemEnvironmentNative()
This method gets environment from os system environment

param
strArray - the string array to convert
return

        HashMap hashMap = new HashMap();
        try 
        {
            CLIDescriptorsReader cdr = CLIDescriptorsReader.getInstance();
            String envPrefix = cdr.getEnvironmentPrefix();
            //if not specified on descriptor file, then use the default value
            envPrefix = envPrefix==null?ENVIRONMENT_PREFIX:envPrefix;

            //get Global Env
            final String[] strArray = new CliUtil().getEnv(envPrefix);

            for (int ii=0; ii<strArray.length; ii++)
            {
                final int index = strArray[ii].indexOf("=");
                // need to break up the string in strArray
                // since the format is in name=value
                final String sOptionName = strArray[ii].substring(envPrefix.length(),
                                                                  index).toLowerCase();
                hashMap.put( sOptionName.replace('_", '-"),
                             strArray[ii].substring(index+1));
            }
        }
        catch (java.lang.UnsatisfiedLinkError e)
        {
            CLILogger.getInstance().printDebugMessage(e.getLocalizedMessage());
            // Emit the warning message
            CLILogger.getInstance().printWarning(getLocalizedString("UnableToReadEnv"));
        }
        return hashMap;
    
public java.lang.ObjectremoveEnvironment(java.lang.String name)
This method removes environment from the environment.

param
name is the name of the environment to be removed.

        return environments.remove(name);
    
public voidsetEnvironment(java.lang.String name, java.lang.String value)
This method adds a name and value to the environment.

param
name - option name
param
value - option value

        if ( environments.containsKey(name) )
        {
            environments.remove( name );
        }
        environments.put(name, value );
    
public java.lang.StringtoString()
This method returns a String format for list of environments in the environment with its argument values.

return
A String object.

        String description = "";
        final Iterator environIter = environments.keySet().iterator();
        while( environIter.hasNext() )
        {
            String environKey = ( String )environIter.next();
            description   +=  " " + environKey + " = " + 
                                    (String)environments.get(environKey);
        }
        return description;