@Override
 public List<Application> findAllByUser(User user) throws ServiceException {
   try {
     List<Application> applications = applicationDAO.findAllByUser(user.getId());
     logger.debug("ApplicationService : All Applications found ");
     return applications;
   } catch (PersistenceException e) {
     logger.error("Error ApplicationService : error findById Method : " + user);
     throw new ServiceException(user.toString(), e);
   }
 }
  @Override
  public Application findByNameAndUser(User user, String name) throws ServiceException {
    try {
      Application application = applicationDAO.findByNameAndUser(user.getId(), name);

      return application;

    } catch (PersistenceException e) {
      logger.error(user.toString(), e);
      throw new ServiceException(user.toString(), e);
    }
  }
 @Override
 public Long countApp(User user) throws ServiceException {
   try {
     return applicationDAO.countApp(user.getId());
   } catch (PersistenceException e) {
     throw new ServiceException(e.getLocalizedMessage(), e);
   }
 }
  /**
   * Lancer par signal de NoPublicController quand le processus sshd est démarré dans les containers
   * serveur et git
   */
  public Application updateEnv(Application application, User user) throws ServiceException {

    logger.info("--update Env of Server--");
    String command = null;
    Map<String, String> configShellModule = new HashMap<>();
    Map<String, String> configShellServer = new HashMap<>();

    Module moduleGit = moduleService.findGitModule(user.getLogin(), application);
    Server server = application.getServers().get(0);

    String rootPassword = application.getUser().getPassword();
    configShellModule.put("port", moduleGit.getSshPort());
    configShellModule.put("dockerManagerAddress", moduleGit.getApplication().getManagerIp());
    configShellModule.put("password", rootPassword);
    configShellModule.put("dockerManagerAddress", application.getManagerIp());
    logger.info("new server ip : " + server.getContainerIP());
    try {
      int counter = 0;
      while (!server.getStatus().equals(Status.START)
          || !moduleGit.getStatus().equals(Status.START)) {
        if (counter == 100) {
          break;
        }
        Thread.sleep(1000);
        logger.info(" wait git and server sshd processus start");
        logger.info(
            "SSHDSTATUS = server : " + server.getStatus() + " - module : " + moduleGit.getStatus());
        moduleGit = moduleService.findById(moduleGit.getId());
        server = serverService.findById(server.getId());
        counter++;
      }
      command = ". /cloudunit/scripts/update-env.sh " + server.getContainerIP();
      logger.info("command shell to execute [" + command + "]");

      shellUtils.executeShell(command, configShellModule);

      configShellServer.put("port", server.getSshPort());
      configShellServer.put("dockerManagerAddress", server.getApplication().getManagerIp());
      configShellServer.put("password", rootPassword);
      command = ". /cloudunit/scripts/rm-auth-keys.sh ";
      logger.info("command shell to execute [" + command + "]");

      shellUtils.executeShell(command, configShellServer);
      String cleanCommand = server.getServerAction().cleanCommand();
      if (cleanCommand != null) {
        shellUtils.executeShell(server.getServerAction().cleanCommand(), configShellServer);
      }
    } catch (Exception e) {
      moduleGit.setStatus(Status.FAIL);
      moduleGit = moduleService.saveInDB(moduleGit);
      server.setStatus(Status.FAIL);
      server = serverService.saveInDB(server);
      logger.error("Error :  Error during update Env var of GIT " + e);
      throw new ServiceException(e.getLocalizedMessage(), e);
    }
    return application;
  }
 /**
  * Test if the application already exists
  *
  * @param user
  * @param applicationName
  * @return
  * @throws ServiceException
  * @throws CheckException
  */
 @Override
 public boolean checkAppExist(User user, String applicationName)
     throws ServiceException, CheckException {
   logger.info("--CHECK APP EXIST--");
   if (applicationDAO.findByNameAndUser(user.getId(), applicationName) == null) {
     return false;
   } else {
     return true;
   }
 }
  /**
   * Lancer par signal de NoPublicController quand le processus sshd est (re)démarré dans container
   * serveur et git, pour mettre à jour la nouvelle IP du serveur
   */
  @Override
  public Application sshCopyIDToServer(Application application, User user) throws ServiceException {
    String command = null;
    Map<String, String> configShell = new HashMap<>();

    Module moduleGit = moduleService.findGitModule(user.getLogin(), application);

    if (logger.isDebugEnabled()) {
      logger.debug("--ssh Copy ID To Server--");
      logger.debug("ssh port : " + moduleGit.getSshPort());
      logger.debug("manager ip : " + application.getManagerIp());
    }

    for (Server server : application.getServers()) {
      configShell.put("password", server.getApplication().getUser().getPassword());
      configShell.put("port", moduleGit.getSshPort());
      configShell.put("dockerManagerAddress", application.getManagerIp());
      configShell.put("userLogin", server.getApplication().getUser().getLogin());

      try {
        int counter = 0;
        while (!server.getStatus().equals(Status.START)
            || !moduleGit.getStatus().equals(Status.START)) {
          if (counter == 100) {
            break;
          }
          Thread.sleep(1000);
          logger.info(" wait git and server ssh processus start");
          logger.info(
              "STATUS = server : " + server.getStatus() + " - module : " + moduleGit.getStatus());

          moduleGit = moduleService.findById(moduleGit.getId());
          server = serverService.findById(server.getId());
          counter++;
        }

        // To permit ssh access on server from git container
        command =
            "expect /cloudunit/scripts/ssh-copy-id-expect.sh "
                + moduleGit.getApplication().getUser().getPassword();
        logger.info("command shell to execute [" + command + "]");

        shellUtils.executeShell(command, configShell);

      } catch (Exception e) {
        moduleGit.setStatus(Status.FAIL);
        moduleGit = moduleService.saveInDB(moduleGit);
        server.setStatus(Status.FAIL);
        server = serverService.saveInDB(server);
        logger.error("Error :  Error during permit git to access to server " + e);

        throw new ServiceException(e.getLocalizedMessage(), e);
      }
    }

    try {
      moduleGit = moduleService.update(moduleGit);

      application.getModules().add(moduleGit);
      application.setGitContainerIP(moduleGit.getContainerIP());

    } catch (ServiceException e) {
      moduleGit.setStatus(Status.FAIL);
      moduleService.saveInDB(moduleGit);
      logger.error("Error :  Error during persist git module " + e);
      throw new ServiceException(e.getLocalizedMessage(), e);
    }
    logger.info(
        "ApplicationService : Application " + application.getName() + " successfully created.");
    return application;
  }