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