@Override
 public void undeployArtifacts(
     CarbonApplication carbonApplication, AxisConfiguration axisConfiguration)
     throws DeploymentException {
   if (Boolean.getBoolean(Constants.DISABLE_ANALYTICS_DATA_PURGING_JVM_OPTION)) {
     return;
   }
   List<Artifact.Dependency> artifacts =
       carbonApplication.getAppConfig().getApplicationArtifact().getDependencies();
   for (Artifact.Dependency dep : artifacts) {
     Artifact artifact = dep.getArtifact();
     if (artifact == null) {
       continue;
     }
     if (TYPE.equals(artifact.getType())) {
       List<CappFile> files = artifact.getFiles();
       if (files.size() != 1) {
         log.error(
             "Purging artifact must have a single XML file to be deployed. But "
                 + files.size()
                 + " "
                 + "files found");
         continue;
       }
       if (AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED.equals(
           artifact.getDeploymentStatus())) {
         String fileName = artifact.getFiles().get(0).getName();
         fileName = GenericUtils.checkAndReturnPath(fileName);
         String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
         deletePurgingTasks(artifactPath);
         artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_PENDING);
       }
     }
   }
 }
 void deploy(EventProcessingDeployer deployer, Artifact artifact) throws DeploymentException {
   List<CappFile> files = artifact.getFiles();
   String fileName = artifact.getFiles().get(0).getName();
   String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
   try {
     deployer.processDeployment(new DeploymentFileData(new File(artifactPath), deployer));
     artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED);
   } catch (Exception e) {
     artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
     log.error("Deployment is failed due to " + e.getMessage(), e);
     throw new DeploymentException(e.getMessage(), e);
   }
 }
  /**
   * Check the artifact type and if it is a Gadget, copy it to the Gadget deployment hot folder
   *
   * @param carbonApp - CarbonApplication instance to check for Gadget artifacts
   * @param axisConfig - AxisConfiguration of the current tenant
   */
  public void deployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig)
      throws DeploymentException {
    List<Artifact.Dependency> artifacts =
        carbonApp.getAppConfig().getApplicationArtifact().getDependencies();

    for (Artifact.Dependency dep : artifacts) {
      Deployer deployer;
      Artifact artifact = dep.getArtifact();
      if (artifact == null) {
        continue;
      }
      if (!isAccepted(artifact.getType())) {
        log.warn(
            "Can't deploy artifact : "
                + artifact.getName()
                + " of type : "
                + artifact.getType()
                + ". Required features are not installed in the system");
        continue;
      }

      if (GADGET_TYPE.equals(artifact.getType())) {
        deployer = AppDeployerUtils.getArtifactDeployer(axisConfig, GADGET_DIR, "gar");
      } else {
        continue;
      }

      List<CappFile> files = artifact.getFiles();
      if (files.size() != 1) {
        log.error(
            "Gadgets must have a single .gar file to "
                + "be deployed. But "
                + files.size()
                + " files found.");
        continue;
      }
      if (deployer != null) {
        String fileName = artifact.getFiles().get(0).getName();
        String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
        try {
          deployer.deploy(new DeploymentFileData(new File(artifactPath), deployer));
          artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED);
        } catch (DeploymentException e) {
          artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
          throw e;
        }
      }
    }
  }
  public void undeployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig) {
    List<Artifact.Dependency> artifacts =
        carbonApp.getAppConfig().getApplicationArtifact().getDependencies();

    for (Artifact.Dependency dep : artifacts) {
      EventProcessingDeployer deployer;
      Artifact artifact = dep.getArtifact();
      if (artifact == null) {
        continue;
      }

      deployer = getDeployer(artifact, axisConfig);

      List<CappFile> files = artifact.getFiles();
      if (files.size() != 1) {
        log.error("Artifact must have only a single file. But " + files.size() + " files found.");
        continue;
      }

      if (deployer != null
          && AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED.equals(
              artifact.getDeploymentStatus())) {
        String fileName = artifact.getFiles().get(0).getName();
        String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
        try {
          deployer.processUndeployment(artifactPath);
          artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_PENDING);
          File artifactFile = new File(artifactPath);
          if (artifactFile.exists() && !artifactFile.delete()) {
            log.warn("Couldn't delete App artifact file : " + artifactPath);
          }
        } catch (Exception e) {
          artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
          log.error(
              "Error occured while trying to undeploy : "
                  + artifact.getName()
                  + " due to "
                  + e.getMessage(),
              e);
        }
      }
    }
  }
  /**
   * Check the artifact type and if it is a Gadget, delete the file from the Gadget deployment hot
   * folder
   *
   * @param carbonApp - CarbonApplication instance to check for Gadget artifacts
   * @param axisConfig - AxisConfiguration of the current tenant
   */
  public void undeployArtifacts(CarbonApplication carbonApp, AxisConfiguration axisConfig) {

    List<Artifact.Dependency> artifacts =
        carbonApp.getAppConfig().getApplicationArtifact().getDependencies();

    for (Artifact.Dependency dep : artifacts) {
      Deployer deployer;
      Artifact artifact = dep.getArtifact();
      if (artifact == null) {
        continue;
      }

      if (GADGET_TYPE.equals(artifact.getType())) {
        deployer = AppDeployerUtils.getArtifactDeployer(axisConfig, GADGET_DIR, "gar");
      } else {
        continue;
      }

      List<CappFile> files = artifact.getFiles();
      if (files.size() != 1) {
        log.error("A Gadget must have a single .gar file. But " + files.size() + " files found.");
        continue;
      }
      if (deployer != null
          && AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED.equals(
              artifact.getDeploymentStatus())) {
        String fileName = artifact.getFiles().get(0).getName();
        String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
        try {
          deployer.undeploy(artifactPath);
          artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_PENDING);
          File artifactFile = new File(artifactPath);
          if (artifactFile.exists() && !artifactFile.delete()) {
            log.warn("Couldn't delete App artifact file : " + artifactPath);
          }
        } catch (DeploymentException e) {
          artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
          log.error("Error occured while trying to un deploy : " + artifact.getName());
        }
      }
    }
  }
 @Override
 public void deployArtifacts(
     CarbonApplication carbonApplication, AxisConfiguration axisConfiguration)
     throws DeploymentException {
   if (Boolean.getBoolean(Constants.DISABLE_ANALYTICS_DATA_PURGING_JVM_OPTION)) {
     if (log.isDebugEnabled()) {
       log.debug("Purging task not scheduling through CApp.");
     }
     return;
   }
   List<Artifact.Dependency> artifacts =
       carbonApplication.getAppConfig().getApplicationArtifact().getDependencies();
   // loop through all artifacts
   for (Artifact.Dependency dep : artifacts) {
     Artifact artifact = dep.getArtifact();
     if (artifact == null) {
       continue;
     }
     if (TYPE.equals(artifact.getType())) {
       List<CappFile> files = artifact.getFiles();
       if (files.size() == 1) {
         String fileName = files.get(0).getName();
         fileName = GenericUtils.checkAndReturnPath(fileName);
         String artifactPath = artifact.getExtractedPath() + File.separator + fileName;
         try {
           registerPurgingTasks(artifactPath);
           artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED);
         } catch (DeploymentException e) {
           artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
           throw e;
         }
       } else if (files.size() != 0) {
         log.warn(
             "Purging artifact must have a single XML file to be deployed. But "
                 + files.size()
                 + " "
                 + "files found");
       }
     }
   }
 }
  /**
   * Deploy or un-deploy data sources. if deploying, adding the data source to the data sources and
   * if undeploying, removing the data source from data sources. there can be multiple data sources
   * as separate xml files.
   *
   * @param deploy - identify whether deployment process or un-deployment process.
   * @param artifacts - list of artifacts to be deployed.
   */
  private void deployUnDeployDataSources(boolean deploy, List<Artifact> artifacts)
      throws DeploymentException {
    for (Artifact artifact : artifacts) {
      if (DATA_SOURCE_TYPE.equals(artifact.getType())) {
        List<CappFile> files = artifact.getFiles();
        if (files == null || files.isEmpty()) {
          throw new DeploymentException(
              "DataSourceCappDeployer::deployUnDeployDataSources --> "
                  + "Error No data sources found in the artifact to deploy");
        }
        for (CappFile cappFile : files) {
          String fileName = cappFile.getName();
          String dataSourceConfigPath = artifact.getExtractedPath() + File.separator + fileName;

          File file = new File(dataSourceConfigPath);
          if (!file.exists()) {
            throw new DeploymentException(
                "DataSourceCappDeployer::deployUnDeployDataSources --> "
                    + "Error Data source file cannot be found in artifact, "
                    + "file name - "
                    + fileName);
          }
          DataSourceMetaInfo dataSourceMetaInfo = readDataSourceFile(file);
          /**
           * If some artifact fail to deploy, then whole car file will fail, then it will invoke
           * un-deploy flow for the car file so that should only un-deploy artifacts which are
           * already deployed. To identify that we use deployment status of the artifact
           */
          if (deploy) {
            try {
              if (DataSourceManager.getInstance()
                      .getDataSourceRepository()
                      .getDataSource(dataSourceMetaInfo.getName())
                  != null) {
                artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_FAILED);
                throw new DeploymentException(
                    "DataSourceCappDeployer::deployUnDeployDataSources --> "
                        + "Error in deploying data source: data source "
                        + "with same name already exist, "
                        + "data source name - "
                        + dataSourceMetaInfo.getName());
              }
              dataSourceMetaInfo.setCarbonApplicationDeployed(true);
              DataSourceManager.getInstance()
                  .getDataSourceRepository()
                  .addDataSource(dataSourceMetaInfo);
              artifact.setDeploymentStatus(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED);
            } catch (DataSourceException e) {
              throw new DeploymentException(
                  "DataSourceCappDeployer::deployUnDeployDataSources --> "
                      + "Error in deploying data source: "
                      + e.getMessage(),
                  e);
            }
          } else {
            try {
              if (DataSourceManager.getInstance()
                          .getDataSourceRepository()
                          .getDataSource(dataSourceMetaInfo.getName())
                      != null
                  && artifact
                      .getDeploymentStatus()
                      .equals(AppDeployerConstants.DEPLOYMENT_STATUS_DEPLOYED)) {
                DataSourceManager.getInstance()
                    .getDataSourceRepository()
                    .deleteDataSource(dataSourceMetaInfo.getName());
              }
            } catch (DataSourceException e) {
              throw new DeploymentException(
                  "DataSourceCappDeployer::deployUnDeployDataSources --> "
                      + "Error in undeploying data source: "
                      + e.getMessage(),
                  e);
            }
          }
        }
      }
    }
  }