1 23 package com.sun.enterprise.util; 24 25 import java.util.HashMap ; 26 27 import java.io.File ; 28 import java.io.BufferedReader ; 29 import java.io.FileReader ; 30 31 import com.sun.logging.LogDomains; 32 33 import java.util.logging.Logger ; 34 import java.util.logging.Level ; 35 36 import com.sun.enterprise.util.net.NetUtils; 37 import java.net.UnknownHostException ; 38 39 47 public class ASenvPropertyReader 48 { 49 private static Logger _logger = null; 50 51 private HashMap _propertyMap = null; 52 private String _configDirectory = null; 53 private boolean useLogger = true; 54 55 63 public ASenvPropertyReader(String configDirectory) { 64 65 _configDirectory = configDirectory; 66 _propertyMap = new HashMap (); 67 68 _propertyMap.put("AS_ANT", 71 SystemPropertyConstants.ANT_ROOT_PROPERTY); 72 _propertyMap.put("AS_ANT_LIB", 73 SystemPropertyConstants.ANT_LIB_PROPERTY); 74 _propertyMap.put("AS_DERBY_INSTALL", 75 SystemPropertyConstants.DERBY_ROOT_PROPERTY); 76 _propertyMap.put("AS_WEBCONSOLE_LIB", 77 SystemPropertyConstants.WEBCONSOLE_LIB_PROPERTY); 78 _propertyMap.put("AS_WEBCONSOLE_APP", 79 SystemPropertyConstants.WEBCONSOLE_APP_PROPERTY); 80 _propertyMap.put("AS_JATO_LIB", 81 SystemPropertyConstants.JATO_ROOT_PROPERTY); 82 _propertyMap.put("AS_WEBSERVICES_LIB", 83 SystemPropertyConstants.WEB_SERVICES_LIB_PROPERTY); 84 _propertyMap.put("AS_PERL", 85 SystemPropertyConstants.PERL_ROOT_PROPERTY); 86 _propertyMap.put("AS_NSS", 87 SystemPropertyConstants.NSS_ROOT_PROPERTY); 88 _propertyMap.put("AS_NSS_BIN", 89 SystemPropertyConstants.NSS_BIN_PROPERTY); 90 _propertyMap.put("AS_IMQ_LIB", 91 SystemPropertyConstants.IMQ_LIB_PROPERTY); 92 _propertyMap.put("AS_IMQ_BIN", 93 SystemPropertyConstants.IMQ_BIN_PROPERTY); 94 _propertyMap.put("AS_CONFIG", 95 SystemPropertyConstants.CONFIG_ROOT_PROPERTY); 96 _propertyMap.put("AS_INSTALL", 97 SystemPropertyConstants.INSTALL_ROOT_PROPERTY); 98 _propertyMap.put("AS_JAVA", 99 SystemPropertyConstants.JAVA_ROOT_PROPERTY); 100 _propertyMap.put("AS_ACC_CONFIG", null); 101 _propertyMap.put("AS_JHELP", 102 SystemPropertyConstants.JHELP_ROOT_PROPERTY); 103 _propertyMap.put("AS_ICU_LIB", 104 SystemPropertyConstants.ICU_LIB_PROPERTY); 105 _propertyMap.put("AS_LOCALE", 106 SystemPropertyConstants.DEFAULT_LOCALE_PROPERTY); 107 _propertyMap.put("AS_DEF_DOMAINS_PATH", 108 SystemPropertyConstants.DOMAINS_ROOT_PROPERTY); 109 _propertyMap.put("AS_HADB", 110 SystemPropertyConstants.HADB_ROOT_PROPERTY); 111 _propertyMap.put("AS_NATIVE_LAUNCHER", 112 SystemPropertyConstants.NATIVE_LAUNCHER); 113 _propertyMap.put("AS_NATIVE_LAUNCHER_LIB_PREFIX", 114 SystemPropertyConstants.NATIVE_LAUNCHER_LIB_PREFIX); 115 _propertyMap.put("AS_JDMK_HOME", 116 SystemPropertyConstants.JDMK_HOME_PROPERTY); 117 _propertyMap.put("AS_MFWK_HOME", 118 SystemPropertyConstants.MFWK_HOME_PROPERTY); 119 } 120 121 public ASenvPropertyReader(String configDirectory, boolean useLogger) { 122 this(configDirectory); 123 this.useLogger = useLogger; 124 } 125 126 136 private void setSystemProperty(String line) { 137 138 int pos = line.indexOf("="); 139 140 if (pos > 0) { 141 String lhs = (line.substring(0, pos)).trim(); 142 String rhs = (line.substring(pos + 1)).trim(); 143 144 if (OS.isWindows()) { lhs = (lhs.substring(3)).trim(); 146 } 147 148 if (OS.isUNIX()) { pos = rhs.indexOf("\""); 150 if(pos != -1) { 151 rhs = (rhs.substring(pos+1)).trim(); 152 pos = rhs.indexOf("\""); 153 if(pos != -1) 154 rhs = (rhs.substring(0, pos)).trim(); 155 } 156 } 157 158 String systemPropertyName = (String )_propertyMap.get(lhs); 159 160 if (systemPropertyName != null) { 161 if (System.getProperty(systemPropertyName) == null) { 162 if(_logger!=null) 163 _logger.log(Level.FINE, "System.setProperty " + 164 systemPropertyName + "=" + rhs); 165 System.setProperty(systemPropertyName, rhs); 166 } 167 } 168 } 169 } 170 171 176 public void setSystemProperties() { 177 if(useLogger) { 178 _logger = LogDomains.getLogger(LogDomains.UTIL_LOGGER); 179 } 180 181 if (System.getProperty(SystemPropertyConstants.HOST_NAME_PROPERTY) == null) { 185 String hostname = "localhost"; 186 try { 187 hostname = NetUtils.getCanonicalHostName(); 189 } catch (Exception ex) { 190 if(_logger!=null) 191 _logger.log(Level.SEVERE, "property_reader.unknownHost", ex); 192 } 193 if(_logger!=null) 194 _logger.log(Level.FINE, "System.setProperty " + 195 SystemPropertyConstants.HOST_NAME_PROPERTY + "=" + hostname); 196 System.setProperty(SystemPropertyConstants.HOST_NAME_PROPERTY, hostname); 197 } 198 199 String fileName = _configDirectory + File.separatorChar; 201 202 if (OS.isUNIX()) { 203 fileName += SystemPropertyConstants.UNIX_ASENV_FILENAME; 204 } else if (OS.isWindows()) { 205 fileName += SystemPropertyConstants.WINDOWS_ASENV_FILENAME; 206 } else { 207 assert false; 208 } 209 210 BufferedReader reader = null; 211 212 try { 213 reader = new BufferedReader (new FileReader (fileName)); 214 215 String line = null; 216 217 while (true) { 218 line = reader.readLine(); 219 220 if (line == null) { 221 break; 222 } else { 223 setSystemProperty(line); 224 } 225 } 226 } catch (Exception ex) { 227 if(_logger!=null) 228 _logger.log(Level.SEVERE, "property_reader.asenvReadError", ex); 229 } finally { 230 try { 231 if (reader != null) { 232 reader.close(); 233 } 234 } catch (Exception ex) { 235 if(_logger!=null) 236 _logger.log(Level.WARNING, "property_reader.asenvCloseError", ex); 237 } 238 } 239 } 240 } 241 | Popular Tags |