001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.monitoring.utilities;
006:
007: import com.sun.portal.log.common.PortalLogger;
008:
009: import java.io.FileInputStream;
010: import java.io.FileNotFoundException;
011: import java.io.IOException;
012: import java.io.InputStream;
013: import java.util.Properties;
014: import java.util.Set;
015: import java.util.TreeSet;
016: import java.util.logging.Level;
017: import java.util.logging.Logger;
018: import java.util.logging.LogRecord;
019:
020: public class PropertyHelper {
021: private static final Logger logger = PortalLogger
022: .getLogger(PropertyHelper.class);
023:
024: private static LogRecord getLogRecord(Level level, String message,
025: Object[] parameters, Throwable t) {
026: LogRecord result = new LogRecord(level, message);
027: result.setLoggerName(logger.getName());
028: result.setParameters(parameters);
029: result.setThrown(t);
030: return result;
031: }
032:
033: private void loadSystemProperties() {
034: this .properties = new Properties();
035: updateProperties(System.getProperties());
036: updatePropertyNames(properties);
037: }
038:
039: public PropertyHelper(Properties properties) {
040: this .properties = properties;
041: if (properties == null) {
042: loadSystemProperties();
043: }
044: }
045:
046: private Properties properties;
047: private Set propertyNames = new TreeSet();
048:
049: public Properties getProperties() {
050: return properties;
051: }
052:
053: private void updateProperties(Properties properties) {
054: this .properties.putAll(properties);
055: }
056:
057: private void updatePropertyNames(Properties properties) {
058: propertyNames.addAll(properties.keySet());
059: }
060:
061: private String getPropertyName(String propertyPrefix,
062: String propertySuffix) {
063: String result = propertyPrefix + "." + propertySuffix;
064:
065: if (!propertyNames.contains(result)) {
066: propertyNames.add(result);
067: }
068:
069: return result;
070: }
071:
072: public void printProperties() {
073: if (logger.isLoggable(Level.CONFIG)) {
074: String[] names = (String[]) propertyNames
075: .toArray(new String[propertyNames.size()]);
076:
077: StringBuffer sb = new StringBuffer();
078: String lineSeparator = System.getProperty("line.separator");
079:
080: for (int i = 0; i < names.length; i++) {
081: if (names[i].startsWith("com.sun.portal")) {
082: sb.append(lineSeparator);
083: sb.append("# <" + names[i] + "> = <"
084: + properties.getProperty(names[i]) + ">");
085: }
086: }
087:
088: logger.log(getLogRecord(Level.CONFIG, "PSMN_CSPM3001",
089: new Object[] { sb.toString() }, null));
090: }
091: }
092:
093: public String getProperty(String propertyPrefix,
094: String propertySuffix, String defaultValue) {
095: return properties.getProperty(getPropertyName(propertyPrefix,
096: propertySuffix), defaultValue);
097: }
098:
099: public String setProperty(String propertyPrefix,
100: String propertySuffix, String value) {
101: return (String) properties.setProperty(getPropertyName(
102: propertyPrefix, propertySuffix), value);
103: }
104:
105: public Boolean getBooleanProperty(String propertyPrefix,
106: String propertySuffix) {
107: return Boolean.valueOf(properties.getProperty(getPropertyName(
108: propertyPrefix, propertySuffix), Boolean.FALSE
109: .toString()));
110: }
111:
112: public Properties loadProperties(InputStream is)
113: throws UtilityException {
114: Properties result = new Properties();
115: if (is == null) {
116: final String message = "Cannot load <" + null + "> stream!";
117: throw new UtilityException(message);
118: } else {
119: try {
120: result.load(is);
121: } catch (IOException ioe) {
122: throw new UtilityException(ioe);
123: }
124: }
125:
126: updateProperties(result);
127: updatePropertyNames(result);
128:
129: return result;
130: }
131:
132: public Properties loadProperties(String propertiesFileName)
133: throws UtilityException {
134: InputStream is;
135: try {
136: is = new FileInputStream(propertiesFileName);
137: } catch (FileNotFoundException e) {
138: is = ClassLoader
139: .getSystemResourceAsStream(propertiesFileName);
140: }
141:
142: Properties result = null;
143: if (is == null) {
144: final String message = "Cannot load <" + propertiesFileName
145: + ">";
146: throw new UtilityException(message);
147: } else {
148: try {
149: result = loadProperties(is);
150: } finally {
151: try {
152: is.close();
153: } catch (IOException ioe) {
154: throw new UtilityException(ioe);
155: }
156: }
157: }
158:
159: return result;
160: }
161: }
|