001: /**
002: * Copyright (C) 2001 Yasna.com. All rights reserved.
003: *
004: * ===================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * Yasna.com (http://www.yasna.com)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "Yazd" and "Yasna.com" must not be used to
027: * endorse or promote products derived from this software without
028: * prior written permission. For written permission, please
029: * contact yazd@yasna.com.
030: *
031: * 5. Products derived from this software may not be called "Yazd",
032: * nor may "Yazd" appear in their name, without prior written
033: * permission of Yasna.com.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of Yasna.com. For more information
051: * on Yasna.com, please see <http://www.yasna.com>.
052: */package com.Yasna.forum.database;
053:
054: import java.util.*;
055: import java.io.*;
056:
057: /**
058: * obtains the properties for the table name
059: */
060: public class SystemProperty {
061:
062: private static SystemProperty manager = null;
063: private static Object managerLock = new Object();
064: private static String propsName = "/SystemYazd.properties";
065:
066: /**
067: * Returns a Yazd property.
068: *
069: * @param name the name of the property to return.
070: * @return the property value specified by name.
071: */
072: public static String getProperty(String name) {
073: if (manager == null) {
074: synchronized (managerLock) {
075: if (manager == null) {
076: manager = new SystemProperty(propsName);
077: }
078: }
079: }
080: return manager.getProp(name);
081: }
082:
083: /**
084: * Returns the names of the Yazd properties.
085: *
086: * @return an Enumeration of the Yazd property names.
087: */
088: public static Enumeration propertyNames() {
089: if (manager == null) {
090: synchronized (managerLock) {
091: if (manager == null) {
092: manager = new SystemProperty(propsName);
093: }
094: }
095: }
096: return manager.propNames();
097: }
098:
099: /**
100: * Returns true if the properties are readable. This method is mainly
101: * valuable at setup time to ensure that the properties file is setup
102: * correctly.
103: */
104: public static boolean propertyFileIsReadable() {
105: if (manager == null) {
106: synchronized (managerLock) {
107: if (manager == null) {
108: manager = new SystemProperty(propsName);
109: }
110: }
111: }
112: return manager.propFileIsReadable();
113: }
114:
115: /**
116: * Returns true if the properties file exists where the path property
117: * purports that it does.
118: */
119: public static boolean propertyFileExists() {
120: if (manager == null) {
121: synchronized (managerLock) {
122: if (manager == null) {
123: manager = new SystemProperty(propsName);
124: }
125: }
126: }
127: return manager.propFileExists();
128: }
129:
130: private Properties properties = null;
131: private Object propertiesLock = new Object();
132: private String resourceURI;
133:
134: /**
135: * Creates a new SystemProperty. Singleton access only.
136: */
137: private SystemProperty(String resourceURI) {
138: this .resourceURI = resourceURI;
139: }
140:
141: /**
142: * Gets a Yazd property. Yazd properties are stored in SystemYazd.properties.
143: * The properties file should be accesible from the classpath. Additionally,
144: * it should have a path field that gives the full path to where the
145: * file is located. Getting properties is a fast operation.
146: *
147: * @param name the name of the property to get.
148: * @return the property specified by name.
149: */
150: protected String getProp(String name) {
151: //If properties aren't loaded yet. We also need to make this thread
152: //safe, so synchronize...
153: if (properties == null) {
154: synchronized (propertiesLock) {
155: //Need an additional check
156: if (properties == null) {
157: loadProps();
158: }
159: }
160: }
161: String property = properties.getProperty(name);
162: if (property == null) {
163: return null;
164: } else {
165: return property.trim();
166: }
167: }
168:
169: protected Enumeration propNames() {
170: //If properties aren't loaded yet. We also need to make this thread
171: //safe, so synchronize...
172: if (properties == null) {
173: synchronized (propertiesLock) {
174: //Need an additional check
175: if (properties == null) {
176: loadProps();
177: }
178: }
179: }
180: return properties.propertyNames();
181: }
182:
183: /**
184: * Loads Yazd properties from the disk.
185: */
186: private void loadProps() {
187: properties = new Properties();
188: InputStream in = null;
189: try {
190: in = getClass().getResourceAsStream(resourceURI);
191: properties.load(in);
192: if (properties.getProperty("PeriodBetweenPosts") == null) {
193: properties.setProperty("PeriodBetweenPosts", "60000"); // 1 minutes
194: }
195: if (properties.getProperty("WaitPeriod") == null) {
196: properties.setProperty("WaitPeriod", "10000"); // 10 seconds
197: }
198: if (properties.getProperty("JNDI.dataprovider") == null) {
199: properties.setProperty("JNDI.dataprovider",
200: "java:/comp/env/jdbc/yazd"); // default jndi name
201: }
202: if (properties.getProperty("RemoveNotActiveAccounts") == null) {
203: properties.setProperty("RemoveNotActiveAccounts",
204: "false"); // default jndi name
205: }
206: } catch (Exception e) {
207: System.err
208: .println("Error reading Yazd System properties in SystemProperty.loadProps() "
209: + e);
210: System.err
211: .println("loading the default properties for User table ");
212: properties = getDefaultProperties();
213: e.printStackTrace();
214: } finally {
215: try {
216: in.close();
217: } catch (Exception e) {
218: }
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()
230: .getResourceAsStream(resourceURI);
231: return true;
232: } catch (Exception 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 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: } else {
250: return false;
251: }
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",
266: "java:/comp/env/jdbc/yazd");
267: return prop;
268: }
269:
270: }
|