@Override
  public void contextInitialized(ServletContextEvent sce) {
    File app = Env.getDefaultEnvFile();
    File keyStore = InstallUtils.getKeyStore();

    Map<String, String> propertiesMap = KeyUtils.getMapProperties(app, keyStore);
    ConfigContainer.putAll(propertiesMap);

    File xmlProp = InstallUtils.getXmlEnv();
    if (!ConfigContainer.isAppInstalled() && xmlProp.exists()) {
      Map<String, String> settings = EnvironmentsUtils.convertFromXml(xmlProp);
      ConfigContainer.putAll(settings);
    }

    File propFile = InstallUtils.getPropEnv();
    if (!ConfigContainer.isAppInstalled() && propFile.exists()) {
      ConfigContainer.putAll(MiscUtils.getProperties(propFile));
    }

    if (!xmlProp.exists()) {
      if (!OpenFlameConfig.DOMAIN_URL.exist())
        OpenFlameConfig.DOMAIN_URL.setValue(TomcatUtils.getCatalinaHost(sce));
      if (!OpenFlameConfig.PORT.exist())
        OpenFlameConfig.PORT.setValue(TomcatUtils.getCatalinaPort(sce).toString());
      if (!OpenFlameConfig.CONTEXT_URL.exist())
        OpenFlameConfig.CONTEXT_URL.setValue(TomcatUtils.getCatalinaContext(sce));
    }

    String domainUrl = OpenFlameConfig.DOMAIN_URL.getValue();
    String port = OpenFlameConfig.PORT.getValue();
    String contextUrl = OpenFlameConfig.CONTEXT_URL.getValue();
    if (domainUrl != null && port != null && contextUrl != null) {
      Env.FIREJACK_URL.setValue(WebUtils.getNormalizedUrl(domainUrl, port, contextUrl));
    }

    ConfigContainer.setHost(TomcatUtils.getCatalinaHost(sce));
    ConfigContainer.setPort(TomcatUtils.getCatalinaPort(sce));

    super.contextInitialized(sce);
  }
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    LOG.info("Try to install MyCollab");
    response.setHeader("Cache-Control", "no-cache");
    response.setHeader("Expires", "-1");
    PrintWriter out = response.getWriter();
    String sitename = request.getParameter("sitename");
    String serverAddress = request.getParameter("serverAddress");
    String databaseName = request.getParameter("databaseName");
    String dbUserName = request.getParameter("dbUserName");
    String dbPassword = request.getParameter("dbPassword");
    String databaseServer = request.getParameter("databaseServer");
    String smtpUserName = request.getParameter("smtpUserName");
    String smtpPassword = request.getParameter("smtpPassword");
    String smtpHost = request.getParameter("smtpHost");
    String smtpPort = request.getParameter("smtpPort");
    String tls = request.getParameter("tls");
    String ssl = request.getParameter("ssl");

    String dbUrl =
        String.format(
            "jdbc:mysql://%s/%s?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&rewriteBatchedStatements=true&useCompression=true&useServerPrepStmts=false",
            databaseServer, databaseName);

    try {
      Class.forName("com.mysql.jdbc.Driver");
    } catch (ClassNotFoundException e) {
      LOG.error("Can not load mysql driver", e);
      return;
    }

    try (Connection connection = DriverManager.getConnection(dbUrl, dbUserName, dbPassword)) {
      LOG.debug("Check database config");
      connection.getMetaData();
    } catch (Exception e) {
      String rootCause = (e.getCause() == null) ? e.getMessage() : e.getCause().getMessage();
      out.write(
          "Cannot establish connection to database. Make sure your inputs are correct. Root cause is "
              + rootCause);
      LOG.error("Can not connect database", e);
      return;
    }

    int mailServerPort;
    try {
      mailServerPort = Integer.parseInt(smtpPort);
    } catch (Exception e) {
      mailServerPort = 0;
    }

    boolean isStartTls = Boolean.parseBoolean(tls);
    boolean isSsl = Boolean.parseBoolean(ssl);
    try {
      InstallUtils.checkSMTPConfig(
          smtpHost, mailServerPort, smtpUserName, smtpPassword, true, isStartTls, isSsl);
    } catch (UserInvalidInputException e) {
      LOG.warn("Cannot authenticate mail server successfully. Make sure your inputs are correct.");
    }

    VelocityContext templateContext = new VelocityContext();
    templateContext.put("sitename", sitename);
    templateContext.put("serveraddress", serverAddress);
    templateContext.put("dbUrl", dbUrl);
    templateContext.put("dbUser", dbUserName);
    templateContext.put("dbPassword", dbPassword);
    templateContext.put("smtpAddress", smtpHost);
    templateContext.put("smtpPort", mailServerPort + "");
    templateContext.put("smtpUserName", smtpUserName);
    templateContext.put("smtpPassword", smtpPassword);
    templateContext.put("smtpTLSEnable", tls);
    templateContext.put("smtpSSLEnable", ssl);

    File confFolder =
        FileUtils.getDesireFile(System.getProperty("user.dir"), "conf", "src/main/conf");
    if (confFolder == null) {
      throw new MyCollabException("The conf folder is not existed");
    }

    try {
      File templateFile = new File(confFolder, "mycollab.properties.template");
      FileReader templateReader = new FileReader(templateFile);

      StringWriter writer = new StringWriter();

      VelocityEngine engine = new VelocityEngine();
      engine.evaluate(templateContext, writer, "log task", templateReader);

      FileOutputStream outStream =
          new FileOutputStream(new File(confFolder, "mycollab.properties"));
      outStream.write(writer.toString().getBytes());
      outStream.flush();
      outStream.close();

      while (waitFlag) {
        try {
          Thread.sleep(5000);
        } catch (InterruptedException e) {
          throw new MyCollabException(e);
        }
      }

    } catch (Exception e) {
      LOG.error("Error while set up MyCollab", e);
      out.write(
          "Can not write the settings to the file system. You should check our knowledge base system or contact us at [email protected] to solve this issue.");
      return;
    }
  }