01: /*
02: *
03: *
04: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: */
26:
27: package com.sun.tck.wma;
28:
29: import java.util.Properties;
30: import java.io.FileInputStream;
31: import java.io.IOException;
32:
33: /**
34: * Tool for load properties from environment or property object.
35: */
36: public class PropLoader {
37:
38: /**
39: * Load string property value from environment or
40: * from connections.prop.
41: *
42: * When environment setting is defined, variable receives the value from it.
43: * Else it is initialized from connections.prop.
44: *
45: * @param valDefault the default value is assigned when neither
46: * environment variable nor property is defined
47: * @param envVar the name of environment variable
48: * @param propsName the properties file name
49: * @param propVar the name of property variable
50: *
51: * @return string property value
52: */
53: protected String getProp(String valDefault, String envVar,
54: String propsName, String propVar) {
55: String retValue = System.getProperty(envVar);
56: if (retValue == null) { // get from properties
57: try {
58: Properties props = new Properties();
59: props.load(new FileInputStream(propsName));
60: retValue = props.getProperty(propVar);
61: if (retValue == null) { // get default value
62: retValue = valDefault;
63: }
64: } catch (IOException ioe) { // no properties
65: retValue = valDefault;
66: }
67: }
68: return retValue;
69: }
70:
71: /**
72: * Load integer property value from environment or
73: * from connections.prop.
74: *
75: * When environment setting is defined, variable receives the value from it.
76: * Else it is initialized from connections.prop.
77: *
78: * @param valDefault the default value is assigned when neither
79: * environment variable nor property is defined
80: * @param envVar the name of environment variable
81: * @param propsName the properties file name
82: * @param propVar the name of property variable
83: *
84: * @return string property value
85: */
86: protected int getIntProp(int valDefault, String envVar,
87: String propsName, String propVar) {
88: int retValue;
89: String strVal = getProp("D", envVar, propsName, propVar);
90: try {
91: retValue = Integer.parseInt(strVal);
92: } catch (NumberFormatException nfe) { // save default value
93: retValue = valDefault;
94: }
95: return retValue;
96: }
97: }
|