1 package com.ontotext.ordi.wsmo4rdf.remote.server; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.util.Properties; 7 8 import org.apache.log4j.Logger; 9 10 /** 11 * Service-wide configuration data, loaded from properties file. 12 * 13 * @author mihail.konstantinov@ontotext.com 14 */ 15 public class Configuration { 16 private static final String ORDIWS_DIR_SOURCES = "ordiws.dir.sources"; 17 private static final String ORDIWS_CONNECTIONS_MAX = "ordiws.connections.max"; 18 private static final String ORDIWS_SOURCE_MAX = "ordiws.source.max"; 19 private static final String CONF_FILENAME = "ordiws-service.properties"; 20 private static final Properties confProps = new Properties(); 21 private static Logger logger = Logger.getLogger(Configuration.class); 22 23 private static Configuration instance; 24 25 private int maxSources = 5; 26 private int maxConnections = 100; 27 private File storeDirPath = new File("."); 28 29 private Configuration() { 30 InputStream resourceAsStream = null; 31 try { 32 resourceAsStream = RegistryManager.class.getResourceAsStream("/"+CONF_FILENAME); 33 if (resourceAsStream == null) { 34 throw new IOException(); 35 } 36 confProps.load(resourceAsStream); 37 } catch (IOException e) { 38 logger.warn("Configuration file " + CONF_FILENAME + " cannot be loaded. Using defaults."); 39 } finally { 40 try { 41 if (resourceAsStream != null) { 42 resourceAsStream.close(); 43 resourceAsStream = null; 44 } 45 } catch (IOException e) {} 46 } 47 48 try { 49 maxSources = Integer.parseInt(confProps.getProperty(ORDIWS_SOURCE_MAX)); 50 } catch (NumberFormatException nfe) { 51 logInvalidValue(ORDIWS_SOURCE_MAX); 52 } 53 54 try { 55 maxConnections = Integer.parseInt(confProps.getProperty(ORDIWS_CONNECTIONS_MAX)); 56 } catch (NumberFormatException nfe) { 57 logInvalidValue(ORDIWS_CONNECTIONS_MAX); 58 } 59 60 String dirSourcesPath = confProps.getProperty(ORDIWS_DIR_SOURCES); 61 if (dirSourcesPath != null) { 62 File dsp = new File(dirSourcesPath); 63 if (!dsp.exists() || !dsp.isDirectory() || !dsp.canRead()) { 64 logInvalidValue(ORDIWS_DIR_SOURCES); 65 } else { 66 this.storeDirPath = dsp; 67 } 68 } 69 } 70 71 public static Configuration newInstance() { 72 if (instance == null) { 73 instance = new Configuration(); 74 } 75 assert(instance != null); 76 return instance; 77 } 78 79 public int getMaxSources() { 80 return this.maxSources; 81 } 82 83 public int getMaxConnections() { 84 return this.maxConnections; 85 } 86 87 public File getStoreDirPath() { 88 return this.storeDirPath; 89 } 90 91 private void logInvalidValue(final String propName) { 92 logger.warn("Invalid value in " + CONF_FILENAME + " configuration file for \"" + propName + "\". Will use default value."); 93 } 94 }