1 25 package org.snipsnap.config; 26 27 import org.snipsnap.app.Application; 28 29 import java.io.File ; 30 import java.io.IOException ; 31 import java.io.InputStream ; 32 import java.io.OutputStream ; 33 import java.net.InetAddress ; 34 import java.net.MalformedURLException ; 35 import java.net.URL ; 36 import java.net.UnknownHostException ; 37 import java.util.Iterator ; 38 import java.util.Locale ; 39 import java.util.Properties ; 40 41 46 public class ConfigurationMap { 47 48 private final static String GLOBALS_CONF = "/org/snipsnap/config/globals.conf"; 49 private final static String DEFAULTS_CONF = "/org/snipsnap/config/defaults.conf"; 50 private final static String TRANSPOSE_MAP = "/org/snipsnap/config/transpose.map"; 51 52 53 private Properties properties = null; 55 private Properties defaults = null; 56 57 private Properties transposeMap = null; 59 60 private static File webInfDir = null; 62 private static Properties globals = null; 63 private static Properties globalDefaults = null; 64 65 static { 67 globalDefaults = new Properties (); 68 try { 69 globalDefaults.load(ConfigurationMap.class.getResourceAsStream(GLOBALS_CONF)); 70 } catch (IOException e) { 71 System.err.println("Configuration: unable to load global defaults: " + e.getMessage()); 72 } 73 globals = new Properties (globalDefaults); 74 } 75 76 public void setGlobal(String name, String value) { 77 ConfigurationMap.globals.setProperty(name, value); 78 } 79 80 public String getGlobal(String name) { 81 String value = replaceTokens(ConfigurationMap.globals.getProperty(name)); 82 return "".equals(value) ? null : value; 83 } 84 85 public String getGlobalDefault(String name) { 86 String value = globalDefaults.getProperty(name); 87 return ("".equals(value) ? null : value); 88 } 89 90 91 public Properties getGlobals() { 93 return globals; 94 } 95 96 public void loadGlobals(InputStream stream) throws IOException { 98 ConfigurationMap.globals.load(stream); 99 } 100 101 public void storeGlobals(OutputStream stream) throws IOException { 102 ConfigurationMap.globals.store(stream, "SnipSnap Globals Configuration $Revision: 1606 $"); 103 } 104 105 109 public void setWebInfDir(File dir) { 110 ConfigurationMap.webInfDir = dir; 111 } 112 113 118 public File getWebInfDir() { 119 return ConfigurationMap.webInfDir; 120 } 121 122 public String getFileStore() { 123 return getFileStore((String )Application.get().getObject(Application.OID)).getPath(); 124 } 125 126 public File getFileStore(String applicationOid) { 127 return new File (getGlobal(Globals.APP_FILE_STORE), applicationOid); 128 } 129 130 public String getVersion() { 131 String version = System.getProperty(ServerConfiguration.VERSION); 132 if (null == version) { 133 version = getGlobal(ServerConfiguration.VERSION); 134 } 135 return version; 136 } 137 138 142 public ConfigurationMap(Configuration init) { 143 initDefaults(); 144 webInfDir = init.getWebInfDir(); 145 initialize((Properties ) init.getProperties().clone()); 146 } 147 148 public ConfigurationMap() { 149 initDefaults(); 150 initialize((Properties ) defaults.clone()); 152 } 153 154 private void initDefaults() { 155 defaults = new Properties (); 156 try { 157 defaults.load(Configuration.class.getResourceAsStream(DEFAULTS_CONF)); 158 } catch (Exception e) { 159 System.err.println("Configuration: unable to load defaults: " + e.getMessage()); 160 } 161 } 162 163 private void initialize(Properties initProperties) { 164 properties = initProperties; 165 166 try { 167 transposeMap = new Properties (); 168 transposeMap.load(Configuration.class.getResourceAsStream(TRANSPOSE_MAP)); 169 } catch (Exception e) { 170 System.err.println("Configuration: unable to load transposition map: " + e.getMessage()); 171 } 172 } 173 174 175 180 public void store(OutputStream stream) throws IOException { 181 properties.store(stream, "SnipSnap Configuration $Revision: 1606 $"); 182 } 183 184 192 public boolean load(InputStream stream) throws IOException { 193 properties.load(stream); 194 return !convertOldProperties(); 195 } 196 197 private boolean convertOldProperties() { 198 boolean hasChanged = false; 199 Iterator propIt = transposeMap.keySet().iterator(); 200 while (propIt.hasNext()) { 201 String oldProperty = (String ) propIt.next(); 202 String newProperty = transposeMap.getProperty(oldProperty); 203 String value = properties.getProperty(oldProperty); 204 if (value != null) { 205 if (newProperty != null) { 206 if (newProperty.startsWith("@DEPRECATED")) { 207 if (convertDeprecatedProperty(oldProperty, value)) { 208 properties.remove(oldProperty); 209 hasChanged = true; 210 } else { 211 System.out.println("INFO: Configuration option '" + oldProperty + "' is deprecated:"); 212 System.out.println("INFO: " + newProperty.substring("@DEPRECATED".length())); 213 System.out.println("INFO: Please edit configuration file manually."); 214 } 215 } 216 if (newProperty.startsWith("@DUPLICATE")) { 217 newProperty = newProperty.substring("@DUPLICATE".length()); 218 String newValue = properties.getProperty(newProperty); 219 if (null == newValue || "".equals(newValue)) { 221 System.out.println("INFO: duplicating value of '" + oldProperty + "' to '" + newProperty + "'"); 222 properties.setProperty(newProperty, value); 223 hasChanged = true; 224 } 225 } else { 226 System.out.println("INFO: converting '" + oldProperty + "' to '" + newProperty + "'"); 227 properties.remove(oldProperty); 228 properties.setProperty(newProperty, value); 229 hasChanged = true; 230 } 231 } 232 } 233 } 234 return hasChanged; 235 } 236 237 private boolean convertDeprecatedProperty(String oldProperty, String value) { 238 if ("app.domain".equals(oldProperty) || "app.url".equals(oldProperty)) { 239 if (value != null && value.length() > 0) { 240 try { 241 URL url = new URL (value); 242 System.out.println("INFO: converting '" + oldProperty + "' to 'app.real.*'"); 243 properties.setProperty(Configuration.APP_REAL_HOST, url.getHost()); 244 if (url.getPort() >= 0 && url.getPort() != 80) { 245 properties.setProperty(Configuration.APP_REAL_PORT, "" + url.getPort()); 246 } 247 properties.setProperty(Configuration.APP_REAL_PATH, url.getPath()); 248 } catch (MalformedURLException e) { 249 System.out.println("WARNING: unable to convert '" + oldProperty + "': malformed URL: '" + value + "'"); 250 return false; 251 } 252 } 253 return true; 254 } 255 return false; 256 } 257 258 264 public void set(String name, String value) { 265 properties.setProperty(name, value); 266 } 267 268 275 public String get(String name) { 276 String value = replaceTokens(properties.getProperty(name)); 277 return "".equals(value) ? null : value; 278 } 279 280 private String replaceTokens(String value) { 282 if (value != null) { 283 int idx = value.indexOf("%WEBINF%"); 284 if (idx != -1) { 285 StringBuffer replaced = new StringBuffer (); 286 if (idx > 0) { 287 replaced.append(value.substring(0, idx)); 288 } 289 replaced.append(getWebInfDir().getPath()); 290 int endIdx = idx + "%WEBINF%".length(); 291 if (endIdx < value.length()) { 292 replaced.append(value.substring(endIdx)); 293 } 294 return replaced.toString(); 295 } 296 } 297 298 return value; 299 } 300 301 public String get(String name, String defaultValue) { 302 String value = get(name); 303 if (value == null) { 304 return defaultValue; 305 } 306 return value; 307 } 308 309 public Properties getProperties() { 310 return properties; 311 } 312 313 public String getDefault(String name) { 314 String value = defaults.getProperty(name); 315 return ("".equals(value) ? null : value); 316 } 317 318 public File getFilePath(String applicationOid) { 319 return new File (getFileStore(applicationOid), "snips"); 320 } 321 322 public File getFilePath() { 323 return getFilePath((String ) Application.get().getObject(Application.OID)); 324 } 325 326 public File getIndexPath() { 327 return new File (getFileStore(), "index"); 328 } 329 330 public File getUserPath(String applicationOid) { 331 return new File (getFileStore(applicationOid), "users"); 332 } 333 334 338 public Locale getLocale() { 339 String language = get(Configuration.APP_LANGUAGE, "en"); 340 String country = get(Configuration.APP_COUNTRY, "US"); 341 return new Locale (language, country); 342 } 343 344 public String getPath() { 345 if ("true".equals(get(Configuration.APP_REAL_AUTODETECT))) { 346 String realPath = get(Configuration.APP_REAL_PATH); 347 if (null != realPath) { 348 return realPath; 349 } 350 } 351 return getGlobal(Globals.APP_PATH); 352 } 353 354 357 public String getUrl() { 358 URL url = (URL )Application.get().getObject(Application.URL); 359 if(null != url) { 360 return url.toExternalForm(); 361 } 362 363 String prot = get(Configuration.APP_REAL_PROTOCOL, "http"); 364 String host = get(Configuration.APP_REAL_HOST); 365 String port = get(Configuration.APP_REAL_PORT); 366 String path = get(Configuration.APP_REAL_PATH); 367 368 if (null == host) { 369 host = getGlobal(Globals.APP_HOST); 370 port = getGlobal(Globals.APP_PORT); 371 path = getGlobal(Globals.APP_PATH); 372 } 373 374 StringBuffer tmp = new StringBuffer (); 375 tmp.append(prot).append("://"); 376 try { 377 tmp.append(host == null || host.length() == 0 ? InetAddress.getLocalHost().getHostName() : host); 378 } catch (UnknownHostException e) { 379 tmp.append(System.getProperty("host", "localhost")); 380 } 381 if (port != null && !"80".equals(port)) { 382 tmp.append(":"); 383 tmp.append(port); 384 } 385 tmp.append(path != null ? path : ""); 386 return tmp.toString(); 387 } 388 389 394 public String getUrl(String target) { 395 return getUrl() + target; 396 } 397 398 402 public String getSnipUrl(String snipName) { 403 return getUrl("/space/" + snipName); 404 } 405 406 public boolean allow(String action) { 408 return "allow".equals(get(action)); 409 } 410 411 public boolean deny(String action) { 412 return "deny".equals(get(action)); 413 } 414 415 public boolean getAllowRegister() { 416 return !deny(Configuration.APP_PERM_REGISTER); 417 } 418 419 public boolean isConfigured() { 420 return "true".equals(properties.getProperty(Configuration.APP_CONFIGURED)); 421 } 422 423 public boolean isInstalled() { 424 return "true".equals(ConfigurationMap.globals.getProperty(Globals.APP_INSTALLED)); 425 } 426 427 428 public String toString() { 429 StringBuffer result = new StringBuffer (); 430 Iterator it = properties.keySet().iterator(); 431 result.append("{"); 432 while (it.hasNext()) { 433 String key = (String ) it.next(); 434 result.append(key).append("=" + properties.get(key)).append(","); 435 } 436 result.append("}"); 437 return result.toString(); 438 } 439 } 440 | Popular Tags |