| 1 package com.daffodilwoods.daffodildb.server.serversystem; 2 3 import java.io.*; 4 import java.lang.ref.*; 5 import java.util.*; 6 import javax.xml.parsers.*; 7 8 import org.xml.sax.*; 9 import com.daffodilwoods.daffodildb.server.backup.*; 10 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 11 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.*; 12 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 13 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Utility; 14 import com.daffodilwoods.daffodildb.server.datasystem.mergesystem.*; 15 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.*; 16 import com.daffodilwoods.daffodildb.server.serversystem.datatriggersystem.*; 17 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem.*; 18 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.statementtriggersystem.*; 19 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.triggersystem.*; 20 import com.daffodilwoods.daffodildb.server.sessionsystem.*; 21 import com.daffodilwoods.daffodildb.server.sql99.*; 22 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 23 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 24 import com.daffodilwoods.daffodildb.utils.*; 25 import com.daffodilwoods.daffodildb.utils.byteconverter.*; 26 import com.daffodilwoods.daffodildb.utils.comparator.*; 27 import com.daffodilwoods.daffodildb.utils.field.*; 28 import com.daffodilwoods.daffodildb.utils.parser.*; 29 import com.daffodilwoods.daffodildb.utils.parser.Parser; 30 import com.daffodilwoods.database.general.*; 31 import com.daffodilwoods.database.resource.*; 32 import com.daffodilwoods.database.utility.*; 33 import java.lang.reflect.Method ; 34 import java.lang.reflect.*; 35 import java.nio.channels.FileChannel ; 36 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.DatabaseConstants; 37 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo.VersionHandlerFactory; 38 39 public class ServerSystem implements _ServerSystem { 40 private _DataDictionarySystem dataDictionarySystem; 41 private _DataTriggerSystem dataTriggerSystem; 42 43 private SessionSystem sessionSystem; 44 private _StatementTriggerSystem statementTriggerSystem; 45 private _DataSystem persistentSystem; 46 _DataSystem persistentIndexSystem; 47 int sessionId = 1; 48 private TreeMap serverSessionList = new TreeMap(); 49 public static final boolean FIRE_COMMIT_TIME_LISTENERS = false; 50 ArrayList scheduleList = new ArrayList(); 51 52 public String daffodilHome; 53 RandomAccessFile raf; 54 private Parser parser; 55 56 private _SequenceSystem sequenceSystem; 57 58 private String defaultUser = SystemTables.SYSTEM; 59 private String defaultPassword = SystemTables.SYSTEM; 60 private DXAResource xaResource; 61 private boolean isSaveMode; 62 _DataSystem cacheSystem; 63 private boolean backupUnderProcess = false; 64 65 private boolean isReadOnlyMode = false, isServerActive; 66 67 public CTbnfUzqfDpnqbsbups sameTypeComparator; 68 69 public ServerSystem() throws DException { 70 parser = new Parser(); 71 isServerActive = true; 72 init(isReadOnlyMode); 73 if (!_Server.ISONEDOLLARDB) { 74 StartScheduler startScheduler = new StartScheduler(this); 75 } 76 sameTypeComparator = new CTbnfUzqfDpnqbsbups(); 77 } 78 79 public ServerSystem(boolean isReadOnlyMode0) throws DException { 80 parser = new Parser(); 81 isServerActive = true; 82 83 isReadOnlyMode = isReadOnlyMode0; 84 init(isReadOnlyMode); 85 if (!_Server.ISONEDOLLARDB) { 86 StartScheduler startScheduler = new StartScheduler(this); 87 } 88 } 89 90 public ServerSystem(String path) throws DException { 91 daffodilHome = path; 92 isServerActive = true; 93 } 94 95 private void init(boolean isReadOnlyMode) throws DException { 96 if (System.getProperty(DAFFODILDB_HOME) == null) { 97 System.setProperty(DAFFODILDB_HOME, 98 System.getProperty("user.home") + File.separator + 99 "DaffodilDB"); 100 } 101 daffodilHome = System.getProperty(DAFFODILDB_HOME); 102 checkDatabaseInUse(); 103 parser.initialisePR(); 104 105 String memory = System.getProperty("memory"); 106 PrintHandler.print(" Memory passed in the server system = " + memory); 107 SystemFieldsCharacteristics sfc = new SystemFieldsCharacteristics(); 108 double mem = 0; 109 dataDictionarySystem = new DataDictionarySystem(null); 110 persistentSystem = new PersistentSystem(daffodilHome, isReadOnlyMode); 111 persistentIndexSystem = new IndexSystem(persistentSystem, isReadOnlyMode); 112 _DataSystem tempIndexSystem = null; 113 if (isReadOnlyMode) { 114 tempIndexSystem = new ReadOnlyTempIndexSystem(); 115 } else { 116 tempIndexSystem = new TempIndexSystem(daffodilHome); 117 } 118 cacheSystem = new MergeSystem(tempIndexSystem, persistentIndexSystem); 119 120 _ConstraintSystem constraintSystem = new ConstraintSystem( 121 dataDictionarySystem, false); 122 _ConstraintSystem deferrableConstraintSystem = new ConstraintSystem( 123 dataDictionarySystem, true); 124 _TriggerSystem triggerSystem = new TriggerSystem(dataDictionarySystem); 125 dataTriggerSystem = new DataTriggerSystem(constraintSystem, 126 deferrableConstraintSystem, 127 triggerSystem, 128 dataDictionarySystem); 129 statementTriggerSystem = new StatementTriggerSystem(dataDictionarySystem); 130 sessionSystem = new SessionSystem(dataDictionarySystem, cacheSystem, 131 deferrableConstraintSystem, 132 constraintSystem, isReadOnlyMode); 133 checkDatabase(); 134 } 135 136 private void checkDatabaseInUse() throws DException { 137 StringTokenizer tokenizer = new StringTokenizer(System.getProperty("java.version"), "."); 138 String javaVersion = null; 139 if (tokenizer.countTokens() > 1) { 140 javaVersion = tokenizer.nextToken(); 141 javaVersion += tokenizer.nextToken(); 142 } 143 if (Integer.parseInt(javaVersion) < 14) { 144 checkDatabaseInUseBelowJavaVersion1_4(); 145 return; 146 } 147 String logFile = daffodilHome + File.separator + "log.lg"; 148 149 File ff = new File(logFile); 150 try { 151 raf = new RandomAccessFile(ff, "rw"); 152 Method mt = null; 153 try { 154 mt = raf.getClass().getDeclaredMethod("getChannel", null); 155 } catch (SecurityException ex1) { 156 System.err.println(ex1); 157 } catch (NoSuchMethodException ex1) { 158 ex1.printStackTrace(); 159 System.err.println(ex1); 160 } 161 if (mt == null) { 162 checkDatabaseInUseBelowJavaVersion1_4(); 163 return; 164 } 165 Object channel = null; 166 Object lockObject = null; 167 try { 168 channel = mt.invoke(raf, null); 169 if (channel == null) { 170 ff = null; 171 raf = null; 172 checkDatabaseInUseBelowJavaVersion1_4(); 173 return; 174 } 175 mt = null; 176 mt = channel.getClass().getMethod("tryLock", null); 177 if (mt == null) { 178 ff = null; 179 raf = null; 180 checkDatabaseInUseBelowJavaVersion1_4(); 181 return; 182 } 183 lockObject = mt.invoke(channel, null); 184 } catch (InvocationTargetException ex2) { 185 ff = null; 186 raf = null; 187 checkDatabaseInUseBelowJavaVersion1_4(); 188 return; 189 } catch (IllegalArgumentException ex2) { 190 ff = null; 191 raf = null; 192 checkDatabaseInUseBelowJavaVersion1_4(); 193 return; 194 } catch (IllegalAccessException ex2) { 195 ff = null; 196 raf = null; 197 checkDatabaseInUseBelowJavaVersion1_4(); 198 return; 199 } catch (SecurityException ex1) { 200 ff = null; 201 raf = null; 202 checkDatabaseInUseBelowJavaVersion1_4(); 203 return; 204 } catch (NoSuchMethodException ex1) { 205 ff = null; 206 raf = null; 207 checkDatabaseInUseBelowJavaVersion1_4(); 208 return; 209 } 210 211 if (lockObject == null) 212 throw new DException("DSE5522", new Object [] {}); 213 } catch (FileNotFoundException ex) { 214 215 } catch (IOException ex) { 216 } 217 } 218 219 private void checkDatabase() throws DException { 220 String databaseURL = daffodilHome + File.separator + 221 DatabaseConstants.SYSTEMDATABASE; 222 File f = new File(databaseURL); 223 double version; 224 if (!f.exists()) { 225 version = VersionHandlerFactory.getLatestVersionHandler().getDbVersion(); 226 SystemTablesCreator.changeStructure(version); 227 createSystemDatabase(DatabaseConstants.SYSTEMDATABASE, 228 DatabaseConstants.INITIALFILESIZE, 229 DatabaseConstants.INCREMENTFACTOR, false, false); 230 } else { 231 PersistentDatabase pd = (PersistentDatabase) persistentSystem. 232 getDatabase(DatabaseConstants.SYSTEMDATABASE); 233 version = pd.getVersionHandler().getDbVersion(); 234 checkDatabaseIsCompleteBit(pd); 235 SystemTablesCreator.changeStructure(version); 236 } 237 } 238 239 public void setVerbose(boolean verbose) throws DException { 240 if (!isServerActive) 241 throw new DException("DSE2023", null); 242 PrintHandler.verbose = verbose; 243 } 244 245 public void setSaveMode(boolean saveMode0) throws DException { 246 if (!isServerActive) 247 throw new DException("DSE2023", null); 248 isSaveMode = saveMode0; 249 } 250 251 255 public _ServerSession getServerSession(String user, Object sessionConstant, 256 Properties sessionProperties, 257 String databaseURL) throws DException { 258 if (!isServerActive) 259 throw new DException("DSE2023", null); 260 sessionProperties = P.convertDaffodilDBKeyssToUpperCase(sessionProperties); 261 sessionProperties.setProperty(USER, user); 262 sessionProperties.setProperty(CREATE, "true"); 263 _UserSession userSession = sessionSystem.getUserSession(user, 264 sessionConstant, sessionProperties, databaseURL); 265 ServerSession serverSession = new ServerSession(databaseURL, userSession, this); 266 serverSession.setVerboseUser(sessionProperties.getProperty("verbose", "")); 267 return serverSession; 268 } 269 270 274 private boolean ensureDatabase(String databaseName, Properties prop) throws 275 DException { 276 277 String databaseURL = daffodilHome + File.separator + databaseName + 278 File.separator + databaseName + ".ddb"; 279 boolean isExists = false; 280 File f = new File(databaseURL); 281 try { 282 isExists = f.exists(); 283 if (!isExists) { 284 String create = prop.getProperty(CREATE); 285 if (create == null || create.equalsIgnoreCase("false")) 286 persistentSystem.getDatabase(databaseName); 287 createDatabase1(databaseName, prop); 288 289 } 290 } catch (DException ex) { 291 f.deleteOnExit(); 292 throw ex; 293 } 294 return isExists; 295 } 296 297 public _DataTriggerTable getDataTriggerTable(String databaseURL, 298 QualifiedIdentifier tableName) throws 299 DException { 300 if (!isServerActive) 301 throw new DException("DSE2023", null); 302 _DataTriggerDatabase dataTriggerDatabase = dataTriggerSystem. 303 getDataTrigerDatabase(databaseURL); 304 return dataTriggerDatabase.getDataTriggerTable(tableName); 305 } 306 307 public _DataTriggerTable getDataViewTriggerTable(String databaseURL, 308 309 QualifiedIdentifier tableName) throws DException { 310 if (!isServerActive) 311 throw new DException("DSE2023", null); 312 _DataTriggerDatabase dataTriggerDatabase = dataTriggerSystem. 313 getDataTrigerDatabase(databaseURL); 314 return dataTriggerDatabase.getDataViewTriggerTable(tableName); 315 } 316 317 public _StatementTriggerTable getStatementTriggerTable(String databaseURL, 318 QualifiedIdentifier tableName) throws DException { 319 if (!isServerActive) 320 throw new DException("DSE2023", null); 321 _StatementTriggerDatabase StatementTriggerDatabase = statementTriggerSystem. 322 getStatementTriggerDatabase(databaseURL); 323 return StatementTriggerDatabase.getStatementTriggerTable(tableName); 324 } 325 326 public _DataDictionarySystem getDataDictionarySystem() throws DException { 327 if (!isServerActive) 328 throw new DException("DSE2023", null); 329 return dataDictionarySystem; 330 } 331 332 public _Database getFileDatabase(String databaseURL) throws DException { 333 if (!isServerActive) 334 throw new DException("DSE2023", null); 335 return persistentSystem.getDatabase(databaseURL); 336 } 337 338 public _Connection getConnection(String databaseName, Properties prop) throws 339 DException { 340 if (!isServerActive) 341 throw new DException("DSE2023", null); 342 synchronized (this) { 343 if (backupUnderProcess) 344 throw new DException("DSE5577", null); 345 } 346 prop = P.convertDaffodilDBKeyssToUpperCase(prop); 347 databaseName = databaseName.toLowerCase(); 348 String databaseURL = databaseName; String user = prop.getProperty(USER, "Public"); 350 String password = prop.getProperty(PASSWORD, "PUBLIC"); 351 String verboseUser = prop.getProperty(VERBOSE, ""); 352 String schema = prop.getProperty(SCHEMA); 353 if (user == null || user.equals(SystemTables.SYSTEM)) 354 throw new DatabaseException("DSE1047", null); 355 ensureDatabase(databaseURL, prop); 356 if (!isValidUser(user, password, databaseName)) 357 throw new DException("DSE1308", new Object [] {user}); 358 String xid = prop.getProperty(XID); 359 _UserSession userSession = sessionSystem.getUserSession(user, xid, prop, 360 databaseURL); 361 Connection connection = isSaveMode ? 362 new SaveModeConnection(databaseURL, userSession, this) : isReadOnlyMode ? 363 new ReadOnlyConnection(databaseURL, userSession, this) 364 : new Connection(databaseURL, userSession, this); 365 connection.setVerboseUser(verboseUser); 366 if (! (user.equalsIgnoreCase(ServerSystem.browserUser) || 367 user.equalsIgnoreCase(SystemTables.SYSTEM))) { 368 connection.setCurrentCatalog("users"); 369 } 370 if (schema != null && 371 getDataDictionary(databaseName).isValidSchema(connection. 372 getCurrentCatalog(), schema)) { 373 connection.setCurrentSchema(schema); 374 } else { 375 378 } 379 String isVisible = prop.getProperty("isvisible"); 380 if (isVisible == null || (!isVisible.equalsIgnoreCase("false"))) 381 addServerSessionInList(databaseURL, connection); 382 if (isSaveMode) 383 ( (SaveModeConnection) connection).getSaveModeHandler() 384 .write(connection.getServerSession().getSessionId(), user, password); 385 if(connection.verbose ) 386 PrintHandler.print(" Connection got on database "+databaseName+" with user "+user, null, verboseUser); 387 return connection; 388 } 389 390 public _Connection get_Connection(String databaseURL, Properties prop) throws 391 DException { 392 if (!isServerActive) 393 throw new DException("DSE2023", null); 394 databaseURL = databaseURL.trim(); 395 databaseURL = databaseURL.toLowerCase(); 396 String user = prop.getProperty(USER); 397 String password = prop.getProperty(PASSWORD); 398 if (user == null ) 399 throw new DatabaseException("DSE1047", null); 400 String xid = prop.getProperty(XID); 401 _UserSession userSession = sessionSystem.getUserSession(user, xid, prop, 402 databaseURL); 403 _Connection connection = new Connection(databaseURL, userSession, this); 404 return connection; 405 } 406 407 private void addServerSessionInList(String databaseURL, 408 _Connection connection) throws DException { 409 ArrayList list = (ArrayList) serverSessionList.get(databaseURL); 410 if (list == null) { 411 list = new ArrayList(); 412 serverSessionList.put(databaseURL, list); 413 } 414 list.add(new WeakReference(connection)); 415 } 416 417 public void deleteTable(String databaseURL, QualifiedIdentifier tableName, 418 boolean dropTable) throws DException { 419 if (!isServerActive) 420 throw new DException("DSE2023", null); 421 _DataDictionary dataDictionary = dataDictionarySystem.getDataDictionary( 422 databaseURL); 423 dataDictionary.removeTable(tableName); 424 _DataTriggerDatabase dataTriggerDatabase = dataTriggerSystem. 425 getDataTrigerDatabase(databaseURL); 426 dataTriggerDatabase.removeTable(tableName); 427 _StatementTriggerDatabase statementTriggerDatabase = statementTriggerSystem. 428 getStatementTriggerDatabase(databaseURL); 429 statementTriggerDatabase.removeTable(tableName); 430 _SessionDatabase sessionDatabase = ( (SessionSystem) sessionSystem). 431 getSessionDatabase(databaseURL, null); 432 sessionDatabase.removeTable(tableName, dropTable); 433 removeEntriesFromServerSession(databaseURL, tableName); 434 } 435 436 public void refereshTable(String databaseURL, QualifiedIdentifier tableName, 437 boolean dropTable) throws DException { 438 if (!isServerActive) 439 throw new DException("DSE2023", null); 440 _DataDictionary dataDictionary = dataDictionarySystem.getDataDictionary( 441 databaseURL); 442 dataDictionary.removeTable(tableName); 443 _DataTriggerDatabase dataTriggerDatabase = dataTriggerSystem. 444 getDataTrigerDatabase(databaseURL); 445 dataTriggerDatabase.removeTable(tableName); 446 _StatementTriggerDatabase statementTriggerDatabase = statementTriggerSystem. 447 getStatementTriggerDatabase(databaseURL); 448 statementTriggerDatabase.removeTable(tableName); 449 _SessionDatabase sessionDatabase = ( (SessionSystem) sessionSystem). 450 getSessionDatabase(databaseURL, null); 451 removeEntriesFromServerSession(databaseURL, tableName); 452 } 453 454 public _User getUser(String userName, String password) throws DException { 455 if (!isServerActive) 456 throw new DException("DSE2023", null); 457 return new User(userName, this, password); 458 } 459 460 public ArrayList getAllDatabases(String userName) throws DException { 461 if (!isServerActive) 462 throw new DException("DSE2023", null); 463 ArrayList databases = ( (PersistentSystem) persistentSystem). 464 getAllDatabases(); 465 466 for (int i = databases.size(); i-- > 0; ) { 467 String databaseName = (String ) databases.get(i); 468 databaseName = databaseName.toLowerCase(); 469 try { 470 _DataDictionary dic = getDataDictionary(databaseName); 471 _PrivilegeCharacteristics pc = dic.getPrivilegeCharacteristics(userName, 472 _PrivilegeCharacteristics.AUTHORIZATION_USER); 473 if (pc == null) 474 databases.remove(i); 475 } catch (DException ex) { 476 databases.remove(i); 477 continue; 478 } 479 } 480 return databases; 481 } 482 483 public ArrayList getAllSessions(String databaseName) throws DException { 484 if (!isServerActive) 485 throw new DException("DSE2023", null); 486 ArrayList list = (ArrayList) serverSessionList.get(databaseName); 487 if (list == null || list.size() == 0) 488 return null; 489 490 synchronized (list) { 491 ArrayList sessions = new ArrayList(list.size()); 492 for (int i = list.size(); i-- > 0; ) { 493 WeakReference wk = (WeakReference) list.get(i); 494 Object obj = wk.get(); 495 if (obj != null) 496 sessions.add(obj); 497 } 498 return sessions; 499 } 500 } 501 502 private void removeClosedConnection(ArrayList list, 503 ServerSession serverSession) { 504 synchronized (list) { 505 for (int i = list.size(); i-- > 0; ) { 506 WeakReference wk = (WeakReference) list.get(i); 507 Object session = wk.get(); 508 if (session == null) 509 list.remove(i); 510 else 511 if (serverSession.equals(session)) 512 list.remove(i); 513 } 514 } 515 } 516 517 public void close(String databaseURL, ServerSession serverSession) throws DException { 518 if (!isServerActive) 519 throw new DException("DSE2023", null); 520 ArrayList list = (ArrayList) serverSessionList.get(databaseURL); 521 try { 522 if (list != null) { 523 removeClosedConnection(list, serverSession); 524 if (list.size() == 0) { 525 SessionDatabase sessionDatabase = (SessionDatabase) ( (SessionSystem) sessionSystem). 526 getSessionDatabase(databaseURL, null); 527 sessionDatabase.getSystemFieldsValue().updateLastTransactionId(); 528 } 529 530 } 531 } catch (DException ex) { 532 } 533 534 } 535 536 public _DataDictionary getDataDictionary(String databaseName) throws 537 DException { 538 if (!isServerActive) 539 throw new DException("DSE2023", null); 540 return sessionSystem.getSessionDatabase(databaseName, new Properties()). 541 getDataDictionary(); 542 } 543 544 public _Database getMergeDatabase(String databasePath) throws DException { 545 if (!isServerActive) 546 throw new DException("DSE2023", null); 547 return sessionSystem.getSessionDatabase(databasePath, new Properties()).getMergeDatabase(); 548 } 549 550 public _MemoryManager getMemoryManager() throws DException { 551 throw new UnsupportedOperationException ("Method getMemoryManager not supported"); 552 } 553 554 public String getHostName() throws DException { 555 if (!isServerActive) 556 throw new DException("DSE2023", null); 557 return "localHost"; 558 } 559 560 public int getPortNumber() throws DException { 561 if (!isServerActive) 562 throw new DException("DSE2023", null); 563 return -1; 564 } 565 566 public void checkUserPassword(String user, String password, 567 String databaseURL) throws DException { 568 569 throw new UnsupportedOperationException ("Method not supported"); 570 571 } 572 573 private void closeAllServerSession() throws DException { 574 Iterator it = serverSessionList.values().iterator(); 575 if (it.hasNext()) { 576 ArrayList list = (ArrayList) it.next(); 577 for (int i = list.size(); i-- > 0; ) { 578 WeakReference wk = (WeakReference) list.get(i); 579 _ServerSession session = (_ServerSession) wk.get(); 580 session.close(); 581 } 582 } 583 } 584 585 private void closeAllServerSession(String databaseName) throws DException { 586 ArrayList list = (ArrayList) serverSessionList.get(databaseName); 587 if (list == null) 588 return; 589 for (int i = list.size(); i-- > 0; ) { 590 WeakReference wk = (WeakReference) list.get(i); 591 _ServerSession session = (_ServerSession) wk.get(); 592 session.close(); 593 } 594 } 595 596 public void changeHome(String home) throws DException { 597 598 if (!isServerActive) 599 throw new DException("DSE2023", null); 600 closeServerSystem(); 601 isServerActive = true; 602 removeDatabasesFromCurrentPath(); 603 removeAllSchedules(); 604 daffodilHome = home; 605 ( (PersistentSystem) persistentSystem).changeHome(home); 606 if (!isReadOnlyMode) 607 ( (MergeSystem) cacheSystem).getMemorySystem().changeHome(home); 608 System.gc(); 609 System.runFinalization(); 610 checkDatabaseInUse(); 611 checkDatabase(); 612 synchronized (this) { 613 xaResource = null; 614 StartScheduler startScheduler = new StartScheduler(this); 615 } 616 } 617 618 public void createDatabase(String databaseName, Properties prop) throws 619 DException { 620 if (!isServerActive) 621 throw new DException("DSE2023", null); 622 623 createDatabase1(databaseName, prop); 624 dataDictionarySystem.deleteDatabase(databaseName); 625 dataTriggerSystem.deleteDatabase(databaseName); 626 statementTriggerSystem.deleteDatabase(databaseName); 627 serverSessionList.remove(databaseName); 628 sessionSystem.deleteDatabase(databaseName, true, false); 629 } 630 631 public void createDatabase1Old(String databaseName, Properties prop) throws 632 DException { 633 if (!isServerActive) 634 throw new DException("DSE2023", null); 635 if (databaseName == null || databaseName.equalsIgnoreCase(DatabaseConstants.SYSTEMDATABASE) || databaseName.equalsIgnoreCase(DatabaseConstants.TEMPDATABASE)) 636 throw new DException("DSE5576", new Object [] {databaseName}); 637 if (! (databaseName.length() <= DatabaseConstants.MAXDATABASENAMELENGTH)) 638 throw new DException("DSE2051", null); 639 640 prop = P.convertDaffodilDBKeyssToUpperCase(prop); 641 if (databaseName == null || databaseName.equalsIgnoreCase("")) 642 throw new DException("DSE5502", new Object [] {"[" + databaseName + "]"}); 643 databaseName = databaseName.toLowerCase(); 644 String user = prop.getProperty(USER); 645 String password = prop.getProperty(PASSWORD); 646 if (user == null && password == null) { 647 user = "PUBLIC"; 648 password = "PUBLIC"; 649 } 650 if (user == null ^ password == null) 651 throw new DException("DSE1208", null); if (user.equals("")) 653 throw new DException("DSE1208", null); user = P.makeDelimitedIdentifier(user); 655 656 DatabaseConnection connection = new DatabaseConnection(this); 657 ( (PersistentSystem) persistentSystem).createDatabase(databaseName, prop); 658 PersistentDatabase dDatabase = (PersistentDatabase) ( (PersistentSystem) 659 persistentSystem).getDatabase(databaseName); 660 boolean powerFileOption = dDatabase.getPowerFileOption(); 661 dDatabase.setWriteInPowerFile(false); 662 IndexDatabase indexDatabase = (IndexDatabase) persistentIndexSystem. 663 getDatabase(databaseName); 664 ArrayList tableDetails = SystemTableInformation.getTableDetails(); 665 ColumnInformation columnInformation = null; 666 for (int i = 0; i < tableDetails.size(); i++) { 667 Object [] values = (Object []) tableDetails.get(i); 668 QualifiedIdentifier tableName = (QualifiedIdentifier) values[0]; 669 columnInformation = (ColumnInformation) values[1]; 670 dDatabase.createTable(tableName, columnInformation); 671 createPermanentIndex(tableName, indexDatabase, i); 672 } 673 674 _ServerSession returnedConn = ( (DatabaseConnection) connection).executeAll( 675 databaseName); 676 password = password.length() == 0 ? "\"\"" : 677 P.makeDelimitedIdentifier(password); 678 String createUser = "Create user " + user + " password " + password; 679 returnedConn.execute(createUser, 0); 680 String defaultSchema = "Create schema users.users Authorization " + user; 681 returnedConn.execute(defaultSchema, 0); 682 if (user != null && user.equalsIgnoreCase("\"" + browserUser + "\"") == false) { 683 createUser = "Create user \"" + browserUser + "\" password daffodil"; 684 returnedConn.execute(createUser, 0); 685 } 686 if (user != null && user.equals("\"PUBLIC\"") == false) { 687 createUser = "Create user \"PUBLIC\" password \"PUBLIC\" "; 688 returnedConn.execute(createUser, 0); 689 } 690 createSystemViews(returnedConn); 691 createDummyTable(returnedConn); 692 returnedConn.commit(); 693 returnedConn.close(); 694 createDummyTable(databaseName, prop); 695 dDatabase.writeInFile(dDatabase.getDatabaseProperties(). 696 ISCOMPLETE_BIT_IN_DATABASE, 697 CCzufDpowfsufs.getBytes(new Byte (DatabaseConstants. 698 TRUE))); 699 _DataDictionary dd = getDataDictionary(databaseName); 700 dd.refereshConstraints(); 701 sessionSystem.refreshConstraintSystem(databaseName); 702 dataTriggerSystem.deleteDatabase(databaseName); 703 dDatabase.setWriteInPowerFile(powerFileOption); 704 } 705 706 public void createDatabase1(String databaseName, Properties prop) throws 707 DException { 708 if (!isServerActive) 709 throw new DException("DSE2023", null); 710 if (databaseName == null || databaseName.equalsIgnoreCase(DatabaseConstants.SYSTEMDATABASE) || databaseName.equalsIgnoreCase(DatabaseConstants.TEMPDATABASE)) 711 throw new DException("DSE5576", new Object [] {databaseName}); 712 if (! (databaseName.length() <= DatabaseConstants.MAXDATABASENAMELENGTH)) 713 throw new DException("DSE2051", null); 714 715 prop = P.convertDaffodilDBKeyssToUpperCase(prop); 716 if (databaseName == null || databaseName.equalsIgnoreCase("")) 717 throw new DException("DSE5502", new Object [] {"[" + databaseName + "]"}); 718 databaseName = databaseName.toLowerCase(); 719 String user = prop.getProperty(USER); 720 if (user != null && user.equalsIgnoreCase("PUBLIC")) 721 throw new DException("DSE8180", new Object [] {user}); 722 String password = prop.getProperty(PASSWORD); 723 if (user == null && password == null) { 724 user = "PUBLIC"; 725 password = "PUBLIC"; 726 } 727 if (user == null ^ password == null) 728 throw new DException("DSE1208", null); if (user.equals("")) 730 throw new DException("DSE1208", null); user = P.makeDelimitedIdentifier(user); 732 733 DatabaseConnection connection = new DatabaseConnection(this); 734 ( (PersistentSystem) persistentSystem).createDatabase(databaseName, prop); 735 PersistentDatabase dDatabase = (PersistentDatabase) ( (PersistentSystem) 736 persistentSystem).getDatabase(databaseName); 737 boolean powerFileOption = dDatabase.getPowerFileOption(); 738 dDatabase.setWriteInPowerFile(false); 739 IndexDatabase indexDatabase = (IndexDatabase) persistentIndexSystem. 740 getDatabase(databaseName); 741 ArrayList tableDetails = SystemTableInformation.getTableDetails(); 742 ColumnInformation columnInformation = null; 743 for (int i = 0; i < tableDetails.size(); i++) { 744 Object [] values = (Object []) tableDetails.get(i); 745 QualifiedIdentifier tableName = (QualifiedIdentifier) values[0]; 746 columnInformation = (ColumnInformation) values[1]; 747 dDatabase.createTable(tableName, columnInformation); 748 createPermanentIndex(tableName, indexDatabase, i); 749 } 750 751 SystemIndexInformation.createAllSystemTableIndexes(indexDatabase); 752 753 _ServerSession returnedConn = ( (DatabaseConnection) connection).executeAllNew( 754 databaseName); 755 756 password = password.length() == 0 ? "\"\"" : 757 P.makeDelimitedIdentifier(password); 758 String createUser = "Create user " + user + " password " + password; 759 returnedConn.execute(createUser, 0); 760 String defaultSchema = "Create schema users.users Authorization " + user; 761 returnedConn.execute(defaultSchema, 0); 762 if (user != null && user.equalsIgnoreCase("\"" + browserUser + "\"") == false) { 763 createUser = "Create user \"" + browserUser + "\" password daffodil"; 764 returnedConn.execute(createUser, 0); 765 } 766 if (user != null && user.equalsIgnoreCase("\"PUBLIC\"") == false) { 767 createUser = "Create user \"PUBLIC\" password \"PUBLIC\" "; 768 returnedConn.execute(createUser, 0); 769 } 770 createSystemViews(returnedConn); 771 createDummyTable(returnedConn); 772 returnedConn.commit(); 773 returnedConn.close(); 774 createDummyTable(databaseName, prop); 775 dDatabase.writeInFile(dDatabase.getDatabaseProperties(). 776 ISCOMPLETE_BIT_IN_DATABASE, 777 CCzufDpowfsufs.getBytes(new Byte (DatabaseConstants. 778 TRUE))); 779 _DataDictionary dd = getDataDictionary(databaseName); 780 dd.setSystemTableConstraint(true); 781 dd.refereshConstraints(); 782 sessionSystem.refreshConstraintSystem(databaseName); 783 dataTriggerSystem.deleteDatabase(databaseName); 784 dDatabase.setWriteInPowerFile(powerFileOption); 785 } 786 787 private void createDummyTable(String databaseName, Properties prop) throws DException { 788 String viewQuery = " create view users.users.dual as Select * from system.information_schema.dualSystemTable"; 789 String grantSelect = "grant SELECT on users.users.dual to PUBLIC"; 790 _Connection serverSession = getConnection(databaseName, prop); 791 serverSession.execute(viewQuery, 0, 0); 792 serverSession.execute(grantSelect, 0, 0); 793 serverSession.commit(); 794 serverSession.close(); 795 } 796 797 private void createDummyTable(_ServerSession serverSession) throws DException { 798 String tableQuery = " create table system.information_schema.dualSystemTable (col1 int) "; 799 String insertQuery = " insert into system.information_schema.dualSystemTable values(20976)"; 800 serverSession.execute(tableQuery, 0, 0); 801 serverSession.executeUpdate(insertQuery, 0); 802 } 803 804 private void createSystemViews(_ServerSession serverSession) throws 805 DException { 806 ArrayList viewsList = SystemTablesCreator.getSystemViews(); 807 for (int i = 0, size = viewsList.size(); i < size; i++) { 808 serverSession.execute( (String ) viewsList.get(i), 0); 809 } 810 } 811 812 public void createSystemDatabaseOld(String databaseName, String initialSize, 813 int incrementalFactor, 814 boolean unicodeSupport, 815 boolean multipleFileSupport) throws 816 DException { 817 if (!isServerActive) 818 throw new DException("DSE2023", null); 819 DatabaseConnection connection = new DatabaseConnection(this); 820 databaseName = databaseName.toLowerCase(); 821 PersistentDatabase dDatabase = (PersistentDatabase) ( (PersistentSystem) 822 persistentSystem).createSystemDatabase(databaseName, initialSize, 823 incrementalFactor, 824 unicodeSupport, 825 multipleFileSupport); 826 boolean powerFileOption = dDatabase.getPowerFileOption(); 827 dDatabase.setWriteInPowerFile(false); 828 IndexDatabase indexDatabase = (IndexDatabase) persistentIndexSystem. 829 getDatabase(databaseName); 830 ArrayList tableDetails = SystemTableInformation.getTableDetails(); 831 ColumnInformation columnInformation = null; 832 for (int i = 0; i < tableDetails.size(); i++) { 833 Object [] values = (Object []) tableDetails.get(i); 834 QualifiedIdentifier tableName = (QualifiedIdentifier) values[0]; 835 columnInformation = (ColumnInformation) values[1]; 836 dDatabase.createTable(tableName, columnInformation); 837 createPermanentIndex(tableName, indexDatabase, i); 838 } 839 840 _ServerSession returnedConn = ( (DatabaseConnection) connection).executeAll( 841 databaseName); 842 returnedConn.commit(); 843 returnedConn.close(); 844 dDatabase.writeInFile(dDatabase.getDatabaseProperties(). 845 ISCOMPLETE_BIT_IN_DATABASE, 846 CCzufDpowfsufs.getBytes(new Byte (DatabaseConstants. 847 TRUE))); 848 _DataDictionary dd = getDataDictionary(databaseName); 849 dd.refereshConstraints(); 850 sessionSystem.refreshConstraintSystem(databaseName); 851 dDatabase.setWriteInPowerFile(powerFileOption); 852 } 853 854 public void createSystemDatabase(String databaseName, String initialSize, 855 int incrementalFactor, 856 boolean unicodeSupport, 857 boolean multipleFileSupport) throws 858 DException { 859 if (!isServerActive) 860 throw new DException("DSE2023", null); 861 DatabaseConnection connection = new DatabaseConnection(this); 862 databaseName = databaseName.toLowerCase(); 863 PersistentDatabase dDatabase = (PersistentDatabase) ( (PersistentSystem) 864 persistentSystem).createSystemDatabase(databaseName, initialSize, 865 incrementalFactor, 866 unicodeSupport, 867 multipleFileSupport); 868 boolean powerFileOption = dDatabase.getPowerFileOption(); 869 dDatabase.setWriteInPowerFile(false); 870 IndexDatabase indexDatabase = (IndexDatabase) persistentIndexSystem. 871 getDatabase(databaseName); 872 ArrayList tableDetails = SystemTableInformation.getTableDetails(); 873 ColumnInformation columnInformation = null; 874 for (int i = 0; i < tableDetails.size(); i++) { 875 Object [] values = (Object []) tableDetails.get(i); 876 QualifiedIdentifier tableName = (QualifiedIdentifier) values[0]; 877 columnInformation = (ColumnInformation) values[1]; 878 dDatabase.createTable(tableName, columnInformation); 879 createPermanentIndex(tableName, indexDatabase, i); 880 } 881 882 SystemIndexInformation.createAllSystemTableIndexes(indexDatabase); 883 _ServerSession returnedConn = ( (DatabaseConnection) connection).executeAllNew( 884 databaseName); 885 dDatabase.writeInFile(dDatabase.getDatabaseProperties(). 886 ISCOMPLETE_BIT_IN_DATABASE, 887 CCzufDpowfsufs.getBytes(new Byte (DatabaseConstants. 888 TRUE))); 889 _DataDictionary dd = getDataDictionary(databaseName); 890 dd.setSystemTableConstraint(true); 891 892 dd.refereshConstraints(); 893 sessionSystem.refreshConstraintSystem(databaseName); 894 dDatabase.setWriteInPowerFile(powerFileOption); 895 } 896 897 private void removeDatabasesFromCurrentPath() throws DException { 898 String currentPath = daffodilHome; 899 File file = new File(currentPath); 900 String [] allDatabases = file.list(); 901 for (int i = 0; i < allDatabases.length; i++) { 902 String databaseName = allDatabases[i]; 903 dataDictionarySystem.deleteDatabase(databaseName); 904 dataTriggerSystem.deleteDatabase(databaseName); 905 statementTriggerSystem.deleteDatabase(databaseName); 906 serverSessionList.remove(databaseName); 907 sessionSystem.deleteDatabase(databaseName, true, false); 908 } 909 910 } 911 912 private void createPermanentIndex(QualifiedIdentifier tableName, 913 IndexDatabase indexDatabase, int i) throws 914 DException { 915 String indexName = "default_index" + i; 916 QualifiedIdentifier indexTableName = new QualifiedIdentifier(tableName. 917 catalog, tableName.schema, indexName); 918 Object [][] indexStructure = indexDatabase.getVersionHandler().getDefaultIndexStructure(); 919 IndexInformations indexInformationGetter = new IndexInformations(tableName, 920 indexName, -1, false, indexStructure, indexDatabase.getVersionHandler()); 921 indexDatabase.createPermanantIndexForSystemTable(tableName, indexName, 922 indexInformationGetter); 923 } 924 925 public void dropDatabase(String databaseName, String userName, 926 String userDatabaseName) throws com.daffodilwoods. 927 database.resource.DException { 928 if (!isServerActive) 929 throw new DException("DSE2023", null); 930 String databaseURL = daffodilHome + File.separator + databaseName + 931 File.separator + databaseName + ".ddb"; 932 databaseName = databaseName.trim().toLowerCase(); 933 databaseURL = P.getAbsolutePath(databaseURL); synchronized (databaseName) { 935 ArrayList list = (ArrayList) serverSessionList.get(databaseName); 936 if (list != null) { 937 synchronized (list) { 938 int noOfConnection = list.size(); 939 if (noOfConnection > 1) 940 throw new DException("DSE5519", new Object [] {databaseName}); 941 if (noOfConnection == 1) { 942 if (!databaseName.equalsIgnoreCase(userDatabaseName)) 943 throw new DException("DSE5519", new Object [] {databaseName}); 944 WeakReference wk = (WeakReference) list.get(0); 945 _ServerSession session = (_ServerSession) wk.get(); 946 if (session != null) { 947 if (!session.getUserSession().getUserName().equalsIgnoreCase( 948 userName)) 949 throw new DException("DSE5519", new Object [] {databaseName}); 950 } 951 } 952 } 953 } 954 dataDictionarySystem.deleteDatabase(databaseName); 955 dataTriggerSystem.deleteDatabase(databaseName); 956 statementTriggerSystem.deleteDatabase(databaseName); 957 closeAllServerSession(databaseName); 958 serverSessionList.remove(databaseName); 959 sessionSystem.deleteDatabase(databaseName, true, true); 960 } 961 removeAllSchedules(); 962 } 963 964 public void dropDatabase(String databaseName, String userName, String password, 965 String userDatabaseName) throws com.daffodilwoods. 966 database.resource.DException { 967 if (!isServerActive) 968 throw new DException("DSE2023", null); 969 try { 970 Properties prop = new Properties(); 971 prop.setProperty(_Server.USER, userName); 972 prop.setProperty(_Server.PASSWORD, password); 973 _Connection conn = getConnection(databaseName, prop); 974 checkIsValidUserBeforeDropDatabase(userName, conn); 975 conn.close(); 976 } catch (DException ex) { 977 if (ex.getDseCode().equalsIgnoreCase("DSE5539") || 978 ex.getDseCode().equalsIgnoreCase("DSE5544")) { 979 dropDatabase(databaseName, userName, userDatabaseName); 980 return; 981 } else if (ex.getDseCode().equalsIgnoreCase("DSE1210")) 982 throw new DException("DSE1207", null); 983 else 984 throw ex; 985 } 986 987 String databaseURL = daffodilHome + File.separator + databaseName + 988 File.separator + databaseName + ".ddb"; 989 databaseName = databaseName.trim().toLowerCase(); 990 databaseURL = P.getAbsolutePath(databaseURL); synchronized (databaseName) { 992 ArrayList list = (ArrayList) serverSessionList.get(databaseName); 993 if (list != null) { 994 synchronized (list) { 995 int noOfConnection = list.size(); 996 if (noOfConnection > 1) 997 throw new DException("DSE5519", new Object [] {databaseName}); 998 if (noOfConnection == 1) { 999 if (!databaseName.equalsIgnoreCase(userDatabaseName)) 1000 throw new DException("DSE5519", new Object [] {databaseName}); 1001 WeakReference wk = (WeakReference) list.get(0); 1002 _ServerSession session = (_ServerSession) wk.get(); 1003 if (session != null) { 1004 if (!session.getUserSession().getUserName().equalsIgnoreCase( 1005 userName)) 1006 throw new DException("DSE5519", new Object [] {databaseName}); 1007 } 1008 } 1009 } 1010 } 1011 dataDictionarySystem.deleteDatabase(databaseName); 1012 dataTriggerSystem.deleteDatabase(databaseName); 1013 statementTriggerSystem.deleteDatabase(databaseName); 1014 closeAllServerSession(databaseName); 1015 serverSessionList.remove(databaseName); 1016 sessionSystem.deleteDatabase(databaseName, true, true); 1017 } 1018 removeAllSchedules(); 1019 } 1020 1021 public _QueryReWriter getQueryReWriter(String query) throws DException { 1022 if (!isServerActive) 1023 throw new DException("DSE2023", null); 1024 return new QueryReWriter(query); 1025 } 1026 1027 1028 public _SequenceManager getSequenceManager(String databaseURL) throws 1029 DException { 1030 if (!isServerActive) 1031 throw new DException("DSE2023", null); 1032 if (sequenceSystem == null) 1033 sequenceSystem = new SequenceSystem(this, dataDictionarySystem); 1034 return sequenceSystem.getSequenceManager(databaseURL); 1035 } 1036 1037 private void removeEntriesFromServerSession(String databaseURL, 1038 QualifiedIdentifier tableName) throws 1039 DException { 1040 ArrayList list = (ArrayList) serverSessionList.get(databaseURL); 1041 if (list == null) 1042 return; 1043 ServerSession serverSession; 1044 synchronized (list) { 1045 for (int i = list.size(); i-- > 0; ) { 1046 WeakReference wk = (WeakReference) list.get(i); 1047 serverSession = (ServerSession) wk.get(); 1048 if (serverSession != null) 1049 serverSession.removeTable(tableName); 1050 } 1051 } 1052 } 1053 1054 public boolean isValidUser(String userName, String password) throws 1055 DException { 1056 if (!isServerActive) 1057 throw new DException("DSE2023", null); 1058 if (defaultUser.equalsIgnoreCase(userName) && 1059 defaultPassword.equalsIgnoreCase(password)) 1060 return true; 1061 1064 ArrayList databases = getAllDatabases(userName); 1065 boolean userfound_passwordNotfound = false; 1066 for (int i = 0; i < databases.size(); i++) { 1067 String databaseName = (String ) databases.get(i); 1068 _DataDictionary dd = sessionSystem.getSessionDatabase(databaseName, new Properties()).getDataDictionary(); 1069 _Iterator iter = dd.getIteratorForUserValidity(userName); 1070 if (iter.first()) { 1071 Object [] data = (Object []) iter.getColumnValues(); 1072 if (!password.equals( (String ) ( (FieldBase) data[SystemTablesFields. 1073 users_table_user_password]).getObject())) { 1074 userfound_passwordNotfound = true; 1075 } else 1076 return true; 1077 } 1078 } 1079 if (userfound_passwordNotfound) 1080 throw new DException("DSE1309", null); 1081 return (databases.size() == 0 && userName.equalsIgnoreCase(browserUser) && 1082 password.equalsIgnoreCase(browserUser)); 1083 } 1084 1085 public synchronized _DXAResource getDxaResource() throws DException { 1086 if (!isServerActive) 1087 throw new DException("DSE2023", null); 1088 return xaResource == null ? (xaResource = new DXAResource(this)) : 1089 xaResource; 1090 } 1091 1092 public boolean isActiveAuthorization(String Authorization, 1093 String databaseName) throws DException { 1094 if (!isServerActive) 1095 throw new DException("DSE2023", null); 1096 ArrayList list = (ArrayList) serverSessionList.get(databaseName); 1097 boolean found = false; 1098 if (list != null) { 1099 synchronized (list) { 1100 for (int i = list.size(); i-- > 0; ) { 1101 WeakReference wk = (WeakReference) list.get(i); 1102 Object session = wk.get(); 1103 if (session == null) { 1104 list.remove(i); 1105 } else { 1106 found = ( (_ServerSession) session). 1107 isEnabledAuthorizationIdentifier(Authorization, false); 1108 if (found) 1109 return found; 1110 } 1111 } 1112 } 1113 } 1114 return false; 1115 } 1116 1117 private void checkDatabaseIsCompleteBit(PersistentDatabase pd) throws DException { 1118 byte[] bytes = pd.readBytes(pd.getDatabaseProperties(). 1119 ISCOMPLETE_BIT_IN_DATABASE, 1); 1120 Boolean isComplete = CCzufDpowfsufs.getBoolean(bytes); 1121 if (!isComplete.booleanValue()) { 1122 throw new DException("DSE5543", null); 1123 } 1124 } 1125 1126 public boolean isValidUser(String userName, String password, 1127 String databaseName) throws DException { 1128 if (!isServerActive) 1129 throw new DException("DSE2023", null); 1130 if (defaultUser.equalsIgnoreCase(userName) && 1131 defaultPassword.equalsIgnoreCase(password)) 1132 return true; 1133 _DataDictionary dd = sessionSystem.getSessionDatabase(databaseName, new Properties()).getDataDictionary(); 1134 _Iterator iter = dd.getIteratorForUserValidity(userName); 1135 if (iter.first()) { 1136 Object [] data = (Object []) iter.getColumnValues(); 1137 if (!password.equals( (String ) ( (FieldBase) data[SystemTablesFields. 1138 users_table_user_password]).getObject())) 1139 throw new DException("DSE1309", null); 1140 return true; 1141 } 1142 return (userName.equalsIgnoreCase(browserUser) && 1143 password.equalsIgnoreCase(browserUser)); 1144 } 1145 1146 public void closeServerSystem() throws DException { 1147 if (!isServerActive) 1148 throw new DException("DSE2023", null); 1149 if (raf != null) { 1150 try { 1151 raf.close(); 1152 raf = null; 1153 } catch (IOException ex) { 1154 } 1155 } 1156 ( (MergeSystem) cacheSystem).closeAllDatabases(); 1157 removeAllSchedules(); 1158 closeAllServerSession(); 1159 isServerActive = false; 1160 } 1161 1162 public synchronized void getInconsistentOnlineBackup(String destination, 1163 String databaseNameSource, 1164 String databaseNameDestination, 1165 boolean overwrite) throws DException { 1166 if (!isServerActive) 1167 throw new DException("DSE2023", null); 1168 if (_Server.ISONEDOLLARDB) 1169 throw new DException("DSE2054", new Object [] {" Online Backup "}); 1170 if (! ( (MergeDatabase) getMergeDatabase(databaseNameSource)).getVersionHandler().isBackUpSupported()) { 1171 throw new DException("DSE5590", new Object [] {"Online Backup"}); 1172 } 1173 if (daffodilHome.equalsIgnoreCase(destination)) 1174 throw new DException("DSE5574", null); 1175 1176 OnlineBackup onlineBackup = new OnlineBackup(this); 1177 onlineBackup.onlineBackup(destination, databaseNameSource, 1178 databaseNameDestination, overwrite); 1179 } 1180 1181 public synchronized void offlineBackup(String userName, String password, String destination, String databaseNameSource, 1182 String databaseNameDestination, boolean overwrite) throws DException { 1183 1184 if (!isServerActive) 1185 throw new DException("DSE2023", null); 1186 if (_Server.ISONEDOLLARDB) 1187 throw new DException("DSE2054", new Object [] {" Offline Backup "}); 1188 try { 1189 if (! ( (MergeDatabase) getMergeDatabase(databaseNameSource)).getVersionHandler().isBackUpSupported()) { 1190 throw new DException("DSE5590", new Object [] {"Offline Backup"}); 1191 } 1192 checkUserValidity(databaseNameSource, userName, password); 1193 checkConnections(databaseNameSource); 1194 backupUnderProcess = true; 1195 OfflineBackup backUp = new OfflineBackup(this); 1196 backUp.offlineBackup(destination, databaseNameSource, 1197 databaseNameDestination, overwrite); 1198 } finally { 1199 backupUnderProcess = false; 1200 } 1201 } 1202 1203 public synchronized void restore(String userName, String password, String sourcePath, 1204 String databaseNameSource, 1205 String databaseNameDestination, 1206 boolean overwrite) throws DException { 1207 if (!isServerActive) 1208 throw new DException("DSE2023", null); 1209 if (_Server.ISONEDOLLARDB) 1210 throw new DException("DSE2054", new Object [] {" Restore Backup "}); 1211 try { 1212 if (! ( (MergeDatabase) getMergeDatabase(DatabaseConstants.SYSTEMDATABASE)).getVersionHandler().isBackUpSupported()) { 1213 throw new DException("DSE5590", new Object [] {"Restore Backup"}); 1214 } 1215 try { 1216 checkUserValidity(databaseNameDestination, userName, password); 1217 checkConnections(databaseNameDestination); 1218 } catch (DException ex) { 1219 if (!ex.getDseCode().equalsIgnoreCase("DSE316")) 1220 throw ex; 1221 } 1222 1223 backupUnderProcess = true; 1224 OfflineBackup backUp = new OfflineBackup(this); 1225 backUp.restoreBackup(sourcePath, databaseNameSource, 1226 databaseNameDestination, overwrite); 1227 } finally { 1228 backupUnderProcess = false; 1229 } 1230 1231 } 1232 1233 1245 1246 public void addSchedule(String databaseName, String scheduleName, 1247 String timeIntervalForBackup, String backupType, 1248 String backupPath, String databaseNameForBackup, 1249 long lastBackupTime) throws DException { 1250 if (!isServerActive) 1251 throw new DException("DSE2023", null); 1252 if (_Server.ISONEDOLLARDB) 1253 throw new DException("DSE2054", new Object [] {" Add Schedule "}); 1254 PersistentDatabase userDatabase = null; 1255 try { 1256 userDatabase = (PersistentDatabase) ( (IndexDatabase) ( ( 1257 MergeDatabase) getMergeDatabase(databaseName)).getFileDatabase()). 1258 getUnderLyingDatabase(); 1259 } catch (DException ex) { 1260 if (ex.getDseCode().equalsIgnoreCase("DSE316")) 1261 throw new DException("DSE5562", new Object [] {databaseName}); 1262 } 1263 if (!userDatabase.getVersionHandler().isBackUpSupported()) { 1264 throw new DException("DSE5590", new Object [] {"Adding Schedule"}); 1265 } 1266 _DatabaseUser user = null; 1267 1268 PersistentDatabase systemDatabase = (PersistentDatabase) ( (IndexDatabase) ( ( 1269 MergeDatabase) getMergeDatabase(DatabaseConstants.SYSTEMDATABASE)). 1270 getFileDatabase()).getUnderLyingDatabase(); 1271 _Table table = null; 1272 try { 1273 table = systemDatabase.getTable(SystemTables.SCHEDULEINFO); 1274 } catch (DException ex1) { 1275 if (ex1.getDseCode().equalsIgnoreCase("DSE959")) { 1276 throw new DException("DSE5578", null); 1277 } 1278 } 1279 DatabaseUserTableIterator scheduleInfoTableIteartor = ( 1280 DatabaseUserTableIterator) ( (_DataTable) table).getIterator(); 1281 BufferRange bytesOfScheduleName = new BufferRange(CCzufDpowfsufs.getBytes( 1282 scheduleName, scheduleName.length(), false)); 1283 CTusjohJoTfotjujwfDpnqbsbups stringComparator = new 1284 CTusjohJoTfotjujwfDpnqbsbups(); 1285 if (scheduleInfoTableIteartor.first()) { 1286 do { 1287 BufferRange bytesGot = (BufferRange) scheduleInfoTableIteartor. 1288 getColumnValues(1); 1289 if (stringComparator.compare(bytesGot, bytesOfScheduleName) == 0) { 1290 throw new DException("DSE5561", new Object [] {scheduleName}); 1291 } 1292 } while (scheduleInfoTableIteartor.next()); 1293 } 1294 1295 _TableCharacteristics tableCharacteristicsScheduleInfo = table. 1296 getTableCharacteristics(); 1297 ArrayList list = new ArrayList(); 1298 list.add(SystemTables.SCHEDULEINFO); 1299 user = systemDatabase.getDatabaseUser(list); 1300 try { 1301 long timeIntervalBackup = getTimeIntervalForBackupInMillis( 1302 timeIntervalForBackup); 1303 lastBackupTime = lastBackupTime - timeIntervalBackup; 1304 Object [] valueForScheduleInfo = new Object [] { 1305 databaseName, scheduleName, new Long (timeIntervalBackup), backupType, 1306 new Long (lastBackupTime), backupPath, databaseNameForBackup}; 1307 BufferRange[] bytesForScheduleInfo = Utility.convertIntoBufferRange( 1308 tableCharacteristicsScheduleInfo.getColumnTypes(), 1309 valueForScheduleInfo, tableCharacteristicsScheduleInfo.getCollator()); 1310 ( (_UserTableOperations) scheduleInfoTableIteartor).insert(user, 1311 bytesForScheduleInfo); 1312 user.writeToFile(); 1313 Scheduler schedule = new Scheduler(this, databaseName, scheduleName, 1314 lastBackupTime, timeIntervalBackup, 1315 backupType, backupPath, 1316 databaseNameForBackup); 1317 Thread t = new Thread (schedule); 1318 addScheduleInScheduleList(schedule); 1319 t.start(); 1320 } finally { 1321 user.releaseCluster(); 1322 } 1323 1324 } 1325 1326 1334 public void dropSchedule(String scheduleName) throws DException { 1335 _DatabaseUser user = null; 1336 if (!isServerActive) 1337 throw new DException("DSE2023", null); 1338 if (_Server.ISONEDOLLARDB) 1339 throw new DException("DSE2054", new Object [] {" Add Schedule "}); 1340 try { 1341 removeScheduleFromScheduleList(scheduleName); PersistentDatabase systemDatabase = (PersistentDatabase) ( (IndexDatabase) ( ( 1343 MergeDatabase) getMergeDatabase(DatabaseConstants.SYSTEMDATABASE)). 1344 getFileDatabase()).getUnderLyingDatabase(); 1345 if (!systemDatabase.getVersionHandler().isBackUpSupported()) { 1346 throw new DException("DSE5590", new Object [] {"Drop Schedule"}); 1347 } 1348 _Table table = systemDatabase.getTable(SystemTables.SCHEDULEINFO); 1349 DatabaseUserTableIterator scheduleInfoTableIteartor = ( 1350 DatabaseUserTableIterator) ( (_DataTable) table).getIterator(); 1351 _TableCharacteristics tableCharacteristicsScheduleInfo = table. 1352 getTableCharacteristics(); 1353 ArrayList list = new ArrayList(); 1354 list.add(SystemTables.SCHEDULEINFO); 1355 user = systemDatabase.getDatabaseUser(list); 1356 BufferRange bytesOfScheduleName = new BufferRange(CCzufDpowfsufs.getBytes( 1357 scheduleName, scheduleName.length(), false)); 1358 CTusjohJoTfotjujwfDpnqbsbups stringComparator = new 1359 CTusjohJoTfotjujwfDpnqbsbups(); 1360 if (scheduleInfoTableIteartor.first()) { 1361 do { 1362 BufferRange bytesGot = (BufferRange) scheduleInfoTableIteartor. 1363 getColumnValues(1); 1364 if (stringComparator.compare(bytesGot, bytesOfScheduleName) == 0) { 1365 ( (_UserTableOperations) scheduleInfoTableIteartor).delete(user); 1366 break; 1367 } 1368 } while (scheduleInfoTableIteartor.next()); 1369 } 1370 user.writeToFile(); 1371 } catch (DException ex) { 1372 if (!ex.getDseCode().equalsIgnoreCase("DSE959")) 1373 throw ex; 1374 } finally { 1375 user.releaseCluster(); 1376 } 1377 } 1378 1379 private long getTimeIntervalForBackupInMillis(String time) { 1380 if (time.indexOf("YEARS") != -1) { 1381 long size = Long.parseLong(time.substring(0, time.indexOf("YEARS")).trim()); 1382 return size * 365 * 24 * 60 * 60 * 1000; 1383 } 1384 if (time.indexOf("MONTHS") != -1) { 1385 long size = Long.parseLong(time.substring(0, time.indexOf("MONTHS")).trim()); 1386 return size * 30 * 24 * 60 * 60 * 1000; 1387 } 1388 if (time.indexOf("WEEKS") != -1) { 1389 long size = Long.parseLong(time.substring(0, time.indexOf("WEEKS")).trim()); 1390 return size * 7 * 24 * 60 * 60 * 1000; 1391 } 1392 if (time.indexOf("DAYS") != -1) { 1393 long size = Long.parseLong(time.substring(0, time.indexOf("DAYS")).trim()); 1394 return size * 24 * 60 * 60 * 1000; 1395 } 1396 if (time.indexOf("HOURS") != -1) { 1397 long size = Long.parseLong(time.substring(0, time.indexOf("HOURS")).trim()); 1398 return size * 60 * 60 * 1000; 1399 } 1400 if (time.indexOf("MINUTES") != -1) { 1401 long size = Long.parseLong(time.substring(0, time.indexOf("MINUTES")). 1402 trim()); 1403 return size * 60 * 1000; 1404 } 1405 if (time.indexOf("SECONDS") != -1) { 1406 long size = Long.parseLong(time.substring(0, time.indexOf("SECONDS")). 1407 trim()); 1408 return size * 1000; 1409 } 1410 return 0; 1411 } 1412 1413 public void addScheduleInScheduleList(Object obj) throws DException { 1414 if (!isServerActive) 1415 throw new DException("DSE2023", null); 1416 if (_Server.ISONEDOLLARDB) 1417 throw new DException("DSE2054", new Object [] {" Add Schedule "}); 1418 scheduleList.add(obj); 1419 } 1420 1421 1427 private void removeScheduleFromScheduleList(String scheduleName) { 1428 for (int i = 0; i < scheduleList.size(); i++) { 1429 Scheduler scheduleEntry = (Scheduler) scheduleList.get(i); 1430 1431 if (scheduleEntry.getScheduleName().equalsIgnoreCase(scheduleName)) { 1432 ( (Scheduler) scheduleList.get(i)).stopThread(); 1433 scheduleList.remove(i); 1434 break; 1435 } 1436 } 1437 } 1438 1439 private void removeAllSchedules() { 1440 for (int i = 0; i < scheduleList.size(); i++) { 1441 ( (Scheduler) scheduleList.get(i)).stopThread(); 1442 scheduleList.remove(i); 1443 } 1444 } 1445 1446 private void checkUserValidity(String databaseName, String userName, 1447 String password) throws DException { 1448 try { 1449 Properties prop = new Properties(); 1450 prop.setProperty(_Server.USER, userName); 1451 prop.setProperty(_Server.PASSWORD, password); 1452 prop.setProperty(_Server.CREATE, "true"); 1453 _Connection conn = getConnection(databaseName, prop); 1454 conn.close(); 1455 } catch (DException ex) { 1456 if (ex.getDseCode().equalsIgnoreCase("DSE1210")) 1457 throw new DException("DSE5575", null); 1458 else 1459 throw ex; 1460 } 1461 } 1462 1463 private void checkConnections(String databaseName) throws DException { 1464 synchronized (databaseName) { 1465 ArrayList list = (ArrayList) serverSessionList.get(databaseName); 1466 int noOfConnection = 0; 1467 if (list != null) { 1468 synchronized (list) { 1469 noOfConnection = list.size(); 1470 } 1471 } 1472 1473 if (noOfConnection == 1) { 1474 WeakReference wk = (WeakReference) list.get(0); 1475 _ServerSession session = (_ServerSession) wk.get(); 1476 if (session.getUserSession().getUserName().equalsIgnoreCase("_Server")) { 1477 session.close(); 1478 } else 1479 throw new DException("DSE5519", new Object [] {databaseName}); 1480 } else if (noOfConnection == 0) { 1481 PersistentDatabase pd = (PersistentDatabase) persistentSystem. 1482 getDatabase(databaseName); 1483 dataDictionarySystem.deleteDatabase(databaseName); 1484 dataTriggerSystem.deleteDatabase(databaseName); 1485 statementTriggerSystem.deleteDatabase(databaseName); 1486 serverSessionList.remove(databaseName); 1487 sessionSystem.deleteDatabase(databaseName, true, false); 1488 1489 } 1490 1491 else 1492 throw new DException("DSE5519", new Object [] {databaseName}); 1493 } 1494 } 1495 1496 public ArrayList getScheduleForDatabase(String databaseName) throws DException { 1497 if (!isServerActive) 1498 throw new DException("DSE2023", null); 1499 if (_Server.ISONEDOLLARDB) 1500 throw new DException("DSE2054", new Object [] {" Get Schedule "}); 1501 PersistentDatabase systemDatabase = (PersistentDatabase) ( (IndexDatabase) ( ( 1502 MergeDatabase) getMergeDatabase(DatabaseConstants.SYSTEMDATABASE)). 1503 getFileDatabase()).getUnderLyingDatabase(); 1504 if (!systemDatabase.getVersionHandler().isBackUpSupported()) { 1505 throw new DException("DSE5590", new Object [] {"Scheduling"}); 1506 } 1507 ArrayList scheduleNames = new ArrayList(); 1508 _DatabaseUser user = null; 1509 try { 1510 _Table table = systemDatabase.getTable(SystemTables.SCHEDULEINFO); 1511 DatabaseUserTableIterator scheduleInfoTableIteartor = ( 1512 DatabaseUserTableIterator) ( (_DataTable) table).getIterator(); 1513 _TableCharacteristics tableCharacteristicsScheduleInfo = table.getTableCharacteristics(); 1514 ArrayList list = new ArrayList(); 1515 list.add(SystemTables.SCHEDULEINFO); 1516 1517 user = systemDatabase.getDatabaseUser(list); 1518 BufferRange bytesOfDatabaseName = new BufferRange(CCzufDpowfsufs. 1519 getBytes( 1520 databaseName, databaseName.length(), false)); 1521 CTusjohJoTfotjujwfDpnqbsbups stringComparator = new 1522 CTusjohJoTfotjujwfDpnqbsbups(); 1523 if (scheduleInfoTableIteartor.first()) { 1524 do { 1525 BufferRange bytesGot = (BufferRange) scheduleInfoTableIteartor. 1526 getColumnValues(0); 1527 if (stringComparator.compare(bytesGot, bytesOfDatabaseName) == 0) { 1528 scheduleNames.add(CCzufDpowfsufs.getString( ( (BufferRange) scheduleInfoTableIteartor.getColumnValues(1)).getBytes())); 1529 } 1530 } while (scheduleInfoTableIteartor.next()); 1531 } 1532 user.releaseCluster(); 1533 } catch (DException ex) { 1534 if (!ex.getDseCode().equalsIgnoreCase("DSE959")) 1535 throw ex; 1536 } finally { 1537 user.releaseCluster(); 1538 } 1539 return scheduleNames; 1540 } 1541 1542 1549 public void restoreSaveMode(String oldDBName, String newdbName) throws DException { 1550 if (!isServerActive) 1551 throw new DException("DSE2023", null); 1552 String oldDBHome = System.getProperty(ServerSystem.DAFFODILDB_HOME) 1553 + File.separator + oldDBName + File.separator; 1554 String xmlFilePath = oldDBHome + SaveModeConstants.XML_FILENAME; 1555 appendRootTag(xmlFilePath); 1556 restoreStatements(oldDBHome, newdbName); 1557 createLogFile(xmlFilePath); 1558 } 1559 1560 private void appendRootTag(String xmlFilePath) throws DException { 1561 try { 1562 FileOutputStream fos = new FileOutputStream(xmlFilePath, true); 1563 OutputStreamWriter osw = new OutputStreamWriter(fos); 1564 osw.write("\n</transactions>"); 1565 osw.close(); 1566 fos.close(); 1567 } catch (IOException ex) { 1568 throw new DException("DSE0", new Object [] {"Save Mode File Close Error " + ex.getMessage()}); 1569 } 1570 } 1571 1572 private void restoreStatements(String oldDBHome, 1573 String newDBName0) throws DException { 1574 try { 1575 SAXParser parser = SAXParserFactory 1576 .newInstance().newSAXParser(); 1577 XMLReader reader = parser.getXMLReader(); 1578 ContentHandler handler = new OperationHandler(this, newDBName0, oldDBHome); 1579 reader.setContentHandler(handler); 1580 reader.parse(oldDBHome + SaveModeConstants.XML_FILENAME); 1581 } catch (SAXException ex1) { 1582 1583 ex1.printStackTrace(); 1584 throw new DException("DSE0", new Object [] {" Restore Operation Error "}); 1585 } catch (Exception ex1) { 1586 ex1.printStackTrace(); 1587 throw new DException("DSE0", new Object [] {" Restore Operation Error "}); 1588 } 1589 } 1590 1591 private void createLogFile(String xmlFilePath) throws DException { 1592 int logFileIndex = 0; 1593 while ( (new File(xmlFilePath.substring(0, xmlFilePath.indexOf(SaveModeConstants.XML_FILENAME 1594 )) + SaveModeConstants.LOG_FILEPREFIX + ++logFileIndex + ".xml")).exists()); 1595 if (!new File(xmlFilePath).renameTo(new File(xmlFilePath.substring(0, 1596 xmlFilePath.indexOf(SaveModeConstants.XML_FILENAME 1597 )) + SaveModeConstants.LOG_FILEPREFIX + logFileIndex + ".xml"))) 1598 throw new DException("DSE0", new Object [] {" Log creation error "}); 1599 } 1600 1601 public _DataSystem getPersistentSystem() throws DException { 1602 if (!isServerActive) 1603 throw new DException("DSE2023", null); 1604 return persistentSystem; 1605 } 1606 1607 private void createIndexForDirtyTransactions(QualifiedIdentifier tableName, 1608 IndexDatabase indexDatabase) throws 1609 DException { 1610 String indexName = "dirty_distributions_index"; 1611 QualifiedIdentifier indexTableName = new QualifiedIdentifier(tableName. 1612 catalog, tableName.schema, indexName); 1613 Object [][] indexStructure = getDirtyDistributionsIndexStructure(); 1614 IndexInformations indexInformationGetter = new IndexInformations(tableName, 1615 indexName, -1, false, indexStructure, indexDatabase.getVersionHandler()); 1616 indexDatabase.createPermanantIndexForSystemTable(tableName, indexName, 1617 indexInformationGetter); 1618 } 1619 1620 private Object [][] getDirtyDistributionsIndexStructure() { 1621 return new Object [][] { { 1622 SystemFields.systemFields[SystemFields.sessionId], Boolean.TRUE, 1623 new Integer (0), new Integer (0)} 1624 }; 1625 } 1626 1627 public _DataTriggerDatabase getDataTriggerDatabase(String url) throws DException { 1628 if (!isServerActive) 1629 throw new DException("DSE2023", null); 1630 return dataTriggerSystem.getDataTrigerDatabase(url); 1631 } 1632 1633 public void refreshTriggers(String databaseName, QualifiedIdentifier tableName) throws DException { 1634 if (!isServerActive) 1635 throw new DException("DSE2023", null); 1636 ArrayList list = (ArrayList) serverSessionList.get(databaseName); 1637 if (list != null) { 1638 synchronized (list) { 1639 for (int i = list.size(); i-- > 0; ) { 1640 WeakReference wk = (WeakReference) list.get(i); 1641 Object session = wk.get(); 1642 if (session != null) { 1643 ( (_ServerSession) session).refreshTriggerInfo(tableName); 1644 } 1645 } 1646 } 1647 } 1648 } 1649 1650 public String getDaffodilHome() throws DException { 1651 if (!isServerActive) 1652 throw new DException("DSE2023", null); 1653 return daffodilHome; 1654 } 1655 1656 public boolean getReadOnlyMode() throws DException { 1657 if (!isServerActive) 1658 throw new DException("DSE2023", null); 1659 return isReadOnlyMode; 1660 } 1661 1662 public void refreshConstraintTable(String databaseURL, QualifiedIdentifier tableName) throws DException { 1663 if (!isServerActive) 1664 throw new DException("DSE2023", null); 1665 _DataTriggerDatabase dataTriggerDatabase = getDataTriggerDatabase(databaseURL); 1666 dataTriggerDatabase.refreshConstraint(tableName); 1667 } 1668 1669 private void checkDatabaseInUseBelowJavaVersion1_4() throws DException { 1670 String logFile = daffodilHome + File.separator + "log.lg"; 1671 File ff = new File(logFile); 1672 boolean isDelete = false; 1673 1674 File dirExist = new File(daffodilHome); 1675 if (!dirExist.exists()) { 1676 if (!dirExist.mkdirs()) { 1677 throw new DException("DSE5523", new Object [] {}); 1678 } 1679 } 1680 1681 if (ff.exists()) { 1682 isDelete = ff.delete(); 1683 if (!isDelete) 1684 throw new DException("DSE5522", new Object [] {}); 1685 } 1686 try { 1687 raf = new RandomAccessFile(ff, "rw"); 1688 } catch (FileNotFoundException ex) { 1689 } 1690 } 1691 1692 1694 public void checkIsValidUserBeforeDropDatabase(String userName, _Connection conn) throws DException { 1695 DataDictionary dd = (DataDictionary) ( (_ServerSession) conn).getDataDictionary(); 1696 _SelectQueryIterator schemaIterator = (_SelectQueryIterator) dd.getPreparedStatementGetter(). 1697 getSchemataTableExecuter().executeForFresh(new Object [] {"users", "users"}); 1698 if (schemaIterator.first()) { 1699 Object [] ob = (Object []) schemaIterator.getObject(); 1700 for (int i = 0; i < ob.length; i++) { 1701 if (! (userName.equalsIgnoreCase(ob[2].toString()) || userName.equalsIgnoreCase(ServerSystem.browserUser))) { 1702 conn.close(); 1703 throw new DException("DSE8179", new Object [] {userName}); 1704 } 1705 } 1706 } 1707 } 1708 1709 public boolean isUserActiveMoreThanOnceOnSameDatabase(String databaseName, String userName) throws DException { 1710 ArrayList listOfConnections = (ArrayList) serverSessionList.get(databaseName); 1711 if (listOfConnections == null || listOfConnections.size() == 0) 1712 return false; 1713 int count = 0; 1714 synchronized (listOfConnections) { 1715 for (int i = listOfConnections.size(); i-- > 0; ) { 1716 WeakReference wk = (WeakReference) listOfConnections.get(i); 1717 Object obj = wk.get(); 1718 if (obj != null) 1719 if ( ( (_Connection) obj).getCurrentUser().equalsIgnoreCase(userName)) { 1720 count++; 1721 if (count > 1) 1722 return true; 1723 } 1724 } 1725 } 1726 return false; 1727 } 1728} 1729 | Popular Tags |