private EventProcessingDeployer getDeployer(Artifact artifact, AxisConfiguration axisConfig) {
   Deployer deployer;
   if (EventProcessingAppDeployerConstants.CEP_EVENT_RECEIVER_TYPE.equals(artifact.getType())) {
     deployer =
         AppDeployerUtils.getArtifactDeployer(
             axisConfig, EventProcessingAppDeployerConstants.CEP_EVENT_RECEIVER_DIR, "xml");
   } else if (EventProcessingAppDeployerConstants.CEP_EVENT_PUBLISHER_TYPE.equals(
       artifact.getType())) {
     deployer =
         AppDeployerUtils.getArtifactDeployer(
             axisConfig, EventProcessingAppDeployerConstants.CEP_EVENT_PUBLISHER_DIR, "xml");
   } else if (EventProcessingAppDeployerConstants.CEP_EXECUTION_PLAN_TYPE.equals(
       artifact.getType())) {
     deployer =
         AppDeployerUtils.getArtifactDeployer(
             axisConfig, EventProcessingAppDeployerConstants.CEP_EXECUTION_PLAN_DIR, "xml");
   } else if (EventProcessingAppDeployerConstants.CEP_EVENT_STREAM_TYPE.equals(
       artifact.getType())) {
     deployer =
         AppDeployerUtils.getArtifactDeployer(
             axisConfig, EventProcessingAppDeployerConstants.CEP_EVENT_STREAM_DIR, "json");
   } else {
     deployer = null;
   }
   return (EventProcessingDeployer) deployer;
 }
 /**
  * Check whether a particular artifact type can be accepted for deployment. If the type doesn't
  * exist in the acceptance list, we assume that it doesn't require any special features to be
  * installed in the system. Therefore, that type is accepted. If the type exists in the acceptance
  * list, the acceptance value is returned.
  *
  * @param serviceType - service type to be checked
  * @return true if all features are there or entry is null. else false
  */
 private boolean isAccepted(String serviceType) {
   if (acceptanceList == null) {
     acceptanceList =
         AppDeployerUtils.buildAcceptanceList(GadgetAppDeployerDSComponent.getRequiredFeatures());
   }
   Boolean acceptance = acceptanceList.get(serviceType);
   return (acceptance == null || acceptance);
 }
  public String extractGarArchive(String garPath) throws CarbonException {
    garPath = AppDeployerUtils.formatPath(garPath);
    String fileName = garPath.substring(garPath.lastIndexOf('/') + 1);

    String javaTempDir = System.getProperty("axis2.work.dir");
    String appUnzipDir =
        javaTempDir.endsWith(File.separator)
            ? javaTempDir + AppDeployerConstants.CARBON_APPS
            : javaTempDir + File.separator + AppDeployerConstants.CARBON_APPS;
    String destinationDir =
        appUnzipDir + File.separator + System.currentTimeMillis() + fileName + File.separator;
    AppDeployerUtils.createDir(destinationDir);

    try {
      extract(garPath, destinationDir);
    } catch (IOException e) {
      throw new CarbonException("Error while extracting cApp artifact : " + fileName, e);
    }

    return destinationDir;
  }
 private void deployTypeSpecifiedArtifacts(
     List<Artifact> artifacts, AxisConfiguration axisConfig, String directory, String fileType)
     throws DeploymentException {
   for (Artifact artifact : artifacts) {
     EventProcessingDeployer deployer;
     deployer =
         (EventProcessingDeployer)
             AppDeployerUtils.getArtifactDeployer(axisConfig, directory, fileType);
     if (deployer != null) {
       deploy(deployer, artifact);
     }
   }
 }
  /**
   * 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;
        }
      }
    }
  }
  /**
   * 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());
        }
      }
    }
  }
  private static void extract(String sourcePath, String destPath) throws IOException {
    Enumeration entries;
    ZipFile zipFile;

    zipFile = new ZipFile(sourcePath);
    entries = zipFile.entries();

    while (entries.hasMoreElements()) {
      ZipEntry entry = (ZipEntry) entries.nextElement();

      // if the entry is a directory, create a new dir
      if (entry.isDirectory()) {
        AppDeployerUtils.createDir(destPath + entry.getName());
        continue;
      }
      // if the entry is a file, write the file
      copyInputStream(
          zipFile.getInputStream(entry),
          new BufferedOutputStream(new FileOutputStream(destPath + entry.getName())));
    }
    zipFile.close();
  }