1 53 54 package com.Yasna.forum.database; 55 56 import java.util.*; 57 import java.io.*; 58 59 62 public class SystemProperty { 63 64 65 private static SystemProperty manager = null; 66 private static Object managerLock = new Object (); 67 private static String propsName = "/SystemYazd.properties"; 68 69 70 76 public static String getProperty(String 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 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 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 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 propertiesLock = new Object (); 136 private String resourceURI; 137 138 141 private SystemProperty(String resourceURI) { 142 this.resourceURI = resourceURI; 143 } 144 145 154 protected String getProp(String name) { 155 if (properties == null) { 158 synchronized(propertiesLock) { 159 if (properties == null) { 161 loadProps(); 162 } 163 } 164 } 165 String 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 == null) { 177 synchronized(propertiesLock) { 178 if (properties == null) { 180 loadProps(); 181 } 182 } 183 } 184 return properties.propertyNames(); 185 } 186 187 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"); } 199 if(properties.getProperty("WaitPeriod") == null){ 200 properties.setProperty("WaitPeriod","10000"); } 202 if(properties.getProperty("JNDI.dataprovider") == null){ 203 properties.setProperty("JNDI.dataprovider","java:/comp/env/jdbc/yazd"); } 205 if(properties.getProperty("RemoveNotActiveAccounts") == null){ 206 properties.setProperty("RemoveNotActiveAccounts","false"); } 208 } 209 catch (Exception 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 e) { } 219 } 220 } 221 222 227 public boolean propFileIsReadable() { 228 try { 229 InputStream in = getClass().getResourceAsStream(resourceURI); 230 return true; 231 } 232 catch (Exception e) { 233 return false; 234 } 235 } 236 237 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 } 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"); prop.setProperty("RemoveNotActiveAccounts","false"); 264 prop.setProperty("PeriodBetweenPosts","60000"); prop.setProperty("JNDI.dataprovider","java:/comp/env/jdbc/yazd"); 266 return prop; 267 } 268 269 } 270 | Popular Tags |