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