1 /* 2 * Copyright (c) 2014, 2019, 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 jdk.jpackage.internal; 27 28 import java.io.ByteArrayOutputStream; 29 import java.io.File; 30 import java.io.FileInputStream; 31 import java.io.BufferedInputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.nio.file.StandardCopyOption; 35 import java.nio.file.Files; 36 import java.text.MessageFormat; 37 import java.util.Map; 38 import java.util.ResourceBundle; 39 40 import jdk.jpackage.internal.resources.ResourceLocator; 41 42 /** 43 * AbstractBundler 44 * 45 * This is the base class all Bundlers extend from. 46 * It contains methods and parameters common to all Bundlers. 47 * The concrete implementations are in the platform specific Bundlers. 48 */ 49 public abstract class AbstractBundler implements Bundler { 50 51 private static final ResourceBundle I18N = ResourceBundle.getBundle( 52 "jdk.jpackage.internal.resources.MainResources"); 53 54 public static final BundlerParamInfo<File> IMAGES_ROOT = 55 new StandardBundlerParam<>( 56 I18N.getString("param.images-root.name"), 57 I18N.getString("param.images-root.description"), 58 "imagesRoot", 59 File.class, 60 params -> new File( 61 StandardBundlerParam.BUILD_ROOT.fetchFrom(params), "images"), 62 (s, p) -> null); 63 64 public InputStream getResourceAsStream(String name) { 65 return ResourceLocator.class.getResourceAsStream(name); 66 } 67 68 protected void fetchResource(String publicName, String category, 69 String defaultName, File result, boolean verbose, File publicRoot) 70 throws IOException { 71 72 InputStream is = streamResource(publicName, category, 73 defaultName, verbose, publicRoot); 74 if (is != null) { 75 try { 76 Files.copy(is, result.toPath(), 77 StandardCopyOption.REPLACE_EXISTING); 78 } finally { 79 is.close(); 80 } 81 } else { 82 if (verbose) { 83 Log.verbose(MessageFormat.format(I18N.getString( 84 "message.no-default-resource"), 85 defaultName == null ? "" : defaultName, 86 category == null ? "" : "[" + category + "] ", 87 publicName)); 88 } 89 } 90 } 91 92 protected void fetchResource(String publicName, String category, 93 File defaultFile, File result, boolean verbose, File publicRoot) 94 throws IOException { 95 96 InputStream is = streamResource(publicName, category, 97 null, verbose, publicRoot); 98 if (is != null) { 99 try { 100 Files.copy(is, result.toPath()); 101 } finally { 102 is.close(); 103 } 104 } else { 105 IOUtils.copyFile(defaultFile, result); 106 if (verbose) { 107 Log.verbose(MessageFormat.format(I18N.getString( 108 "message.using-custom-resource-from-file"), 109 category == null ? "" : "[" + category + "] ", 110 defaultFile.getAbsoluteFile())); 111 } 112 } 113 } 114 115 private InputStream streamResource(String publicName, String category, 116 String defaultName, boolean verbose, File publicRoot) 117 throws IOException { 118 boolean custom = false; 119 InputStream is = null; 120 if (publicName != null) { 121 if (publicRoot != null) { 122 File publicResource = new File(publicRoot, publicName); 123 if (publicResource.exists() && publicResource.isFile()) { 124 is = new BufferedInputStream( 125 new FileInputStream(publicResource)); 126 } 127 } else { 128 is = getResourceAsStream(publicName); 129 } 130 custom = (is != null); 131 } 132 if (is == null && defaultName != null) { 133 is = getResourceAsStream(defaultName); 134 } 135 if (verbose && is != null) { 136 String msg = null; 137 if (custom) { 138 msg = MessageFormat.format(I18N.getString( 139 "message.using-custom-resource"), 140 category == null ? 141 "" : "[" + category + "] ", publicName); 142 } else { 143 msg = MessageFormat.format(I18N.getString( 144 "message.using-default-resource"), 145 defaultName == null ? "" : defaultName, 146 category == null ? "" : "[" + category + "] ", 147 publicName); 148 } 149 Log.verbose(msg); 150 } 151 return is; 152 } 153 154 protected String preprocessTextResource(String publicName, String category, 155 String defaultName, Map<String, String> pairs, 156 boolean verbose, File publicRoot) throws IOException { 157 InputStream inp = streamResource( 158 publicName, category, defaultName, verbose, publicRoot); 159 if (inp == null) { 160 throw new RuntimeException( 161 "Jar corrupt? No " + defaultName + " resource!"); 162 } 163 164 // read fully into memory 165 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 166 byte[] buffer = new byte[1024]; 167 int length; 168 while ((length = inp.read(buffer)) != -1) { 169 baos.write(buffer, 0, length); 170 } 171 172 // substitute 173 String result = new String(baos.toByteArray()); 174 for (Map.Entry<String, String> e : pairs.entrySet()) { 175 if (e.getValue() != null) { 176 result = result.replace(e.getKey(), e.getValue()); 177 } 178 } 179 return result; 180 } 181 182 @Override 183 public String toString() { 184 return getName(); 185 } 186 187 @Override 188 public void cleanup(Map<String, ? super Object> params) { 189 try { 190 IOUtils.deleteRecursive( 191 StandardBundlerParam.BUILD_ROOT.fetchFrom(params)); 192 } catch (IOException e) { 193 Log.debug(e.getMessage()); 194 } 195 } 196 }