FileDocCategorySizeDatePackage
ASenvPropertyReader.javaAPI DocGlassfish v2 API9924Fri May 04 22:32:06 BST 2007com.sun.enterprise.util

ASenvPropertyReader

public class ASenvPropertyReader extends Object
Class ASenvPropertyReader This class converts the envrionment variables stored in asenv.conf (UNIX) or asenv.bat (WINDOWS) into their equivalent system properties. This means that a number of system properties with fixed values do not have to be passed on the java command line using -D.

Fields Summary
private static Logger
_logger
private HashMap
_propertyMap
private String
_configDirectory
private boolean
useLogger
Constructors Summary
public ASenvPropertyReader(String configDirectory)
Constructor ASenvPropertyReader

param
configDirectory The configuration directory where asenv.conf or asenv.bat resides.


                     
       

        _configDirectory = configDirectory;
        _propertyMap = new HashMap();

        //The _propertyMap keeps the mapping between environment variable
        //name and system property name.
        _propertyMap.put("AS_ANT", 
            SystemPropertyConstants.ANT_ROOT_PROPERTY);
        _propertyMap.put("AS_ANT_LIB", 
            SystemPropertyConstants.ANT_LIB_PROPERTY);
        _propertyMap.put("AS_DERBY_INSTALL", 
            SystemPropertyConstants.DERBY_ROOT_PROPERTY);
	_propertyMap.put("AS_WEBCONSOLE_LIB",
            SystemPropertyConstants.WEBCONSOLE_LIB_PROPERTY);
        _propertyMap.put("AS_WEBCONSOLE_APP",
            SystemPropertyConstants.WEBCONSOLE_APP_PROPERTY);
        _propertyMap.put("AS_JATO_LIB",
            SystemPropertyConstants.JATO_ROOT_PROPERTY);
        _propertyMap.put("AS_WEBSERVICES_LIB", 
            SystemPropertyConstants.WEB_SERVICES_LIB_PROPERTY);
        _propertyMap.put("AS_PERL", 
             SystemPropertyConstants.PERL_ROOT_PROPERTY);
        _propertyMap.put("AS_NSS", 
             SystemPropertyConstants.NSS_ROOT_PROPERTY);
        _propertyMap.put("AS_NSS_BIN", 
             SystemPropertyConstants.NSS_BIN_PROPERTY);
        _propertyMap.put("AS_IMQ_LIB", 
             SystemPropertyConstants.IMQ_LIB_PROPERTY);
        _propertyMap.put("AS_IMQ_BIN", 
             SystemPropertyConstants.IMQ_BIN_PROPERTY);
        _propertyMap.put("AS_CONFIG", 
             SystemPropertyConstants.CONFIG_ROOT_PROPERTY);
        _propertyMap.put("AS_INSTALL", 
             SystemPropertyConstants.INSTALL_ROOT_PROPERTY);
        _propertyMap.put("AS_JAVA", 
             SystemPropertyConstants.JAVA_ROOT_PROPERTY);
        _propertyMap.put("AS_ACC_CONFIG", null);
        _propertyMap.put("AS_JHELP", 
             SystemPropertyConstants.JHELP_ROOT_PROPERTY);
        _propertyMap.put("AS_ICU_LIB", 
             SystemPropertyConstants.ICU_LIB_PROPERTY);
        _propertyMap.put("AS_LOCALE", 
             SystemPropertyConstants.DEFAULT_LOCALE_PROPERTY);
        _propertyMap.put("AS_DEF_DOMAINS_PATH", 
             SystemPropertyConstants.DOMAINS_ROOT_PROPERTY);
        _propertyMap.put("AS_HADB",
            SystemPropertyConstants.HADB_ROOT_PROPERTY);
        _propertyMap.put("AS_NATIVE_LAUNCHER",
            SystemPropertyConstants.NATIVE_LAUNCHER);
        _propertyMap.put("AS_NATIVE_LAUNCHER_LIB_PREFIX",
            SystemPropertyConstants.NATIVE_LAUNCHER_LIB_PREFIX);        
        _propertyMap.put("AS_JDMK_HOME",
            SystemPropertyConstants.JDMK_HOME_PROPERTY);
        _propertyMap.put("AS_MFWK_HOME",
            SystemPropertyConstants.MFWK_HOME_PROPERTY);
    
