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.sun.javafx.tools.packager;
27
28 import com.oracle.tools.packager.Bundlers;
29 import com.oracle.tools.packager.ConfigException;
30 import com.oracle.tools.packager.Log;
31 import com.oracle.tools.packager.Platform;
32 import com.oracle.tools.packager.UnsupportedPlatformException;
33 import com.sun.javafx.tools.packager.JarSignature.InputStreamSource;
34 import com.sun.javafx.tools.packager.bundlers.BundleParams;
35 import com.sun.javafx.tools.packager.bundlers.Bundler.BundleType;
36 import com.sun.javafx.tools.resource.PackagerResource;
37
38 import java.io.BufferedReader;
39 import java.io.File;
40 import java.io.FileInputStream;
41 import java.io.FileNotFoundException;
42 import java.io.FileOutputStream;
43 import java.io.FileWriter;
44 import java.io.IOException;
45 import java.io.InputStream;
46 import java.io.InputStreamReader;
47 import java.io.OutputStream;
48 import java.io.Writer;
49 import java.lang.reflect.Method;
50 import java.net.MalformedURLException;
51 import java.net.URL;
52 import java.net.URLClassLoader;
53 import java.nio.file.Files;
54 import java.nio.file.StandardCopyOption;
55 import java.security.InvalidKeyException;
56 import java.security.KeyStore;
57 import java.security.KeyStoreException;
58 import java.security.NoSuchAlgorithmException;
59 import java.security.PrivateKey;
60 import java.security.SignatureException;
61 import java.security.UnrecoverableKeyException;
62 import java.security.cert.Certificate;
63 import java.security.cert.CertificateException;
64 import java.security.cert.X509Certificate;
65 import java.text.MessageFormat;
66 import java.util.ArrayList;
67 import java.util.Base64;
68 import java.util.Enumeration;
69 import java.util.HashMap;
70 import java.util.HashSet;
71 import java.util.List;
72 import java.util.Map;
81 import java.util.zip.ZipEntry;
82 import java.util.zip.ZipOutputStream;
83
84 /**
85 * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead.
86 */
87 @Deprecated(since="10", forRemoval=true)
88 public class PackagerLib {
89 public static final String JAVAFX_VERSION = System.getProperty("java.version");
90
91 private static final ResourceBundle bundle =
92 ResourceBundle.getBundle("com/sun/javafx/tools/packager/Bundle");
93
94 private CreateJarParams createJarParams;
95 private CreateBSSParams createBssParams;
96 private File bssTmpDir;
97
98
99 private enum Filter {ALL, CLASSES_ONLY, RESOURCES}
100
101 private ClassLoader classLoader;
102
103 private ClassLoader getClassLoader() throws PackagerException {
104 if (classLoader == null) {
105 try {
106 URL[] urls = {new URL(getJfxrtPath())};
107 classLoader = URLClassLoader.newInstance(urls);
108 } catch (MalformedURLException ex) {
109 throw new PackagerException(ex, "ERR_CantFindRuntime");
110 }
111 }
112 return classLoader;
113 }
114
115 // if set of input resources consist of SINGLE element and
116 // this element is jar file then we expect this to be request to
117 // "update" jar file
118 // Input jar file MUST be executable jar file
119 //
120 // Check if we are in "special case" scenario
121 private File jarFileToUpdate(CreateJarParams params) {
122 if (params.resources.size() == 1) {
123 PackagerResource p = params.resources.get(0);
124 File f = p.getFile();
125 if (!f.isFile() || !f.getAbsolutePath().toLowerCase().endsWith(".jar")) {
126 return null;
127 }
128 try (JarFile jf = new JarFile(f)) {
129 jf.getManifest(); //try to read manifest to validate it is jar
130 return f;
131 } catch (Exception e) {
132 //treat any exception as "not a special case" scenario
133 com.oracle.tools.packager.Log.verbose(e);
134 }
500 InputStreamSource in = () -> new FileInputStream(jar);
501 if (!signedJar.isFile()) {
502 signedJar.createNewFile();
503 }
504 FileOutputStream fos = new FileOutputStream(signedJar);
505 signature.signJarAsBLOB(in, new ZipOutputStream(fos));
506 }
507
508 public void makeAll(MakeAllParams makeAllParams) throws PackagerException {
509 final String exe = (Platform.getPlatform() == Platform.WINDOWS) ? ".exe" : "";
510 String jHome = System.getenv("JAVA_HOME");
511 if (jHome == null) {
512 jHome = System.getProperty("java.home");
513 }
514 if (jHome == null) {
515 throw new PackagerException("ERR_MissingJavaHome");
516 }
517
518 final File javac = new File(new File(jHome), "bin/javac" + exe);
519
520 String jfxHome = System.getenv("JAVAFX_HOME");
521 if (jfxHome == null) {
522 jfxHome = System.getProperty("javafx.home");
523 }
524 if (jfxHome == null) {
525 throw new PackagerException("ERR_MissingJavaFxHome");
526 }
527
528 final String srcDirName = "src";
529 final String compiledDirName = "compiled";
530 final String distDirName = "dist";
531 final String outfileName = "dist";
532 final String jarName = outfileName + ".jar";
533
534 final File distDir = new File(distDirName);
535
536 final File compiledDir = new File(compiledDirName);
537 compiledDir.mkdir();
538
539 try {
540 final File tmpFile = File.createTempFile("javac", "sources", new File("."));
541 tmpFile.deleteOnExit();
542 try (FileWriter sources = new FileWriter(tmpFile)) {
543 scanAndCopy(new PackagerResource(new File(srcDirName), "."), sources, compiledDir);
544 }
545 String classpath = jfxHome + "/../lib/jfxrt.jar";
546 if (makeAllParams.classpath != null) {
547 classpath += File.pathSeparator + makeAllParams.classpath;
548 }
549 if (makeAllParams.verbose) {
550 Log.info("Executing javac:");
551 Log.infof("%s %s %s %s %s %s%n",
552 javac.getAbsolutePath(),
553 "-d", compiledDirName,
554 "-cp", classpath,
555 "@" + tmpFile.getAbsolutePath());
556 }
557 int ret = execute(
558 javac.getAbsolutePath(),
559 "-d", compiledDirName,
560 "-cp", classpath,
561 "@" + tmpFile.getAbsolutePath());
562 if (ret != 0) {
563 throw new PackagerException("ERR_JavacFailed", Integer.toString(ret));
564 }
565 } catch (PackagerException e) {
566 throw e;
567 } catch (Exception e) {
568 throw new PackagerException(e, "ERR_MakeAllJavacFailed");
569 }
570
571 CreateJarParams cjp = new CreateJarParams();
572 cjp.applicationClass = makeAllParams.appClass;
573 cjp.preloader = makeAllParams.preloader;
574 cjp.classpath = makeAllParams.classpath;
575 cjp.css2bin = false;
576 cjp.outdir = distDir;
577 cjp.outfile = jarName;
578 cjp.addResource(compiledDir, ".");
579
580 packageAsJar(cjp);
581
816 }
817
818 private void createBinaryCss(File f, File outdir, String relPath)
819 throws PackagerException {
820 if (f.isDirectory()) {
821 File[] children = f.listFiles();
822 if (children != null) {
823 for (File innerFile : children) {
824 createBinaryCss(innerFile, outdir, relPath + '/' + innerFile.getName());
825 }
826 }
827 } else if (f.getName().endsWith(".css")) {
828 String cssFileName = f.getAbsolutePath();
829 String bssFileName = new File(outdir.getAbsolutePath(),
830 replaceExtensionByBSS(relPath))
831 .getAbsolutePath();
832 createBinaryCss(cssFileName, bssFileName);
833 }
834 }
835
836 // Returns path to jfxrt.jar relatively to jar containing PackagerLib.class
837 private String getJfxrtPath() throws PackagerException {
838 String theClassFile = "PackagerLib.class";
839 Class theClass = PackagerLib.class;
840 String classUrl = theClass.getResource(theClassFile).toString();
841
842 if (!classUrl.startsWith("jar:file:") || !classUrl.contains("!")){
843 throw new PackagerException("ERR_CantFindRuntime");
844 }
845
846 // Strip everything after and including the "!"
847 classUrl = classUrl.substring(0, classUrl.lastIndexOf("!"));
848 // Strip everything after the last "/" or "\" to get rid of the jar filename
849 int lastIndexOfSlash = Math.max(classUrl.lastIndexOf("/"), classUrl.lastIndexOf("\\"));
850
851 return classUrl.substring(0, lastIndexOfSlash)
852 + "/../lib/jfxrt.jar!/";
853 }
854
855 private Class loadClassFromRuntime(String className) throws PackagerException {
856 try {
857 ClassLoader cl = getClassLoader();
858 return cl.loadClass(className);
859 } catch (ClassNotFoundException ex) {
860 throw new PackagerException(ex, "ERR_CantFindRuntime");
861 }
862 }
863
864 private void createBinaryCss(String cssFile, String binCssFile) throws PackagerException {
865 String ofname = (binCssFile != null)
866 ? binCssFile
867 : replaceExtensionByBSS(cssFile);
868
869 // create parent directories
870 File of = new File(ofname);
871 File parentFile = of.getParentFile();
872 if (parentFile != null) {
873 parentFile.mkdirs();
874 }
875
876 // Using reflection because CSS parser is part of runtime
877 // and we want to avoid dependency on jfxrt during build
878 Class<?> clazz;
879 try {
880 clazz = Class.forName("com.sun.javafx.css.parser.Css2Bin");
881 } catch (ClassNotFoundException e) {
882 // class was not found with default class loader, trying to
883 // locate it by loading from jfxrt.jar
884 clazz = loadClassFromRuntime("com.sun.javafx.css.parser.Css2Bin");
885 }
886
887 try {
888 Method m = clazz.getMethod("convertToBinary", String.class, String.class);
889 m.invoke(null, cssFile, ofname);
890 } catch (Exception ex) {
891 Throwable causeEx = ex.getCause();
892 String cause = (causeEx != null) ? causeEx.getMessage()
893 : bundle.getString("ERR_UnknownReason");
894
895 throw new PackagerException(ex, "ERR_BSSConversionFailed", cssFile, cause);
896 }
897 }
898
899 private static String replaceExtensionByBSS(String cssName) {
900 return cssName.substring(0, cssName.lastIndexOf(".") + 1).concat("bss");
901 }
902
903
904 private boolean isResource(String name) {
905 if (name.endsWith(".class")) {
906 return false;
907 }
908 if (name.endsWith(".java")) {
909 return false;
910 }
|
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.sun.javafx.tools.packager;
27
28 import com.oracle.tools.packager.Bundlers;
29 import com.oracle.tools.packager.ConfigException;
30 import com.oracle.tools.packager.Log;
31 import com.oracle.tools.packager.Platform;
32 import com.oracle.tools.packager.UnsupportedPlatformException;
33 import com.sun.javafx.tools.packager.JarSignature.InputStreamSource;
34 import com.sun.javafx.tools.packager.bundlers.BundleParams;
35 import com.sun.javafx.tools.packager.bundlers.Bundler.BundleType;
36 import com.sun.javafx.tools.resource.PackagerResource;
37 import com.sun.javafx.css.parser.Css2Bin;
38
39 import java.io.BufferedReader;
40 import java.io.File;
41 import java.io.FileInputStream;
42 import java.io.FileNotFoundException;
43 import java.io.FileOutputStream;
44 import java.io.FileWriter;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.InputStreamReader;
48 import java.io.OutputStream;
49 import java.io.Writer;
50 import java.nio.file.Files;
51 import java.nio.file.StandardCopyOption;
52 import java.security.InvalidKeyException;
53 import java.security.KeyStore;
54 import java.security.KeyStoreException;
55 import java.security.NoSuchAlgorithmException;
56 import java.security.PrivateKey;
57 import java.security.SignatureException;
58 import java.security.UnrecoverableKeyException;
59 import java.security.cert.Certificate;
60 import java.security.cert.CertificateException;
61 import java.security.cert.X509Certificate;
62 import java.text.MessageFormat;
63 import java.util.ArrayList;
64 import java.util.Base64;
65 import java.util.Enumeration;
66 import java.util.HashMap;
67 import java.util.HashSet;
68 import java.util.List;
69 import java.util.Map;
78 import java.util.zip.ZipEntry;
79 import java.util.zip.ZipOutputStream;
80
81 /**
82 * @deprecated use {@link ToolProvider} to locate the {@code "javapackager"} tool instead.
83 */
84 @Deprecated(since="10", forRemoval=true)
85 public class PackagerLib {
86 public static final String JAVAFX_VERSION = System.getProperty("java.version");
87
88 private static final ResourceBundle bundle =
89 ResourceBundle.getBundle("com/sun/javafx/tools/packager/Bundle");
90
91 private CreateJarParams createJarParams;
92 private CreateBSSParams createBssParams;
93 private File bssTmpDir;
94
95
96 private enum Filter {ALL, CLASSES_ONLY, RESOURCES}
97
98 // if set of input resources consist of SINGLE element and
99 // this element is jar file then we expect this to be request to
100 // "update" jar file
101 // Input jar file MUST be executable jar file
102 //
103 // Check if we are in "special case" scenario
104 private File jarFileToUpdate(CreateJarParams params) {
105 if (params.resources.size() == 1) {
106 PackagerResource p = params.resources.get(0);
107 File f = p.getFile();
108 if (!f.isFile() || !f.getAbsolutePath().toLowerCase().endsWith(".jar")) {
109 return null;
110 }
111 try (JarFile jf = new JarFile(f)) {
112 jf.getManifest(); //try to read manifest to validate it is jar
113 return f;
114 } catch (Exception e) {
115 //treat any exception as "not a special case" scenario
116 com.oracle.tools.packager.Log.verbose(e);
117 }
483 InputStreamSource in = () -> new FileInputStream(jar);
484 if (!signedJar.isFile()) {
485 signedJar.createNewFile();
486 }
487 FileOutputStream fos = new FileOutputStream(signedJar);
488 signature.signJarAsBLOB(in, new ZipOutputStream(fos));
489 }
490
491 public void makeAll(MakeAllParams makeAllParams) throws PackagerException {
492 final String exe = (Platform.getPlatform() == Platform.WINDOWS) ? ".exe" : "";
493 String jHome = System.getenv("JAVA_HOME");
494 if (jHome == null) {
495 jHome = System.getProperty("java.home");
496 }
497 if (jHome == null) {
498 throw new PackagerException("ERR_MissingJavaHome");
499 }
500
501 final File javac = new File(new File(jHome), "bin/javac" + exe);
502
503 final String srcDirName = "src";
504 final String compiledDirName = "compiled";
505 final String distDirName = "dist";
506 final String outfileName = "dist";
507 final String jarName = outfileName + ".jar";
508
509 final File distDir = new File(distDirName);
510
511 final File compiledDir = new File(compiledDirName);
512 compiledDir.mkdir();
513
514 try {
515 final File tmpFile = File.createTempFile("javac", "sources", new File("."));
516 tmpFile.deleteOnExit();
517 try (FileWriter sources = new FileWriter(tmpFile)) {
518 scanAndCopy(new PackagerResource(new File(srcDirName), "."), sources, compiledDir);
519 }
520
521 if (makeAllParams.verbose) {
522 Log.info("Executing javac:");
523 Log.infof("%s %s %s %s %s %s%n",
524 javac.getAbsolutePath(),
525 "-d", compiledDirName,
526 "-cp", makeAllParams.classpath != null ?
527 makeAllParams.classpath : "<null>",
528 "@" + tmpFile.getAbsolutePath());
529 }
530
531 int ret = -1;
532 if (makeAllParams.classpath != null) {
533 ret = execute(
534 javac.getAbsolutePath(),
535 "-d", compiledDirName,
536 "-cp", makeAllParams.classpath,
537 "@" + tmpFile.getAbsolutePath());
538 } else {
539 ret = execute(
540 javac.getAbsolutePath(),
541 "-d", compiledDirName,
542 "@" + tmpFile.getAbsolutePath());
543 }
544
545 if (ret != 0) {
546 throw new PackagerException("ERR_JavacFailed", Integer.toString(ret));
547 }
548 } catch (PackagerException e) {
549 throw e;
550 } catch (Exception e) {
551 throw new PackagerException(e, "ERR_MakeAllJavacFailed");
552 }
553
554 CreateJarParams cjp = new CreateJarParams();
555 cjp.applicationClass = makeAllParams.appClass;
556 cjp.preloader = makeAllParams.preloader;
557 cjp.classpath = makeAllParams.classpath;
558 cjp.css2bin = false;
559 cjp.outdir = distDir;
560 cjp.outfile = jarName;
561 cjp.addResource(compiledDir, ".");
562
563 packageAsJar(cjp);
564
799 }
800
801 private void createBinaryCss(File f, File outdir, String relPath)
802 throws PackagerException {
803 if (f.isDirectory()) {
804 File[] children = f.listFiles();
805 if (children != null) {
806 for (File innerFile : children) {
807 createBinaryCss(innerFile, outdir, relPath + '/' + innerFile.getName());
808 }
809 }
810 } else if (f.getName().endsWith(".css")) {
811 String cssFileName = f.getAbsolutePath();
812 String bssFileName = new File(outdir.getAbsolutePath(),
813 replaceExtensionByBSS(relPath))
814 .getAbsolutePath();
815 createBinaryCss(cssFileName, bssFileName);
816 }
817 }
818
819 private void createBinaryCss(String cssFile, String binCssFile) throws PackagerException {
820 String ofname = (binCssFile != null)
821 ? binCssFile
822 : replaceExtensionByBSS(cssFile);
823
824 // create parent directories
825 File of = new File(ofname);
826 File parentFile = of.getParentFile();
827 if (parentFile != null) {
828 parentFile.mkdirs();
829 }
830
831 try {
832 Css2Bin.convertToBinary(cssFile, ofname);
833 } catch (IOException ex) {
834 Throwable causeEx = ex.getCause();
835 String cause = (causeEx != null) ? causeEx.getMessage()
836 : bundle.getString("ERR_UnknownReason");
837
838 throw new PackagerException(ex, "ERR_BSSConversionFailed", cssFile, cause);
839 }
840 }
841
842 private static String replaceExtensionByBSS(String cssName) {
843 return cssName.substring(0, cssName.lastIndexOf(".") + 1).concat("bss");
844 }
845
846
847 private boolean isResource(String name) {
848 if (name.endsWith(".class")) {
849 return false;
850 }
851 if (name.endsWith(".java")) {
852 return false;
853 }
|