1 19 package org.openharmonise.rm.dsi; 20 21 import java.io.*; 22 import java.util.*; 23 import java.util.logging.*; 24 25 import org.openharmonise.rm.config.*; 26 27 28 36 public class DatabaseSettings { 37 38 41 public static final String PROPERTIES_FILENAME = "/harmonise.properties"; 42 45 private File properties_file; 46 47 50 private static Logger logger = Logger.getLogger(DatabaseSettings.class.getName()); 51 52 53 private String dsiClass = null; 54 private String dbUrl = null; 55 private String dbPwd = null; 56 private String dbDriver = null; 57 private String dbUser = null; 58 59 private static DatabaseSettings instance; 60 61 71 public static void createDatabaseSettings(String dbUser, String dbPwd, String dbUrl, String dbDriver, String dsiClass) throws ConfigException { 72 instance = new DatabaseSettings(); 74 instance.setDbUser(dbUser); 75 instance.setDbPwd(dbPwd); 76 instance.setDbUrl(dbUrl); 77 instance.setDbDriver(dbDriver); 78 instance.setDsiClass(dsiClass); 79 } 80 81 82 88 private DatabaseSettings() throws ConfigException { 89 } 90 91 public static void createDatabaseSettingsFromFile() throws ConfigException { 92 Properties props = new Properties(); 93 94 String classpath = System.getProperty("java.class.path"); 95 StringTokenizer paths = new StringTokenizer(classpath, File.pathSeparator); 96 File propertiesFile = null; 97 boolean found = false; 98 while (!found && paths.hasMoreTokens()) { 99 String path = paths.nextToken(); 100 propertiesFile = new File(path, "harmonise.properties"); 101 found = propertiesFile.exists(); 102 } 103 if (found) { 104 105 try { 106 props.load(new FileInputStream(propertiesFile)); 107 } catch (FileNotFoundException e) { 108 throw new ConfigException(e); 109 } catch (IOException e) { 110 throw new ConfigException(e); 111 } 112 instance = new DatabaseSettings(); 113 instance.setDbUser(props.getProperty(DataStoreInterfaceFactory.DBUSR_PNAME)); 114 instance.setDbPwd(props.getProperty(DataStoreInterfaceFactory.DBPWD_PNAME)); 115 instance.setDbUrl(props.getProperty(DataStoreInterfaceFactory.DBURL_PNAME)); 116 instance.setDbDriver(props.getProperty(DataStoreInterfaceFactory.DBDRV_PNAME)); 117 instance.setDsiClass(props.getProperty(DataStoreInterfaceFactory.DSI_PNAME)); 118 119 120 } 121 } 122 123 129 public static DatabaseSettings getInstance() throws ConfigException { 130 142 if (instance == null) { 143 createDatabaseSettingsFromFile(); 144 } 145 return instance; 146 } 147 148 151 public String getDbDriver() { 152 return dbDriver; 153 } 154 157 private void setDbDriver(String dbDriver) { 158 if (dbDriver == null || dbDriver.length() == 0) { 159 throw new IllegalArgumentException ("dbDriver may not be null or zero length"); 160 } 161 this.dbDriver = dbDriver; 162 } 163 166 public String getDbPwd() { 167 return dbPwd; 168 } 169 172 private void setDbPwd(String dbPwd) { 173 if (dbPwd == null || dbPwd.length() == 0) { 174 throw new IllegalArgumentException ("dbPwd may not be null or zero length"); 175 } 176 this.dbPwd = dbPwd; 177 } 178 179 182 public String getDbUrl() { 183 return dbUrl; 184 } 185 186 189 private void setDbUrl(String dbUrl) { 190 if (dbUrl == null || dbUrl.length() == 0) { 191 throw new IllegalArgumentException ("dbUrl may not be null or zero length"); 192 } 193 this.dbUrl = dbUrl; 194 } 195 196 199 public String getDbUser() { 200 return dbUser; 201 } 202 203 206 private void setDbUser(String dbUser) { 207 if (dbUser == null || dbUser.length() == 0) { 208 throw new IllegalArgumentException ("dbUser may not be null or zero length"); 209 } 210 this.dbUser = dbUser; 211 } 212 213 216 public String getDsiClass() { 217 return dsiClass; 218 } 219 220 223 private void setDsiClass(String dsiClass) { 224 if (dsiClass == null || dsiClass.length() == 0) { 225 throw new IllegalArgumentException ("dsiClass may not be null or zero length"); 226 } 227 this.dsiClass = dsiClass; 228 } 229 230 231 } 232 | Popular Tags |