1 30 31 32 package org.hsqldb; 33 34 import java.util.Vector ; 35 36 import org.hsqldb.lib.FileUtil; 37 import org.hsqldb.lib.HashMap; 38 import org.hsqldb.lib.HashSet; 39 import org.hsqldb.lib.HsqlTimer; 40 import org.hsqldb.lib.IntKeyHashMap; 41 import org.hsqldb.lib.Iterator; 42 import org.hsqldb.persist.HsqlProperties; 43 import org.hsqldb.store.ValuePool; 44 45 60 public class DatabaseManager { 61 62 64 65 private static int dbIDCounter; 66 67 68 static final HashMap memDatabaseMap = new HashMap(); 69 70 71 static final HashMap fileDatabaseMap = new HashMap(); 72 73 74 static final HashMap resDatabaseMap = new HashMap(); 75 76 77 static final IntKeyHashMap databaseIDMap = new IntKeyHashMap(); 78 79 82 public static Vector getDatabaseURIs() { 83 84 Vector v = new Vector (); 85 Iterator it = databaseIDMap.values().iterator(); 86 87 while (it.hasNext()) { 88 Database db = (Database) it.next(); 89 90 v.addElement(db.getURI()); 91 } 92 93 return v; 94 } 95 96 104 public static void closeDatabases(int mode) { 105 106 Iterator it = databaseIDMap.values().iterator(); 107 108 while (it.hasNext()) { 109 Database db = (Database) it.next(); 110 111 try { 112 db.close(mode); 113 } catch (HsqlException e) {} 114 } 115 } 116 117 120 static Session newSession(int dbID, String user, 121 String password) throws HsqlException { 122 123 Database db = (Database) databaseIDMap.get(dbID); 124 125 return db == null ? null 126 : db.connect(user, password); 127 } 128 129 132 133 public static Session newSession(String type, String path, String user, 135 String password, 136 HsqlProperties props) 137 throws HsqlException { 138 139 Database db = getDatabase(type, path, props); 140 141 return db == null ? null 142 : db.connect(user, password); 143 } 144 145 149 static Session getSession(int dbId, int sessionId) { 150 151 Database db = (Database) databaseIDMap.get(dbId); 152 153 return db == null ? null 154 : db.sessionManager.getSession(sessionId); 155 } 156 157 160 161 static int getDatabase(String type, String path, Server server, 163 HsqlProperties props) throws HsqlException { 164 165 Database db = getDatabase(type, path, props); 166 167 registerServer(server, db); 168 169 return db.databaseID; 170 } 171 172 186 187 static Database getDatabase(String type, String path, 189 HsqlProperties props) throws HsqlException { 190 191 Database db = getDatabaseObject(type, path, props); 198 199 synchronized (db) { 200 switch (db.getState()) { 201 202 case Database.DATABASE_ONLINE : 203 break; 204 205 case Database.DATABASE_SHUTDOWN : 206 207 if (lookupDatabaseObject(type, path) == null) { 210 addDatabaseObject(type, path, db); 211 } 212 213 db.open(); 214 break; 215 216 case Database.DATABASE_CLOSING : 223 224 case Database.DATABASE_OPENING : 229 throw Trace.error(Trace.DATABASE_ALREADY_IN_USE, 230 Trace.DatabaseManager_getDatabase); 231 } 232 } 233 234 return db; 235 } 236 237 private static synchronized Database getDatabaseObject(String type, 239 String path, HsqlProperties props) throws HsqlException { 240 241 Database db; 242 String key = path; 243 HashMap databaseMap; 244 245 if (type == DatabaseURL.S_FILE) { 246 databaseMap = fileDatabaseMap; 247 key = filePathToKey(path); 248 } else if (type == DatabaseURL.S_RES) { 249 databaseMap = resDatabaseMap; 250 } else if (type == DatabaseURL.S_MEM) { 251 databaseMap = memDatabaseMap; 252 } else { 253 throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION, 254 "DatabaseManager.getDatabaseObject"); 255 } 256 257 db = (Database) databaseMap.get(key); 258 259 if (db == null) { 260 db = new Database(type, path, type + key, props); 261 db.databaseID = dbIDCounter; 262 263 databaseIDMap.put(dbIDCounter, db); 264 265 dbIDCounter++; 266 267 databaseMap.put(key, db); 268 } 269 270 return db; 271 } 272 273 277 private static synchronized Database lookupDatabaseObject(String type, 278 String path) throws HsqlException { 279 280 Object key = path; 281 HashMap databaseMap; 282 283 if (type == DatabaseURL.S_FILE) { 284 databaseMap = fileDatabaseMap; 285 key = filePathToKey(path); 286 } else if (type == DatabaseURL.S_RES) { 287 databaseMap = resDatabaseMap; 288 } else if (type == DatabaseURL.S_MEM) { 289 databaseMap = memDatabaseMap; 290 } else { 291 throw (Trace.runtimeError( 292 Trace.UNSUPPORTED_INTERNAL_OPERATION, 293 "DatabaseManager.lookupDatabaseObject()")); 294 } 295 296 return (Database) databaseMap.get(key); 297 } 298 299 303 private static synchronized void addDatabaseObject(String type, 304 String path, Database db) throws HsqlException { 305 306 Object key = path; 307 HashMap databaseMap; 308 309 if (type == DatabaseURL.S_FILE) { 310 databaseMap = fileDatabaseMap; 311 key = filePathToKey(path); 312 } else if (type == DatabaseURL.S_RES) { 313 databaseMap = resDatabaseMap; 314 } else if (type == DatabaseURL.S_MEM) { 315 databaseMap = memDatabaseMap; 316 } else { 317 throw Trace.runtimeError(Trace.UNSUPPORTED_INTERNAL_OPERATION, 318 "DatabaseManager.addDatabaseObject()"); 319 } 320 321 databaseIDMap.put(db.databaseID, db); 322 databaseMap.put(key, db); 323 } 324 325 328 static void removeDatabase(Database database) { 329 330 int dbID = database.databaseID; 331 String type = database.getType(); 332 String path = database.getPath(); 333 Object key = path; 334 HashMap databaseMap; 335 336 notifyServers(database); 337 338 if (type == DatabaseURL.S_FILE) { 339 databaseMap = fileDatabaseMap; 340 341 try { 351 key = filePathToKey(path); 352 } catch (HsqlException e) { 353 Iterator it = databaseMap.keySet().iterator(); 354 Object foundKey = null; 355 356 while (it.hasNext()) { 357 Object currentKey = it.next(); 358 359 if (databaseMap.get(currentKey) == database) { 360 foundKey = currentKey; 361 362 break; 363 } 364 } 365 366 if (foundKey == null) { 367 368 } else { 370 key = foundKey; 371 } 372 } 373 } else if (type == DatabaseURL.S_RES) { 374 databaseMap = resDatabaseMap; 375 } else if (type == DatabaseURL.S_MEM) { 376 databaseMap = memDatabaseMap; 377 } else { 378 throw (Trace.runtimeError( 379 Trace.UNSUPPORTED_INTERNAL_OPERATION, 380 "DatabaseManager.lookupDatabaseObject()")); 381 } 382 383 databaseIDMap.remove(dbID); 384 databaseMap.remove(key); 385 386 if (databaseIDMap.isEmpty()) { 387 ValuePool.resetPool(); 388 } 389 } 390 391 398 static HashMap serverMap = new HashMap(); 399 400 403 static void deRegisterServer(Server server) { 404 serverMap.remove(server); 405 } 406 407 410 private static void deRegisterServer(Server server, Database db) { 411 412 Iterator it = serverMap.values().iterator(); 413 414 for (; it.hasNext(); ) { 415 HashSet databases = (HashSet) it.next(); 416 417 databases.remove(db); 418 419 if (databases.isEmpty()) { 420 it.remove(); 421 } 422 } 423 } 424 425 428 private static void registerServer(Server server, Database db) { 429 430 if (!serverMap.containsKey(server)) { 431 serverMap.put(server, new HashSet()); 432 } 433 434 HashSet databases = (HashSet) serverMap.get(server); 435 436 databases.add(db); 437 } 438 439 443 private static void notifyServers(Database db) { 444 445 Iterator it = serverMap.keySet().iterator(); 446 447 for (; it.hasNext(); ) { 448 Server server = (Server) it.next(); 449 HashSet databases = (HashSet) serverMap.get(server); 450 451 if (databases.contains(db)) { 452 server.notify(ServerConstants.SC_DATABASE_SHUTDOWN, 453 db.databaseID); 454 } 455 } 456 } 457 458 static boolean isServerDB(Database db) { 459 460 Iterator it = serverMap.keySet().iterator(); 461 462 for (; it.hasNext(); ) { 463 Server server = (Server) it.next(); 464 HashSet databases = (HashSet) serverMap.get(server); 465 466 if (databases.contains(db)) { 467 return true; 468 } 469 } 470 471 return false; 472 } 473 474 private static final HsqlTimer timer = new HsqlTimer(); 476 477 public static HsqlTimer getTimer() { 478 return timer; 479 } 480 481 private static String filePathToKey(String path) throws HsqlException { 484 485 try { 486 return FileUtil.canonicalPath(path); 487 } catch (Exception e) { 488 throw Trace.error(Trace.FILE_IO_ERROR, e.toString()); 489 } 490 } 491 } 492 | Popular Tags |