1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: TestProperties.java,v 1.2 2004/01/31 13:44:24 tanderson Exp $
44 */
45 package org.exolab.jmscts.core;
46
47 import org.apache.log4j.Category;
48
49 import java.io.File;
50 import java.io.FileInputStream;
51 import java.io.IOException;
52 import java.util.Properties;
53
54
55 /***
56 * Helper class to provide access to properties defined in a
57 * <em>jmscts.properties</em> property file.
58 * This is read from the ${jmscts.home}/config directory.
59 *
60 * @version $Revision: 1.2 $ $Date: 2004/01/31 13:44:24 $
61 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62 */
63 public final class TestProperties {
64
65 /***
66 * The properties
67 */
68 private static final Properties PROPS;
69
70 /***
71 * The logger
72 */
73 private static final Category log =
74 Category.getInstance(TestProperties.class.getName());
75
76
77 /***
78 * Prevent construction of utility class
79 */
80 private TestProperties() {
81 }
82
83 /***
84 * Returns the value of a property. The property name is the
85 * concatentation of prefix.getName() + "." + name
86 *
87 * @param prefix the property name prefix
88 * @param name the property name suffix
89 * @param defaultValue the value to use if no property exists
90 * @return the value of the property, or <code>defaultValue</code> if
91 * no property exists
92 */
93 public static String getString(Class prefix, String name,
94 String defaultValue) {
95 return getString(concat(prefix, name), defaultValue);
96 }
97
98 /***
99 * Returns the value of a property.
100 *
101 * @param name the property name
102 * @param defaultValue the value to use if no property exists
103 * @return the value of the property, or <code>defaultValue</code> if
104 * no property exists
105 */
106 public static String getString(String name, String defaultValue) {
107 return PROPS.getProperty(name, defaultValue);
108 }
109
110 /***
111 * Returns the value of a property. The property name is the
112 * concatentation of prefix.getName() + "." + name
113 *
114 * @param prefix the property name prefix
115 * @param name the property name suffix
116 * @param defaultValue the value to use if no property exists
117 * @return the value of the property, or <code>defaultValue</code> if
118 * no property exists
119 */
120 public static int getInt(Class prefix, String name, int defaultValue) {
121 return getInt(concat(prefix, name), defaultValue);
122 }
123
124 /***
125 * Returns the value of a property.
126 *
127 * @param name the property name
128 * @param defaultValue the value to use if no property exists
129 * @return the value of the property, or <code>defaultValue</code> if
130 * no property exists
131 */
132 public static int getInt(String name, int defaultValue) {
133 int result = defaultValue;
134 String value = getString(name, Integer.toString(defaultValue));
135 try {
136 result = Integer.parseInt(value);
137 } catch (NumberFormatException ignore) {
138 log.warn("Error parsing integer property=" + name
139 + ", using default value");
140 }
141 return result;
142 }
143
144 /***
145 * Returns the value of a property. The property name is the
146 * concatentation of prefix.getName() + "." + name
147 *
148 * @param prefix the property name prefix
149 * @param name the property name suffix
150 * @param defaultValue the value to use if no property exists
151 * @return the value of the property, or <code>defaultValue</code> if
152 * no property exists
153 */
154 public static long getLong(Class prefix, String name, long defaultValue) {
155 return getLong(concat(prefix, name), defaultValue);
156 }
157
158 /***
159 * Returns the value of a property.
160 *
161 * @param name the property name
162 * @param defaultValue the value to use if no property exists
163 * @return the value of the property, or <code>defaultValue</code> if
164 * no property exists
165 */
166 public static long getLong(String name, long defaultValue) {
167 long result = defaultValue;
168 String value = getString(name, Long.toString(defaultValue));
169 try {
170 result = Long.parseLong(value);
171 } catch (NumberFormatException ignore) {
172 log.warn("Error parsing long property=" + name
173 + ", using default value");
174 }
175 return result;
176 }
177
178 /***
179 * Helper to concatenate the class prefix and name to form a property name
180 *
181 * @param prefix the property name prefix
182 * @param name the property name suffix
183 * @return the property name
184 */
185 private static String concat(Class prefix, String name) {
186 return prefix.getName() + "." + name;
187 }
188
189 static {
190 PROPS = new Properties();
191 String home = System.getProperty("jmscts.home");
192 if (home == null) {
193 log.warn("jmscts.home not set - not loading properties");
194 } else {
195 String path = home + "/config/jmscts.properties";
196 try {
197 File file = new File(path);
198 if (file.exists()) {
199 FileInputStream stream = new FileInputStream(file);
200 PROPS.load(stream);
201 } else {
202 log.warn("jmscts.properties not found: path=" + path);
203 }
204 } catch (IOException exception) {
205 log.error("Failed to read " + path, exception);
206 }
207 }
208 }
209
210 }
This page was automatically generated by Maven