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