コード例 #1
0
  @Override
  public void contextInitialized(ServletContextEvent sce) {
    Map<String, String> env = System.getenv();
    // If we are running in the OPENSHIFT environment change the pu-name
    if (env.keySet().contains("OPENSHIFT_MYSQL_DB_HOST")) {
      PU_NAME = "PU_OPENSHIFT";
    }
    try {
      ServletContext context = sce.getServletContext();
      EntityManagerFactory emf =
          Persistence.createEntityManagerFactory(DeploymentConfiguration.PU_NAME);
      EntityManager em = emf.createEntityManager();

      // This flag is set in Web.xml -- Make sure to disable for a REAL system
      boolean makeTestUsers =
          context.getInitParameter("makeTestUsers").toLowerCase().equals("true");
      if (!makeTestUsers
          || (em.find(User.class, "user") != null
              && em.find(User.class, "admin") != null
              && em.find(User.class, "user_admin") != null)) {
        return;
      }
      Role userRole = new Role("User");
      Role adminRole = new Role("Admin");

      User user = new User("user", PasswordHash.createHash("test"));
      User admin = new User("admin", PasswordHash.createHash("test"));
      User both = new User("user_admin", PasswordHash.createHash("test"));
      user.AddRole(userRole);
      admin.AddRole(adminRole);
      both.AddRole(userRole);
      both.AddRole(adminRole);

      try {
        em.getTransaction().begin();
        em.persist(userRole);
        em.persist(adminRole);

        em.persist(user);
        em.persist(admin);
        em.persist(both);
        em.getTransaction().commit();
      } finally {
        em.close();
      }
    } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) {
      Logger.getLogger(DeploymentConfiguration.class.getName()).log(Level.SEVERE, null, ex);
    }
  }