@Override
  public void deployPackage(
      PackageModel packageRN,
      String token,
      InputStream stream,
      Map<String, DatabaseAction> associatedDatabases) {
    try {
      install(stream, packageRN, associatedDatabases);

      String name = packageRN.getUrlPath();
      if (packageRN.getUrlPath() == null || packageRN.getUrlPath().isEmpty()) name = "ROOT";

      name = name + PackageFileType.APP_WAR.getDotExtension();
      String file = packageRN.getName() + PackageFileType.APP_WAR.getDotExtension();
      progress.status("... deploying application: " + name, 1, LogLevel.INFO);
      requestTaskService.deploy(packageRN.getId(), name, file);
      progress.status("Archive version", 2, LogLevel.INFO);
      OPFEngine.RegistryService.archive(packageRN.getId());
      progress.status("Installation process has been completed", 2, LogLevel.INFO);
    } catch (ClientHandlerException e) {
      if (e.getCause() instanceof FileNotFoundException) {
        progress.status("Package archive has not been found", 1, LogLevel.ERROR);
      } else if (e.getCause() instanceof ConnectException) {
        progress.status("Connection refused", 1, LogLevel.ERROR);
      } else {
        progress.status("Occurred error", 1, LogLevel.ERROR);
      }
      logger.error(e.getMessage(), e);
    } catch (Exception e) {
      progress.status("Occurred error", 1, LogLevel.ERROR);
      logger.error(e.getMessage(), e);
    } finally {
      if (stream != null) {
        IOUtils.closeQuietly(stream);
      }
    }
  }
  private void uninstall(PackageModel model, Map<String, DatabaseAction> associatedDatabases) {
    progress.status("Uninstallation process is running...", 1, LogLevel.INFO);
    String ofr = model.getName() + PackageFileType.PACKAGE_OFR.getDotExtension();
    try {
      InputStream stream =
          OPFEngine.FileStoreService.download(
              OpenFlame.FILESTORE_BASE,
              ofr,
              helper.getVersion(),
              model.getId().toString(),
              model.getDatabaseVersion().toString());
      InputStream xml = ArchiveUtils.getFile(stream, PackageFileType.PACKAGE_XML.getOfrFileName());
      IPackageDescriptor descriptor = packageInstallationService.getPackageDescriptor(xml);
      IOUtils.closeQuietly(xml);
      IOUtils.closeQuietly(stream);

      Map<String, List<String>> tablesMap =
          packageInstallationService.generateSQLTables(descriptor);
      int droppedDatabases = 0;
      for (Map.Entry<String, DatabaseAction> entry : associatedDatabases.entrySet()) {
        String lookup = entry.getKey();
        DatabaseAction databaseAction = entry.getValue();
        if (DatabaseActionType.DROP.equals(databaseAction.getAction())) {
          Database database = databaseAction.getDatabase();

          List<String> tables = tablesMap.get(lookup);
          if (tables != null && tables.size() != 0) {
            databaseManager.dropTables(database, tables);

            progress.status(
                "... database '" + database.getUrlPath() + "' dropping process is running",
                2,
                LogLevel.DEBUG);
            databaseManager.dropDatabase(database);
            progress.status(
                "database '" + database.getUrlPath() + "' dropping process has been finished",
                1,
                LogLevel.DEBUG);
            droppedDatabases++;
          }
        }
      }
      if (droppedDatabases > 0) {
        progress.status("all tables have been dropped.", 1, LogLevel.DEBUG);
      }
    } catch (IOException e) {
      logger.error(e.getMessage(), e);
    }
  }
  @Override
  public void undeployPackage(
      PackageModel packageRN, String token, Map<String, DatabaseAction> associatedDatabases) {
    try {
      if (!associatedDatabases.isEmpty()) uninstall(packageRN, associatedDatabases);

      String name = packageRN.getUrlPath();
      if (packageRN.getUrlPath() == null || packageRN.getUrlPath().isEmpty()) name = "ROOT";

      name = name + PackageFileType.APP_WAR.getDotExtension();
      progress.status("... undeploying application: " + name, 1, LogLevel.INFO);
      requestTaskService.undeploy(packageRN.getId(), name);
      progress.status("Uninstallation process has been completed", 1, LogLevel.INFO);
    } catch (Exception e) {
      progress.status("Occurred error" + e.getMessage(), 1, LogLevel.ERROR);
      logger.error(e.getMessage(), e);
    }
  }