KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > database > SystemProperty


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 package com.Yasna.forum.database;
55
56 import java.util.*;
57 import java.io.*;
58
59 /**
60  * obtains the properties for the table name
61  */

62 public class SystemProperty {
63
64
65     private static SystemProperty manager = null;
66     private static Object JavaDoc managerLock = new Object JavaDoc();
67     private static String JavaDoc propsName = "/SystemYazd.properties";
68
69
70     /**
71      * Returns a Yazd property.
72      *
73      * @param name the name of the property to return.
74      * @return the property value specified by name.
75      */

76     public static String JavaDoc getProperty(String JavaDoc name) {
77         if (manager == null) {
78             synchronized(managerLock) {
79                 if (manager == null) {
80                     manager = new SystemProperty(propsName);
81                 }
82             }
83         }
84         return manager.getProp(name);
85     }
86     /**
87      * Returns the names of the Yazd properties.
88      *
89      * @return an Enumeration of the Yazd property names.
90      */

91     public static Enumeration propertyNames() {
92         if (manager == null) {
93             synchronized(managerLock) {
94                 if (manager == null) {
95                     manager = new SystemProperty(propsName);
96                 }
97             }
98         }
99         return manager.propNames();
100     }
101
102     /**
103      * Returns true if the properties are readable. This method is mainly
104      * valuable at setup time to ensure that the properties file is setup
105      * correctly.
106      */

107     public static boolean propertyFileIsReadable() {
108         if (manager == null) {
109             synchronized(managerLock) {
110                 if (manager == null) {
111                     manager = new SystemProperty(propsName);
112                 }
113             }
114         }
115         return manager.propFileIsReadable();
116     }
117
118     /**
119      * Returns true if the properties file exists where the path property
120      * purports that it does.
121      */

122     public static boolean propertyFileExists() {
123         if (manager == null) {
124             synchronized(managerLock) {
125                 if (manager == null) {
126                     manager = new SystemProperty(propsName);
127                 }
128             }
129         }
130         return manager.propFileExists();
131     }
132
133
134     private Properties properties = null;
135     private Object JavaDoc propertiesLock = new Object JavaDoc();
136     private String JavaDoc resourceURI;
137
138     /**
139      * Creates a new SystemProperty. Singleton access only.
140      */

141     private SystemProperty(String JavaDoc resourceURI) {
142         this.resourceURI = resourceURI;
143     }
144
145     /**
146      * Gets a Yazd property. Yazd properties are stored in SystemYazd.properties.
147      * The properties file should be accesible from the classpath. Additionally,
148      * it should have a path field that gives the full path to where the
149      * file is located. Getting properties is a fast operation.
150      *
151      * @param name the name of the property to get.
152      * @return the property specified by name.
153      */

154     protected String JavaDoc getProp(String JavaDoc name) {
155         //If properties aren't loaded yet. We also need to make this thread
156
//safe, so synchronize...
157
if (properties == null) {
158             synchronized(propertiesLock) {
159                 //Need an additional check
160
if (properties == null) {
161                     loadProps();
162                 }
163             }
164         }
165         String JavaDoc property = properties.getProperty(name);
166         if (property == null) {
167             return null;
168         }
169         else {
170             return property.trim();
171         }
172     }
173     protected Enumeration propNames() {
174         //If properties aren't loaded yet. We also need to make this thread
175
//safe, so synchronize...
176
if (properties == null) {
177             synchronized(propertiesLock) {
178                 //Need an additional check
179
if (properties == null) {
180                     loadProps();
181                 }
182             }
183         }
184         return properties.propertyNames();
185     }
186
187     /**
188      * Loads Yazd properties from the disk.
189      */

190     private void loadProps() {
191         properties = new Properties();
192         InputStream in = null;
193         try {
194             in = getClass().getResourceAsStream(resourceURI);
195             properties.load(in);
196             if(properties.getProperty("PeriodBetweenPosts") == null){
197                 properties.setProperty("PeriodBetweenPosts","60000"); // 1 minutes
198
}
199             if(properties.getProperty("WaitPeriod") == null){
200                 properties.setProperty("WaitPeriod","10000"); // 10 seconds
201
}
202             if(properties.getProperty("JNDI.dataprovider") == null){
203                 properties.setProperty("JNDI.dataprovider","java:/comp/env/jdbc/yazd"); // default jndi name
204
}
205             if(properties.getProperty("RemoveNotActiveAccounts") == null){
206                 properties.setProperty("RemoveNotActiveAccounts","false"); // default jndi name
207
}
208         }
209         catch (Exception JavaDoc e) {
210             System.err.println("Error reading Yazd System properties in SystemProperty.loadProps() " + e);
211             System.err.println("loading the default properties for User table ");
212         properties = getDefaultProperties();
213             e.printStackTrace();
214         }
215         finally {
216             try {
217                 in.close();
218             } catch (Exception JavaDoc e) { }
219         }
220     }
221
222     /**
223      * Returns true if the properties are readable. This method is mainly
224      * valuable at setup time to ensure that the properties file is setup
225      * correctly.
226      */

227     public boolean propFileIsReadable() {
228         try {
229             InputStream in = getClass().getResourceAsStream(resourceURI);
230             return true;
231         }
232         catch (Exception JavaDoc e) {
233             return false;
234         }
235     }
236
237     /**
238      * Returns true if the yazd.properties file exists where the path property
239      * purports that it does.
240      */

241     public boolean propFileExists() {
242         String JavaDoc path = getProp("path");
243         if( path == null ) {
244             return false;
245         }
246         File file = new File(path);
247         if (file.isFile()) {
248             return true;
249         }
250         else {
251             return false;
252         }
253     }
254     private Properties getDefaultProperties(){
255         Properties prop = new Properties();
256         prop.setProperty("User.Table","yazdUser");
257         prop.setProperty("User.Column.UserID","userID");
258         prop.setProperty("User.Column.Name","name");
259         prop.setProperty("User.Column.Username","username");
260         prop.setProperty("User.Column.PasswordHash","passwordHash");
261         prop.setProperty("User.Column.Email","email");
262         prop.setProperty("WaitPeriod","10000"); // 10 seconds
263
prop.setProperty("RemoveNotActiveAccounts","false");
264         prop.setProperty("PeriodBetweenPosts","60000"); // 1 minutes
265
prop.setProperty("JNDI.dataprovider","java:/comp/env/jdbc/yazd");
266         return prop;
267     }
268
269 }
270
Popular Tags