예제 #1
0
  private Alarm getNext() {
    Set<Alarm> alarmQueue =
        new TreeSet<Alarm>(
            new Comparator<Alarm>() {
              @Override
              public int compare(Alarm lhs, Alarm rhs) {
                int result = 0;
                long diff =
                    lhs.getAlarmTime().getTimeInMillis() - rhs.getAlarmTime().getTimeInMillis();
                if (diff > 0) {
                  return 1;
                } else if (diff < 0) {
                  return -1;
                }
                return result;
              }
            });

    Database.init(getApplicationContext());
    List<Alarm> alarms = Database.getAll();

    for (Alarm alarm : alarms) {
      if (alarm.getAlarmActive()) alarmQueue.add(alarm);
    }
    if (alarmQueue.iterator().hasNext()) {
      return alarmQueue.iterator().next();
    } else {
      return null;
    }
  }
예제 #2
0
 public static boolean init(String username, String password) {
   Database.init("databases.aii.avans.nl", "miniproject7_db2", username, password);
   Connection con = Database.getConnection();
   return (con != null);
 }
예제 #3
0
  private Session createSession(ConnectionInfo ci, boolean ifExists) {
    String name = ci.getDatabaseName();
    name = Database.parseDatabaseShortName(ci.getDbSettings(), name);
    Database database;
    boolean openNew = ci.getProperty("OPEN_NEW", false);
    if (openNew) {
      database = null;
    } else {
      database = DATABASES.get(name);
    }
    User user = null;
    boolean opened = false;
    if (database == null) {
      if (ifExists && !SystemDatabase.exists(name)) {
        throw DbException.get(ErrorCode.DATABASE_NOT_FOUND_1, name);
      }
      database = createDatabase(ci.isPersistent());
      database.init(ci, name);
      opened = true;
      if (database.getAllUsers().isEmpty()) {
        // users is the last thing we add, so if no user is around,
        // the database is new (or not initialized correctly)
        user = new User(database, database.allocateObjectId(), ci.getUserName(), false);
        user.setAdmin(true);
        user.setUserPasswordHash(ci.getUserPasswordHash());
        database.setMasterUser(user);
      }
      DATABASES.put(name, database);

      if (database.isPersistent())
        SystemDatabase.addDatabase(database.getShortName(), database.getStorageEngineName());
    } else {
      if (!database.isInitialized()) database.init(ci, name);
    }

    synchronized (database) {
      if (opened) {
        // start the thread when already synchronizing on the database
        // otherwise a deadlock can occur when the writer thread
        // opens a new database (as in recovery testing)
        database.opened();
      }
      if (database.isClosing()) {
        return null;
      }
      if (user == null) {
        if (database.validateFilePasswordHash(
            ci.getProperty("CIPHER", null), ci.getFilePasswordHash())) {
          user = database.findUser(ci.getUserName());
          if (user != null) {
            if (!user.validateUserPasswordHash(ci.getUserPasswordHash())) {
              user = null;
            }
          }
        }
        if (opened && (user == null || !user.isAdmin())) {
          // reset - because the user is not an admin, and has no
          // right to listen to exceptions
          database.setEventListener(null);
        }
      }
      if (user == null) {
        database.removeSession(null);
        throw DbException.get(ErrorCode.WRONG_USER_OR_PASSWORD);
      }
      Session session = database.createSession(user);
      session.setConnectionInfo(ci);
      return session;
    }
  }