1 /***
2 * The contents of this file are subject to the Mozilla Public
3 * License Version 1.1 (the "License"); you may not use this file
4 * except in compliance with the License. You may obtain a copy of
5 * the License at http://www.mozilla.org/MPL/
6 *
7 * Software distributed under the License is distributed on an "AS
8 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9 * implied. See the License for the specific language governing
10 * rights and limitations under the License.
11 *
12 * The Original Code is pow2toolkit library.
13 *
14 * The Initial Owner of the Original Code is
15 * Power Of Two S.R.L. (www.pow2.com)
16 *
17 * Portions created by Power Of Two S.R.L. are
18 * Copyright (C) Power Of Two S.R.L.
19 * All Rights Reserved.
20 *
21 * Contributor(s):
22 */
23
24 package com.pow2.resources;
25
26 import com.pow2.util.FileUtil;
27
28
29 import java.io.PrintWriter;
30 import java.io.StringWriter;
31
32 import org.apache.log4j.PropertyConfigurator;
33
34
35 /***
36 * Prefs class
37 *
38 * @author Luca Fossato
39 * @created 22 aprile 2002
40 */
41 public class Prefs extends PropertyContainerImpl
42 {
43 private static Prefs instance = null;
44 private String propertiesFile;
45 private String log4jPropertiesFile;
46
47
48 /***
49 * Default private constructor.
50 */
51 private Prefs()
52 {
53 }
54
55
56 /***
57 * Get the instance of Prefs class.<br>
58 *
59 * @return the instance of Prefs class.
60 */
61 public static synchronized Prefs instance()
62 {
63 if (instance == null)
64 instance = new Prefs();
65
66 return instance;
67 }
68
69
70 /***
71 * Get the name of the properties file.
72 *
73 * @return the name of the properties file.
74 */
75 public String getpropertiesFile()
76 {
77 return propertiesFile;
78 }
79
80
81 /***
82 * Set the name of the properties file.
83 *
84 * @param v the name of the properties file.
85 */
86 public void setpropertiesFile(String v)
87 {
88 propertiesFile = v;
89 }
90
91
92 /***
93 * Configure the Prefs class using the properties file
94 * defined by <code>propertiesFile</code> class member attribute.
95 *
96 * @exception Exception if any error occurs
97 */
98 public void configure() throws Exception
99 {
100 configure(propertiesFile);
101 }
102
103
104 /***
105 * Configure the Prefs class using the input properties file.
106 *
107 * @param propertiesFile the full path name of the properties file
108 * @exception Exception if any error occurs
109 */
110 public void configure(String propertiesFile) throws Exception
111 {
112
113
114 properties = FileUtil.getProperties(propertiesFile, true);
115
116
117
118
119 String log4JProp = getProperty("log4j.configuration");
120
121 log4jPropertiesFile = ((log4JProp != null) ? log4JProp : propertiesFile);
122 addProperty("log4j.configuration", log4jPropertiesFile);
123
124
125 if (log4jPropertiesFile != null)
126 {
127 PropertyConfigurator.configure(log4jPropertiesFile);
128 cat.info("::initLog4J - log4j properly configured using the properties file ["
129 + log4jPropertiesFile + "]");
130 }
131 }
132
133
134 /***
135 * Get a String with the list of all the Prefs properties.
136 *
137 * @return a string with the list of all the Prefs properties.
138 */
139 public String toString()
140 {
141 StringWriter sw = new StringWriter();
142 PrintWriter pw = new PrintWriter(sw);
143 properties.list(pw);
144
145 String res = sw.toString();
146 pw.close();
147
148 try
149 {
150 sw.close();
151 }
152 catch(java.io.IOException e)
153 {
154 cat.error("::toString - cannot close the StringWriter object");
155 }
156
157 return res;
158 }
159 }