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;
  27 
  28 import com.oracle.tools.packager.windows.WindowsBundlerParam;
  29 
  30 import java.io.ByteArrayOutputStream;
  31 import java.io.File;
  32 import java.io.IOException;
  33 import java.io.InputStream;
  34 import java.net.URL;
  35 import java.text.MessageFormat;
  36 import java.util.*;
  37 import java.util.regex.Pattern;
  38 
  39 public abstract class AbstractBundler implements Bundler {
  40 
  41     private static final ResourceBundle I18N =
  42             ResourceBundle.getBundle(AbstractBundler.class.getName());
  43 
  44     public static final BundlerParamInfo<File> IMAGES_ROOT = new WindowsBundlerParam<>(
  45             I18N.getString("param.images-root.name"),
  46             I18N.getString("param.images-root.description"),
  47             "imagesRoot",
  48             File.class,
  49             params -> new File(StandardBundlerParam.BUILD_ROOT.fetchFrom(params), "images"),
  50             (s, p) -> null);
  51 
  52     //do not use file separator -
  53     // we use it for classpath lookup and there / are not platform specific
  54     public final static String BUNDLER_PREFIX = "package/";
  55 
  56     protected Class baseResourceLoader = null;
  57     
  58     //helper method to test if required files are present in the runtime
  59     public void testRuntime(RelativeFileSet runtime, String[] file) throws ConfigException {
  60         if (runtime == null) {
  61             return; //null runtime is ok (request to use system)
  62         }
  63 
  64         Pattern[] weave = Arrays.stream(file).map(Pattern::compile).toArray(Pattern[]::new);
  65 
  66         if (!runtime.getIncludedFiles().stream().anyMatch(s ->
  67                 Arrays.stream(weave).anyMatch(pattern -> pattern.matcher(s).matches())
  68         )) {
  69             throw new ConfigException(
  70                     MessageFormat.format(I18N.getString("error.jre-missing-file"), Arrays.toString(file)),
  71                     I18N.getString("error.jre-missing-file.advice"));
  72         }
  73     }
  74 
  75     protected void fetchResource(
  76             String publicName, String category,
  77             String defaultName, File result, boolean verbose, File publicRoot)
  78             throws IOException {
  79         URL u = locateResource(publicName, category, defaultName, verbose, publicRoot);
  80         if (u != null) {
  81             IOUtils.copyFromURL(u, result);
  82         } else {
  83             if (verbose) {
  84                 Log.info(MessageFormat.format(I18N.getString("message.using-default-resource"), category == null ? "" : "[" + category + "] ", publicName));
  85             }
  86         }
  87     }
  88 
  89     protected void fetchResource(
  90             String publicName, String category,
  91             File defaultFile, File result, boolean verbose, File publicRoot)
  92             throws IOException {
  93         URL u = locateResource(publicName, category, null, verbose, publicRoot);
  94         if (u != null) {
  95             IOUtils.copyFromURL(u, result);
  96         } else {
  97             IOUtils.copyFile(defaultFile, result);
  98             if (verbose) {
  99                 Log.info(MessageFormat.format(I18N.getString("message.using-custom-resource-from-file"), category == null ? "" : "[" + category + "] ", defaultFile.getAbsoluteFile()));
 100             }
 101         }
 102     }
 103 
 104     private URL locateResource(String publicName, String category,
 105                                String defaultName, boolean verbose, File publicRoot) throws IOException {
 106         URL u = null;
 107         boolean custom = false;
 108         if (publicName != null) {
 109             if (publicRoot != null) {
 110                 File publicResource = new File(publicRoot, publicName);
 111                 if (publicResource.exists() && publicResource.isFile()) {
 112                     u = publicResource.toURI().toURL();
 113                 }
 114             } else {
 115                 u = baseResourceLoader.getClassLoader().getResource(publicName);
 116             }
 117             custom = (u != null);
 118         }
 119         if (u == null && defaultName != null) {
 120             u = baseResourceLoader.getResource(defaultName);
 121         }
 122         String msg = null;
 123         if (custom) {
 124             msg = MessageFormat.format(I18N.getString("message.using-custom-resource-from-classpath"), category == null ? "" : "[" + category + "] ", publicName);
 125         } else if (u != null) {
 126             msg = MessageFormat.format(I18N.getString("message.using-default-resource-from-classpath"), category == null ? "" : "[" + category + "] ", publicName);
 127         }
 128         if (verbose && u != null) {
 129             Log.info(msg);
 130         }
 131         return u;
 132     }
 133 
 134     protected String preprocessTextResource(String publicName, String category,
 135                                             String defaultName, Map<String, String> pairs,
 136                                             boolean verbose, File publicRoot) throws IOException {
 137         URL u = locateResource(publicName, category, defaultName, verbose, publicRoot);
 138         InputStream inp = u.openStream();
 139         if (inp == null) {
 140             throw new RuntimeException("Jar corrupt? No "+defaultName+" resource!");
 141         }
 142 
 143         //read fully into memory
 144         ByteArrayOutputStream baos = new ByteArrayOutputStream();
 145         byte[] buffer = new byte[1024];
 146         int length;
 147         while ((length = inp.read(buffer)) != -1) {
 148             baos.write(buffer, 0, length);
 149         }
 150 
 151         //substitute
 152         String result = new String(baos.toByteArray());
 153         for (Map.Entry<String, String> e : pairs.entrySet()) {
 154             if (e.getValue() != null) {
 155                 result = result.replace(e.getKey(), e.getValue());
 156             }
 157         }
 158         return result;
 159     }
 160 
 161     @Override
 162     public String toString() {
 163         return getName();
 164     }
 165 }