1 /*
   2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.oracle.tools.packager.windows;
  27 
  28 import static com.oracle.tools.packager.StandardBundlerParam.*;
  29 
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.text.MessageFormat;
  33 import java.util.Arrays;
  34 import java.util.Collection;
  35 import java.util.Map;
  36 import java.util.ResourceBundle;
  37 
  38 import com.oracle.tools.packager.AbstractBundler;
  39 import com.oracle.tools.packager.BundlerParamInfo;
  40 import com.oracle.tools.packager.Log;
  41 import com.oracle.tools.packager.ConfigException;
  42 import com.oracle.tools.packager.IOUtils;
  43 import com.oracle.tools.packager.UnsupportedPlatformException;
  44 
  45 public class WinServiceBundler extends AbstractBundler {
  46 
  47     private static final ResourceBundle I18N = 
  48             ResourceBundle.getBundle(WinServiceBundler.class.getName());
  49     
  50     private final static String EXECUTABLE_SVC_NAME = "WinLauncherSvc.exe";
  51     
  52     public WinServiceBundler() {
  53         super();
  54         baseResourceLoader = WinResources.class;
  55     }
  56     
  57     @Override
  58     public String getName() {
  59         return I18N.getString("bundler.name");
  60     }
  61 
  62     @Override
  63     public String getDescription() {
  64         return I18N.getString("bundler.description");
  65     }
  66 
  67     @Override
  68     public String getID() {
  69         return "windows.service";
  70     }
  71 
  72     @Override
  73     public String getBundleType() {
  74         return "IMAGE";
  75     }
  76 
  77     @Override
  78     public Collection<BundlerParamInfo<?>> getBundleParameters() {
  79         return getServiceBundleParameters();
  80     }
  81 
  82     public static Collection<BundlerParamInfo<?>> getServiceBundleParameters() {
  83         return Arrays.asList(
  84                 APP_NAME,
  85                 BUILD_ROOT
  86         );
  87     }
  88     
  89     @Override
  90     public boolean validate(Map<String, ? super Object> params)
  91             throws UnsupportedPlatformException, ConfigException
  92     {
  93         try {
  94             if (params == null) throw new ConfigException(
  95                     I18N.getString("error.parameters-null"),
  96                     I18N.getString("error.parameters-null.advice"));
  97 
  98             return doValidate(params);
  99         } catch (RuntimeException re) {
 100             throw new ConfigException(re);
 101         }
 102     }
 103 
 104     boolean doValidate(Map<String, ? super Object> p) throws UnsupportedPlatformException, ConfigException {
 105         if (!System.getProperty("os.name").toLowerCase().startsWith("win")) {
 106             throw new UnsupportedPlatformException();
 107         }
 108 
 109         if (WinResources.class.getResource(EXECUTABLE_SVC_NAME) == null) {
 110             throw new ConfigException(
 111                     I18N.getString("error.no-windows-resources"),
 112                     I18N.getString("error.no-windows-resources.advice"));
 113         }
 114         
 115         return true;
 116     }
 117     
 118     @Override
 119     public File execute(Map<String, ? super Object> params, File outputParentDir) {
 120         return doBundle(params, outputParentDir, false);
 121     }
 122     
 123     static String getAppName(Map<String, ? super Object>  p) {
 124         return APP_NAME.fetchFrom(p);
 125     }
 126     
 127     static String getAppSvcName(Map<String, ? super Object>  p) {
 128         return APP_NAME.fetchFrom(p) + "Svc";
 129     }
 130     
 131     public static File getLauncherSvc(File outDir, Map<String, ? super Object> p) {
 132         return new File(outDir, getAppName(p) + "Svc.exe");
 133     }
 134     
 135     /*
 136      * Copies the service launcher to the output folder
 137      * 
 138      * Note that the bundler doesn't create folder structure and
 139      * just copies the launcher to the output folder
 140      */
 141     File doBundle(Map<String, ? super Object> p, File outputDirectory, boolean dependentTask) {
 142         if (!outputDirectory.isDirectory() && !outputDirectory.mkdirs()) {
 143             throw new RuntimeException(MessageFormat.format(I18N.getString("error.cannot-create-output-dir"), outputDirectory.getAbsolutePath()));
 144         }
 145         if (!outputDirectory.canWrite()) {
 146             throw new RuntimeException(MessageFormat.format(I18N.getString("error.cannot-write-to-output-dir"), outputDirectory.getAbsolutePath()));
 147         }
 148         try {
 149             if (!dependentTask) {
 150                 Log.info(MessageFormat.format(I18N.getString("message.creating-service-bundle"), getAppSvcName(p), outputDirectory.getAbsolutePath()));
 151             }
 152             
 153             // Copy executable to install application as service
 154             File executableSvcFile = getLauncherSvc(outputDirectory, p);
 155             IOUtils.copyFromURL(
 156                     WinResources.class.getResource(EXECUTABLE_SVC_NAME),
 157                     executableSvcFile);
 158             executableSvcFile.setExecutable(true, false);
 159             
 160             if (!dependentTask) {
 161                 Log.info(MessageFormat.format(I18N.getString("message.result-dir"), outputDirectory.getAbsolutePath()));
 162             }
 163 
 164             return outputDirectory;
 165         } catch (IOException ex) {
 166             Log.info("Exception: "+ex);
 167             Log.debug(ex);
 168             return null;
 169         }
 170         
 171     }
 172 
 173 }