1 /*
   2  * Copyright (c) 2014, 2015, 2016 Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  */
   5 package com.oracle.appbundlers.utils;
   6 
   7 import java.io.File;
   8 import java.io.IOException;
   9 import java.nio.file.Path;
  10 import java.util.Map;
  11 import java.util.concurrent.ExecutionException;
  12 import java.util.logging.Level;
  13 import java.util.logging.Logger;
  14 
  15 import com.oracle.appbundlers.utils.installers.AbstractBundlerUtils;
  16 import com.oracle.tools.packager.Bundler;
  17 import com.oracle.tools.packager.ConfigException;
  18 import com.oracle.tools.packager.UnsupportedPlatformException;
  19 
  20 /**
  21  *
  22  * @author Andrei Eremeev <andrei.eremeev@oracle.com>
  23  */
  24 public abstract class BundlingManager implements Constants {
  25 
  26     private static final Logger LOG = Logger
  27             .getLogger(BundlingManager.class.getName());
  28 
  29     private boolean installed = false;
  30 
  31     private final AbstractBundlerUtils bundlerUtils;
  32     protected AppWrapper app;
  33 
  34     public BundlingManager(AbstractBundlerUtils bundlerUtils) {
  35         this.bundlerUtils = bundlerUtils;
  36     }
  37 
  38     public abstract boolean validate(Map<String, Object> params)
  39             throws UnsupportedPlatformException, ConfigException;
  40 
  41     public abstract File execute(Map<String, Object> params, File file)
  42             throws IOException;
  43 
  44     public Path execute(Map<String, Object> params, AppWrapper app)
  45             throws IOException {
  46         this.app = app;
  47         LOG.log(Level.INFO, "Bundling with params: {0}.", params);
  48         return execute(params, app.getBundlesDir().toFile()).toPath();
  49     }
  50 
  51     public Bundler getBundler() {
  52         return bundlerUtils.getBundler();
  53     }
  54 
  55     public void verifyOption(String name, Object value, AppWrapper app2,
  56             String applicationTitle) {
  57         bundlerUtils.verifyOption(name, value, app2, applicationTitle);
  58     }
  59 
  60     public Path getInstalledAppRootLocation(AppWrapper app,
  61             String applicationTitle) {
  62         return bundlerUtils.getInstalledAppRootLocation(app, applicationTitle);
  63     }
  64 
  65     public ProcessOutput runInstalledExecutable(AppWrapper app,
  66             String applicationTitle) throws IOException, ExecutionException {
  67         return bundlerUtils.runInstalledExecutable(app, applicationTitle);
  68     }
  69 
  70     public Path getInstalledExecutableLocation(AppWrapper app,
  71             String applicationTitle) {
  72         return bundlerUtils.getInstalledExecutableLocation(app,
  73                 applicationTitle);
  74     }
  75 
  76     /**
  77      * install application
  78      *
  79      * @param app
  80      *            application to install
  81      * @param applicationTitle
  82      *            application title
  83      * @return executable path
  84      * @throws java.io.IOException
  85      */
  86     public String install(AppWrapper app, String applicationTitle,
  87             boolean manual) throws IOException {
  88         try {
  89             if (!manual) {
  90                 return bundlerUtils.install(app, applicationTitle);
  91             } else {
  92                 bundlerUtils.manualInstall(app);
  93                 return null;
  94             }
  95         } finally {
  96             installed = true;
  97         }
  98     }
  99 
 100     /**
 101      * uninstall application
 102      *
 103      * @param app
 104      *            application to uninstall
 105      * @param applicationTitle
 106      *            application title
 107      * @throws java.io.IOException
 108      */
 109     public void uninstall(AppWrapper app, String applicationTitle)
 110             throws IOException {
 111         if (installed) {
 112             bundlerUtils.uninstall(app, applicationTitle);
 113             installed = false;
 114         }
 115     }
 116 
 117     public String getAppName(Map<String, Object> params) {
 118         return bundlerUtils.getAppName(params);
 119     }
 120 
 121     public abstract String getShortName();
 122 
 123     @Override
 124     public String toString() {
 125         return getBundler().getID() + "-" + getShortName();
 126     }
 127 
 128     public Path getAppCDSCacheFile(AppWrapper app, String appName) {
 129         return bundlerUtils.getAppCDSCacheFile(app, appName);
 130     }
 131 
 132     public AbstractBundlerUtils getBundlerUtils() {
 133         return bundlerUtils;
 134     }
 135 }