KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > ASenvPropertyReader


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23 package com.sun.enterprise.util;
24
25 import java.util.HashMap JavaDoc;
26
27 import java.io.File JavaDoc;
28 import java.io.BufferedReader JavaDoc;
29 import java.io.FileReader JavaDoc;
30
31 import com.sun.logging.LogDomains;
32
33 import java.util.logging.Logger JavaDoc;
34 import java.util.logging.Level JavaDoc;
35
36 import com.sun.enterprise.util.net.NetUtils;
37 import java.net.UnknownHostException JavaDoc;
38
39 /**
40  * Class ASenvPropertyReader
41  *
42  * This class converts the envrionment variables stored in asenv.conf (UNIX)
43  * or asenv.bat (WINDOWS) into their equivalent system properties.
44  * This means that a number of system properties with fixed values do
45  * not have to be passed on the java command line using -D.
46  */

47 public class ASenvPropertyReader
48 {
49     private static Logger JavaDoc _logger = null;
50         
51     private HashMap JavaDoc _propertyMap = null;
52     private String JavaDoc _configDirectory = null;
53     private boolean useLogger = true;
54
55     /**
56      * Constructor ASenvPropertyReader
57      *
58      *
59      * @param configDirectory The configuration directory where asenv.conf
60      * or asenv.bat resides.
61      *
62      */

63     public ASenvPropertyReader(String JavaDoc configDirectory) {
64
65         _configDirectory = configDirectory;
66         _propertyMap = new HashMap JavaDoc();
67
68         //The _propertyMap keeps the mapping between environment variable
69
//name and system property name.
70
_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 JavaDoc configDirectory, boolean useLogger) {
122         this(configDirectory);
123         this.useLogger = useLogger;
124     }
125
126     /**
127      * Method setSystemProperty
128      * Parses a single line of asenv.conf or asenv.bat and attempt to
129      * set the corresponding system property. Note that if the system
130      * property is already set (e.g. via -D on the command line), then
131      * we will not clobber its existing value.
132      *
133      * @param line
134      *
135      */

136     private void setSystemProperty(String JavaDoc line) {
137
138         int pos = line.indexOf("=");
139
140         if (pos > 0) {
141             String JavaDoc lhs = (line.substring(0, pos)).trim();
142             String JavaDoc rhs = (line.substring(pos + 1)).trim();
143
144             if (OS.isWindows()) { //trim off the "set "
145
lhs = (lhs.substring(3)).trim();
146             }
147
148             if (OS.isUNIX()) { // take the quotes out
149
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 JavaDoc systemPropertyName = (String JavaDoc)_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     /**
172      * Method setSystemProperties
173      * Iterate through the lines of asenv.conf or asenv.bat and convert to
174      * system properties.
175      */

176     public void setSystemProperties() {
177         if(useLogger) {
178             _logger = LogDomains.getLogger(LogDomains.UTIL_LOGGER);
179         }
180         
181         //Set static properties. Currently this includes com.sun.aas.hostName. This
182
//property is used to avoid placing hardcoded host names into domain.xml
183
//making it non-relocatable.
184
if (System.getProperty(SystemPropertyConstants.HOST_NAME_PROPERTY) == null) {
185             String JavaDoc hostname = "localhost";
186             try {
187                 // canonical name checks to make sure host is proper
188
hostname = NetUtils.getCanonicalHostName();
189             } catch (Exception JavaDoc 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         //Read in asenv.conf/bat and set system properties accordingly
200
String JavaDoc 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 JavaDoc reader = null;
211
212         try {
213             reader = new BufferedReader JavaDoc(new FileReader JavaDoc(fileName));
214
215             String JavaDoc 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 JavaDoc 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 JavaDoc ex) {
235                 if(_logger!=null)
236                 _logger.log(Level.WARNING, "property_reader.asenvCloseError", ex);
237             }
238         }
239     }
240 }
241
Popular Tags