示例#1
0
文件: Database.java 项目: flabser/NB3
  private int theFirst(_Session ses) {
    List<String> lwd =
        Console.getValFromConsole("enter adminstrator user name> ", StringUtil.USERNAME_PATTERN);
    if (lwd != null) {
      if (lwd.contains("quit")) {
        Server.shutdown();
      } else {
        String userName = lwd.get(0);
        String pwd = "";
        try {
          pwd = lwd.get(1);
        } catch (IndexOutOfBoundsException e) {
          pwd = userName;
        }

        System.out.println("user \"" + userName + "\" has been registered");

        User entity = new User();
        entity.setSuperUser(true);
        entity.setLogin(userName);
        entity.setPwd(pwd);
        ApplicationDAO aDao = new ApplicationDAO(ses);
        entity.setAllowedApps(aDao.findAll());
        UserDAO uDao = new UserDAO(this);
        uDao.add(entity);
      }
    }
    return 0;
  }
示例#2
0
文件: Database.java 项目: flabser/NB3
  public Database() {
    Properties props = new Properties();
    props.setProperty("user", EnvConst.DB_USER);
    props.setProperty("password", EnvConst.DB_PWD);
    String sysDbURL =
        "jdbc:postgresql://" + EnvConst.DATABASE_HOST + ":" + EnvConst.CONN_PORT + "/postgres";

    try {
      if (!hasDatabase(EnvConst.APP_NAME, sysDbURL, props)) {
        Server.logger.infoLogEntry("creating database \"" + EnvConst.APP_NAME + "\"...");
        registerUser(dbUser, dbPwd, sysDbURL, props);
        if (createDatabase(EnvConst.APP_NAME, dbUser, sysDbURL, props) == 0) {
          Server.logger.infoLogEntry("the database has been created");
          isNascence = true;
        }
      }
    } catch (SQLException e) {
      Server.logger.errorLogEntry(e);
    }

    connectionURL =
        "jdbc:postgresql://"
            + EnvConst.DATABASE_HOST
            + ":"
            + EnvConst.CONN_PORT
            + "/"
            + EnvConst.APP_NAME;

    dbPool = new DBConnectionPool();
    try {
      dbPool.initConnectionPool(EnvConst.JDBC_DRIVER, connectionURL, dbUser, dbPwd);
    } catch (InstantiationException
        | IllegalAccessException
        | ClassNotFoundException
        | DatabasePoolException e) {
      e.printStackTrace();
    }

    Map<String, String> properties = new HashMap<String, String>();
    properties.put(PersistenceUnitProperties.JDBC_DRIVER, EnvConst.JDBC_DRIVER);
    properties.put(PersistenceUnitProperties.JDBC_USER, dbUser);
    properties.put(PersistenceUnitProperties.JDBC_PASSWORD, dbPwd);
    properties.put(PersistenceUnitProperties.JDBC_URL, connectionURL);

    // INFO,
    // OFF,
    // ALL,
    // CONFIG (developing)
    properties.put(PersistenceUnitProperties.LOGGING_LEVEL, EnvConst.JPA_LOG_LEVEL);
    properties.put(
        PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.CREATE_OR_EXTEND);
    properties.put(
        PersistenceUnitProperties.SCHEMA_GENERATION_SCRIPTS_ACTION,
        PersistenceUnitProperties.SCHEMA_GENERATION_DROP_AND_CREATE_ACTION);

    PersistenceProvider pp = new PersistenceProvider();
    factory = pp.createEntityManagerFactory(EnvConst.ADMINISTRATOR_APP_NAME, properties);
    if (factory == null) {
      Server.logger.errorLogEntry(
          "the entity manager of \""
              + EnvConst.ADMINISTRATOR_APP_NAME
              + "\" has not been initialized");
    } else {

      if (isNascence) {
        Server.logger.infoLogEntry("Loading primary data...");
        AppEnv env = new AppEnv(EnvConst.ADMINISTRATOR_APP_NAME, this);
        _Session ses = new _Session(env, new SuperUser());
        Server.logger.infoLogEntry("setup localization environment...");
        LanguageDAO dao = new LanguageDAO(ses);
        for (LanguageCode lc : Environment.langs) {
          Language entity = ServerConst.getLanguage(lc);
          try {
            dao.add(entity);
            Server.logger.infoLogEntry("add " + entity.getCode() + " language");
          } catch (SecureException e) {
            Server.logger.errorLogEntry(e);
          }
        }

        Server.logger.infoLogEntry("setup applications...");
        ApplicationDAO aDao = new ApplicationDAO(ses);
        for (Site site : Environment.webAppToStart.values()) {
          Application entity = ServerConst.getApplication(site);
          try {
            aDao.add(entity);
            Server.logger.infoLogEntry("register \"" + entity.getName() + "\" application");
          } catch (SecureException e) {
            Server.logger.errorLogEntry(e);
          }
        }

        theFirst(ses);
      }
    }
  }