1 24 package org.objectweb.jalisto.se.impl; 25 26 import org.objectweb.jalisto.se.api.JalistoProperties; 27 import org.objectweb.jalisto.se.api.internal.Constants; 28 import org.objectweb.jalisto.se.exception.JalistoException; 29 import org.objectweb.jalisto.se.impl.util.JalistoUtils; 30 31 import java.io.File ; 32 import java.io.IOException ; 33 import java.util.*; 34 35 public class JalistoPropertiesImpl implements JalistoProperties { 36 37 private static final String NAME = "jalisto"; 38 private static final String IMPLEMENTATION = MONO_IMPLEMENTATION; 39 private static final String DATABASE_FILES_PATHS = ""; 40 private static final String DATABASE_INITIAL_SIZE = "1000"; 41 private static final String CACHE_IMPLEMENTATION = Constants.CACHE_DIC; 42 private static final String OBJECT_CACHE_SIZE = "0"; 43 private static final String PAGE_CACHE_SIZE = "20"; 44 private static final String CACHE_CLEAR_POURCENT = "20"; 45 private static final String OIDTABLE_SIZE = "10000"; 46 private static final String OBJECT_INST_PAGE_SIZE = "200"; 47 private static final String OBJECT_SYST_PAGE_SIZE = "10"; 48 private static final String OBJECT_CLASS_PAGE_SIZE = "10"; 49 private static final String OBJECT_OID_PAGE_SIZE = "50"; 50 private static final String CONCURRENCY_MODE = Constants.OPTIMISTIC_MODE; 51 private static final String TIME_OUT = "100"; 52 private static final String ADD_SPACE = "15"; 53 private static final String INTERNAL_KEY_MAX_SIZE = "12"; 54 private static final String ALLOWS_SPECIAL_FUNCTIONNALITIES = "yes"; 55 private static final String MBEAN_JMX_SERVER_PORT = "8082"; 56 private static final String PHYSICAL_ACCESS_CLASS = Constants.PHYSICAL_FILE_ACCESS_DIC; 57 private static final String INTERNAL_FACTORY_CLASS = Constants.INTERNALFACTORYCLASS_DIC; 58 private static final String COMMUNICATION_FACTORY_CLASS = Constants.COMMUNICATION_FACTORY_DIC; 59 private static final String QUERY_MANAGER_CLASS = Constants.QUERY_MANAGER; 60 private static final String MBEAN_SERVER_CLASS = Constants.JMX_SERVER_DIC; 61 private static final String KEEP_IN_MEMORY = "yes"; 62 private static final String CLEAN_LOG = "yes"; 63 private static final String LOG_FILE = "log.jalisto"; 64 private static final String TRACE = ""; 65 66 private File parent; 67 private Properties prop; 68 private String [] pathes; 69 private String propertiesPath; 70 71 static final long serialVersionUID = -7589477069041161459L; 72 73 public JalistoPropertiesImpl(String databasePropertiesFilePath) { 74 this.propertiesPath = databasePropertiesFilePath; 75 76 try { 77 prop = new Properties(); 78 prop.put(NAME_KEY, NAME); 79 prop.put(IMPLEMENTATION_KEY, IMPLEMENTATION); 80 prop.put(DATABASE_FILES_PATHS_KEY, DATABASE_FILES_PATHS); 81 prop.put(DATABASE_INITIAL_SIZE_KEY, DATABASE_INITIAL_SIZE); 82 prop.put(CACHE_IMPLEMENTATION_KEY, CACHE_IMPLEMENTATION); 83 prop.put(OBJECT_CACHE_SIZE_KEY, OBJECT_CACHE_SIZE); 84 prop.put(PAGE_CACHE_SIZE_KEY, PAGE_CACHE_SIZE); 85 prop.put(CACHE_CLEAR_POURCENT_KEY, CACHE_CLEAR_POURCENT); 86 prop.put(OIDTABLE_SIZE_KEY, OIDTABLE_SIZE); 87 prop.put(OBJECT_INST_PAGE_SIZE_KEY, OBJECT_INST_PAGE_SIZE); 88 prop.put(OBJECT_SYST_PAGE_SIZE_KEY, OBJECT_SYST_PAGE_SIZE); 89 prop.put(OBJECT_CLASS_PAGE_SIZE_KEY, OBJECT_CLASS_PAGE_SIZE); 90 prop.put(OBJECT_OID_PAGE_SIZE_KEY, OBJECT_OID_PAGE_SIZE); 91 prop.put(CONCURRENCY_MODE_KEY, CONCURRENCY_MODE); 92 prop.put(TIME_OUT_KEY, TIME_OUT); 93 prop.put(ADD_SPACE_KEY, ADD_SPACE); 94 prop.put(INTERNAL_KEY_MAX_SIZE_KEY, INTERNAL_KEY_MAX_SIZE); 95 prop.put(ALLOWS_SPECIAL_FUNCTIONNALITIES_KEY, ALLOWS_SPECIAL_FUNCTIONNALITIES); 96 prop.put(PHYSICAL_ACCESS_CLASS_KEY, PHYSICAL_ACCESS_CLASS); 97 prop.put(INTERNAL_FACTORY_CLASS_KEY, INTERNAL_FACTORY_CLASS); 98 prop.put(COMMUNICATION_FACTORY_CLASS_KEY, COMMUNICATION_FACTORY_CLASS); 99 prop.put(QUERY_MANAGER_CLASS_KEY, QUERY_MANAGER_CLASS); 100 prop.put(MBEAN_SERVER_CLASS_KEY, MBEAN_SERVER_CLASS); 101 prop.put(MBEAN_JMX_SERVER_PORT_KEY, MBEAN_JMX_SERVER_PORT); 102 prop.put(KEEP_IN_MEMORY_KEY, KEEP_IN_MEMORY); 103 prop.put(CLEAN_LOG_KEY, CLEAN_LOG); 104 prop.put(LOG_FILE_KEY, ""); 105 prop.put(TRACE_KEY, TRACE); 106 107 File propfile; 108 boolean isLoaded; 109 if (!databasePropertiesFilePath.equalsIgnoreCase("")) { 110 propfile = JalistoUtils.loadPropertiesFileIn(databasePropertiesFilePath, prop); 111 parent = propfile.getAbsoluteFile().getParentFile(); 112 isLoaded = true; 113 } else { 114 parent = new File (System.getProperty("user.dir")); 115 System.out.println("Use default properties values with execution path : " + parent.getAbsolutePath()); 116 isLoaded = true; 117 } 118 119 if (!isLoaded) { 120 parent = new File (System.getProperty("user.dir")); 121 System.out.println("Exception during reading of Jalisto properties. Uses only default values"); 122 System.out.println("Work in system user.dir directory : " + String.valueOf(parent)); 123 } 124 125 JalistoUtils.truncateProperties(prop); 126 loadDataFiles(); 127 } catch (Exception e) { 128 throw new JalistoException( 129 "Error during initialisation of JalistoProperties, using " + databasePropertiesFilePath, e); 130 } 131 } 132 133 public String getName() { 134 return getProperty(NAME_KEY); 135 } 136 137 public int getInitialSize() { 138 return Integer.parseInt(getProperty(DATABASE_INITIAL_SIZE_KEY)); 139 } 140 141 public int getOidPageSize() { 142 return Integer.parseInt(getProperty(OBJECT_OID_PAGE_SIZE_KEY)); 143 } 144 145 public int getOidTableSize() { 146 return Integer.parseInt(getProperty(OIDTABLE_SIZE_KEY)); 147 } 148 149 public int getAdditionalSpace() { 150 return Integer.parseInt(getProperty(ADD_SPACE_KEY)); 151 } 152 153 public int getObjectCacheSize() { 154 return Integer.parseInt(getProperty(OBJECT_CACHE_SIZE_KEY)); 155 } 156 157 public int getPageCacheSize() { 158 return Integer.parseInt(getProperty(PAGE_CACHE_SIZE_KEY)); 159 } 160 161 public int getMBeanJmxPort() { 162 return Integer.parseInt(getProperty(MBEAN_JMX_SERVER_PORT_KEY)); 163 } 164 165 public double getCacheClearPourcent() { 166 return Double.parseDouble("0." + getProperty(CACHE_CLEAR_POURCENT_KEY)); 167 } 168 169 public short getClassPageSize() { 170 return Short.parseShort(getProperty(OBJECT_CLASS_PAGE_SIZE_KEY)); 171 } 172 173 public short getSystemPageSize() { 174 return Short.parseShort(getProperty(OBJECT_SYST_PAGE_SIZE_KEY)); 175 } 176 177 public String getImplementation() { 178 return getProperty(IMPLEMENTATION_KEY); 179 } 180 181 public String getInternalFactoryClass() { 182 return getProperty(INTERNAL_FACTORY_CLASS_KEY); 183 } 184 185 public boolean isMonoImplementation() { 186 return getProperty(IMPLEMENTATION_KEY).equalsIgnoreCase(MONO_IMPLEMENTATION); 187 } 188 189 public boolean isReadOnlyImplementation() { 190 return getProperty(IMPLEMENTATION_KEY).equalsIgnoreCase(READONLY_IMPLEMENTATION); 191 } 192 193 public boolean isMultiImplementation() { 194 return getProperty(IMPLEMENTATION_KEY).equalsIgnoreCase(MULTI_IMPLEMENTATION); 195 } 196 197 public boolean isKeepingInMemory() { 198 return getProperty(KEEP_IN_MEMORY_KEY).equalsIgnoreCase("yes"); 199 } 200 201 public boolean isCleanLog() { 202 return getProperty(CLEAN_LOG_KEY).equalsIgnoreCase("yes"); 203 } 204 205 public boolean allowsSpecialFunctionnalities() { 206 return getProperty(ALLOWS_SPECIAL_FUNCTIONNALITIES_KEY).equalsIgnoreCase("yes"); 207 } 208 209 public short getInstancePageSize() { 210 return Short.parseShort(getProperty(OBJECT_INST_PAGE_SIZE_KEY)); 211 } 212 213 public int getKeyLength() { 214 return Integer.parseInt(getProperty(INTERNAL_KEY_MAX_SIZE_KEY)); 215 } 216 217 public String getCommunicationFactoryClassName() { 218 return getProperty(COMMUNICATION_FACTORY_CLASS_KEY); 219 } 220 221 public Collection getKeys() { 222 return prop.keySet(); 223 } 224 225 public String getLogFileName() { 226 String logFileName = prop.getProperty(LOG_FILE_KEY); 227 if (logFileName.equals("")) { 228 logFileName = getName() + "-" + LOG_FILE; 229 } 230 return logFileName; 231 } 232 233 public String getProperty(String key) { 234 return prop.getProperty(key); 235 } 236 237 public void setProperty(String key, String property) { 238 prop.setProperty(key, property); 239 } 240 241 public Collection getTraceModuleNames() { 242 return getStrings(TRACE_KEY, ","); 243 } 244 245 public boolean isTraceEnable() { 246 String modules = getProperty(TRACE_KEY); 247 return ((modules != null) && (!modules.equals(""))); 248 } 249 250 public JalistoProperties getClone() { 251 return new JalistoPropertiesImpl(propertiesPath); 252 } 253 254 public void compareProperties(JalistoProperties oldProps) { 255 if (oldProps == null) { 256 return; 257 } 258 checkProperty(oldProps, NAME_KEY); 259 checkProperty(oldProps, OBJECT_INST_PAGE_SIZE_KEY); 260 checkProperty(oldProps, OBJECT_SYST_PAGE_SIZE_KEY); 261 checkProperty(oldProps, OBJECT_CLASS_PAGE_SIZE_KEY); 262 checkProperty(oldProps, OBJECT_OID_PAGE_SIZE_KEY); 263 checkProperty(oldProps, INTERNAL_KEY_MAX_SIZE_KEY); 264 checkProperty(oldProps, PHYSICAL_ACCESS_CLASS_KEY); 265 } 266 267 public void checkProperty(JalistoProperties oldProps, String property) { 268 if (!oldProps.getProperty(property).equalsIgnoreCase(prop.getProperty(property))) { 269 throw new JalistoException("Old Jalisto configuration property '" + property + "' with value '" + 270 oldProps.getProperty(property) + "'is not equal to new value '" + 271 prop.getProperty(property) + "'"); 272 } 273 } 274 275 278 279 public void loadDataFiles() throws IOException { 280 String files = getProperty(DATABASE_FILES_PATHS_KEY); 281 if (files.equals("")) { 282 files = getName() + ".jalisto"; 283 } 284 StringTokenizer tokenizer = new StringTokenizer(files, ",;"); 285 int size = tokenizer.countTokens(); 286 if (size == 2) { 287 throw new JalistoException("Not a valid number of path in property '" + DATABASE_FILES_PATHS_KEY + "'"); 288 } 289 pathes = new String [size]; 290 int i = 0; 291 while (tokenizer.hasMoreTokens()) { 292 String path = tokenizer.nextToken(); 293 File jalistoFile = new File (path); 294 295 if (jalistoFile.isAbsolute()) { 296 pathes[i] = jalistoFile.getCanonicalPath(); 297 } else { 298 File f = new File (parent, path); 299 pathes[i] = f.getCanonicalPath(); 300 } 301 i++; 302 } 303 } 304 305 public String getPropertiesPath() { 306 return propertiesPath; 307 } 308 309 public String [] getPathes() { 310 return pathes; 311 } 312 313 public String getDbFileFullName() { 314 return pathes[0]; 315 } 316 317 public String getOidDbFilePath() { 318 if (pathes.length > 1) { 319 return pathes[OID_INDEX]; 320 } else { 321 return pathes[0]; 322 } 323 } 324 325 public String getSystemDbFilePath() { 326 if (pathes.length > 1) { 327 return pathes[SYSTEM_INDEX]; 328 } else { 329 return pathes[0]; 330 } 331 } 332 333 public int getInstanceDbFileNumber() { 334 if (pathes.length > 1) { 335 return (pathes.length - 2); 336 } else { 337 return 1; 338 } 339 } 340 341 public String getInstanceDbFilePathAt(int index) { 342 if (pathes.length > 1) { 343 return pathes[index + 2]; 344 } else { 345 return pathes[0]; 346 } 347 } 348 349 350 351 public String getHost() { 352 throw new UnsupportedOperationException (); 353 } 354 355 public int getPort() { 356 throw new UnsupportedOperationException (); 357 } 358 359 public String getServerPropertiesPath() { 360 throw new UnsupportedOperationException (); 361 } 362 363 366 367 private Collection getStrings(Object key, String separator) { 368 String value = prop.getProperty((String ) key); 369 List result = new ArrayList(); 370 if (value != null) { 371 StringTokenizer tokenizer = new StringTokenizer(value, separator); 372 while (tokenizer.hasMoreTokens()) { 373 result.add(tokenizer.nextToken().trim()); 374 } 375 } 376 return result; 377 } 378 379 public String toString() { 380 StringBuffer sb = new StringBuffer (); 381 sb.append("JalistoProperties["); 382 Iterator keys = prop.keySet().iterator(); 383 while (keys.hasNext()) { 384 String key = (String ) keys.next(); 385 sb.append(key).append("=").append(prop.getProperty(key)); 386 if (keys.hasNext()) { 387 sb.append(","); 388 } 389 } 390 sb.append("]"); 391 return sb.toString(); 392 } 393 } 394 | Popular Tags |