public ASenvPropertyReader(String configDirectory, boolean useLogger)

        this(configDirectory);
        this.useLogger = useLogger;
    
Methods Summary
public voidsetSystemProperties()
Method setSystemProperties Iterate through the lines of asenv.conf or asenv.bat and convert to system properties.

        if(useLogger) {
            _logger = LogDomains.getLogger(LogDomains.UTIL_LOGGER);
        }
        
        //Set static properties. Currently this includes com.sun.aas.hostName. This
        //property is used to avoid placing hardcoded host names into domain.xml 
        //making it non-relocatable.
        if (System.getProperty(SystemPropertyConstants.HOST_NAME_PROPERTY) == null) {
            String hostname = "localhost";
            try {                
                // canonical name checks to make sure host is proper
                hostname = NetUtils.getCanonicalHostName();                       
            } catch (Exception ex) {
               if(_logger!=null)
                _logger.log(Level.SEVERE, "property_reader.unknownHost", ex);
            }           
            if(_logger!=null)
            _logger.log(Level.FINE, "System.setProperty " +
                SystemPropertyConstants.HOST_NAME_PROPERTY + "=" + hostname);    
            System.setProperty(SystemPropertyConstants.HOST_NAME_PROPERTY, hostname);
        }
        
        //Read in asenv.conf/bat and set system properties accordingly
        String fileName = _configDirectory + File.separatorChar;
        
        if (OS.isUNIX()) {
            fileName +=  SystemPropertyConstants.UNIX_ASENV_FILENAME;
        } else if (OS.isWindows()) {
            fileName +=  SystemPropertyConstants.WINDOWS_ASENV_FILENAME;
        } else {
            assert false;
        }

        BufferedReader reader = null;

        try {
            reader = new BufferedReader(new FileReader(fileName));

            String line = null;

            while (true) {
                line = reader.readLine();

                if (line == null) {
                    break;
                } else {
                    setSystemProperty(line);
                }
            }
        } catch (Exception ex) {
            if(_logger!=null)
            _logger.log(Level.SEVERE, "property_reader.asenvReadError", ex);
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception ex) {
                if(_logger!=null)
                _logger.log(Level.WARNING, "property_reader.asenvCloseError", ex);
            }
        }
    
private voidsetSystemProperty(java.lang.String line)
Method setSystemProperty Parses a single line of asenv.conf or asenv.bat and attempt to set the corresponding system property. Note that if the system property is already set (e.g. via -D on the command line), then we will not clobber its existing value.

param
line


        int pos = line.indexOf("=");

        if (pos > 0) {
            String lhs = (line.substring(0, pos)).trim();
            String rhs = (line.substring(pos + 1)).trim();

            if (OS.isWindows()) {    //trim off the "set "
                lhs = (lhs.substring(3)).trim();
            }

            if (OS.isUNIX()) {      // take the quotes out
               pos = rhs.indexOf("\"");
               if(pos != -1) {
                    rhs = (rhs.substring(pos+1)).trim();
                    pos = rhs.indexOf("\"");
                    if(pos != -1)
                        rhs = (rhs.substring(0, pos)).trim();
               }
            }

            String systemPropertyName = (String)_propertyMap.get(lhs);
            
            if (systemPropertyName != null) {
                if (System.getProperty(systemPropertyName) == null) {
                    if(_logger!=null)
                    _logger.log(Level.FINE, "System.setProperty " +
                            systemPropertyName + "=" + rhs);                    
                    System.setProperty(systemPropertyName, rhs);
                }
            }
        }