1 /* 2 * Copyright (c) 2014, 2017, 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.FileInputStream; 33 import java.io.FileNotFoundException; 34 import java.io.IOException; 35 import java.io.InputStream; 36 import java.nio.file.Files; 37 import java.text.MessageFormat; 38 import java.util.*; 39 import com.oracle.tools.packager.IOUtils; 40 41 public abstract class AbstractBundler implements Bundler { 42 43 private static final ResourceBundle I18N = 44 ResourceBundle.getBundle(AbstractBundler.class.getName()); 45 46 public static final BundlerParamInfo<File> IMAGES_ROOT = new WindowsBundlerParam<>( 47 I18N.getString("param.images-root.name"), 48 I18N.getString("param.images-root.description"), 49 "imagesRoot", 50 File.class, 51 params -> new File(StandardBundlerParam.BUILD_ROOT.fetchFrom(params), "images"), 52 (s, p) -> null); 53 54 //do not use file separator - 55 // we use it for classpath lookup and there / are not platform specific 56 public final static String BUNDLER_PREFIX = "package/"; 57 58 protected Class baseResourceLoader = null; 59 60 protected void fetchResource( 61 String publicName, String category, 62 String defaultName, File result, boolean verbose, File publicRoot) 63 throws IOException { 64 InputStream is = streamResource(publicName, category, defaultName, verbose, publicRoot); 65 if (is != null) { 66 Files.copy(is, result.toPath()); 67 } else { 68 if (verbose) { 69 Log.info(MessageFormat.format(I18N.getString("message.using-default-resource"), category == null ? "" : "[" + category + "] ", publicName)); 70 } 71 } 72 } 73 74 protected void fetchResource( 75 String publicName, String category, 76 File defaultFile, File result, boolean verbose, File publicRoot) 77 throws IOException { 78 InputStream is = streamResource(publicName, category, null, verbose, publicRoot); 79 if (is != null) { 80 Files.copy(is, result.toPath()); 81 } else { 82 IOUtils.copyFile(defaultFile, result); 83 if (verbose) { 84 Log.info(MessageFormat.format(I18N.getString("message.using-custom-resource-from-file"), category == null ? "" : "[" + category + "] ", defaultFile.getAbsoluteFile())); 85 } 86 } 87 } 88 89 private InputStream streamResource(String publicName, String category, 90 String defaultName, boolean verbose, File publicRoot) throws IOException { 91 boolean custom = false; 92 InputStream is = null; 93 if (publicName != null) { 94 if (publicRoot != null) { 95 File publicResource = new File(publicRoot, publicName); 96 if (publicResource.exists() && publicResource.isFile()) { 97 is = new FileInputStream(publicResource); 98 } 99 } else { 100 is = baseResourceLoader.getClassLoader().getResourceAsStream(publicName); 101 } 102 custom = (is != null); 103 } 104 if (is == null && defaultName != null) { 105 is = baseResourceLoader.getResourceAsStream(defaultName); 106 } 107 String msg = null; 108 if (custom) { 109 msg = MessageFormat.format(I18N.getString("message.using-custom-resource-from-classpath"), category == null ? "" : "[" + category + "] ", publicName); 110 } else if (is != null) { 111 msg = MessageFormat.format(I18N.getString("message.using-default-resource-from-classpath"), category == null ? "" : "[" + category + "] ", publicName); 112 } 113 if (verbose && is != null) { 114 Log.info(msg); 115 } 116 return is; 117 } 118 119 protected String preprocessTextResource(String publicName, String category, 120 String defaultName, Map<String, String> pairs, 121 boolean verbose, File publicRoot) throws IOException { 122 InputStream inp = streamResource(publicName, category, defaultName, verbose, publicRoot); 123 if (inp == null) { 124 throw new RuntimeException("Jar corrupt? No "+defaultName+" resource!"); 125 } 126 127 //read fully into memory 128 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 129 byte[] buffer = new byte[1024]; 130 int length; 131 while ((length = inp.read(buffer)) != -1) { 132 baos.write(buffer, 0, length); 133 } 134 135 //substitute 136 String result = new String(baos.toByteArray()); 137 for (Map.Entry<String, String> e : pairs.entrySet()) { 138 if (e.getValue() != null) { 139 result = result.replace(e.getKey(), e.getValue()); 140 } 141 } 142 return result; 143 } 144 145 @Override 146 public String toString() { 147 return getName(); 148 } 149 150 @Override 151 public void cleanup(Map<String, ? super Object> params) { 152 if (!StandardBundlerParam.VERBOSE.fetchFrom(params)) { 153 try { 154 IOUtils.deleteRecursive(StandardBundlerParam.BUILD_ROOT.fetchFrom(params)); 155 } catch (FileNotFoundException ignored) {} 156 } 157 } 158 }