1414import org .apache .commons .io .FileUtils ;
1515import org .apache .logging .log4j .LogManager ;
1616import org .apache .logging .log4j .Logger ;
17+ import qz .build .jlink .Arch ;
18+ import qz .build .jlink .Platform ;
19+ import qz .build .jlink .Vendor ;
20+ import qz .build .jlink .Url ;
1721import qz .common .Constants ;
1822import qz .utils .*;
1923
2226import java .nio .file .*;
2327import java .util .HashMap ;
2428import java .util .LinkedHashSet ;
25- import java .util .Locale ;
2629
2730public class JLink {
2831 private static final Logger log = LogManager .getLogger (JLink .class );
29- private static final String JAVA_VENDOR = "BellSoft" ;
30- private static final String JAVA_VERSION = "11.0.17+7" ;
31- private static final String JAVA_MAJOR = JAVA_VERSION .split ("\\ ." )[0 ];
32- private static final String JAVA_MINOR = JAVA_VERSION .split ("\\ ." )[1 ];
33- private static final String JAVA_PATCH = JAVA_VERSION .split ("\\ .|\\ +|-" )[2 ];
34- private static final String JAVA_VERSION_FILE = JAVA_VERSION .replaceAll ("\\ +" , "_" );
35- private static final String JAVA_DEFAULT_GC_ENGINE = "hotspot" ;
36- private static final String JAVA_DEFAULT_ARCH = VendorArch .ADOPT_AMD64 .use ;
32+ public static final Vendor JAVA_DEFAULT_VENDOR = Vendor .BELLSOFT ;
33+ private static final String JAVA_DEFAULT_VERSION = "11.0.17+7" ;
34+ private static final String JAVA_DEFAULT_GC_ENGINE = "hotspot" ; // or "openj9"
35+ private static final String JAVA_DEFAULT_GC_VERSION = "0.35.0" ; // openj9 gc only
3736
3837 private Path jarPath ;
3938 private Path jdepsPath ;
4039 private Path jlinkPath ;
4140 private Path jmodsPath ;
4241 private Path outPath ;
4342 private Version jdepsVersion ;
44- private String javaVendor ;
45- private String targetPlatform ;
43+ private Platform hostPlatform ;
44+ private Platform targetPlatform ;
45+ private Arch hostArch ;
46+ private Arch targetArch ;
47+ private Vendor javaVendor ;
48+ private String gcEngine ;
49+ private String javaVersion ;
50+ private String gcVersion ;
51+
52+ private Version javaSemver ;
53+
4654 private LinkedHashSet <String > depList ;
4755
48- public JLink (String targetPlatform , String arch , String gcEngine ) throws IOException {
49- this .javaVendor = JAVA_VENDOR ;
50- this .targetPlatform = targetPlatform ;
56+ public JLink (String targetPlatform , String targetArch , String javaVendor , String javaVersion , String gcEngine , String gcVersion ) throws IOException {
57+ this .hostPlatform = Platform .getCurrentPlatform ();
58+ this .hostArch = Arch .getCurrentArch ();
59+
60+ this .targetPlatform = Platform .parse (targetPlatform , this .hostPlatform );
61+ this .targetArch = Arch .parse (targetArch , this .hostArch );
62+ this .javaVendor = Vendor .parse (javaVendor , JAVA_DEFAULT_VENDOR );
63+ this .gcEngine = getParam ("gcEngine" , gcEngine , JAVA_DEFAULT_GC_ENGINE );
64+ this .javaVersion = getParam ("javaVersion" , javaVersion , JAVA_DEFAULT_VERSION );
65+ this .gcVersion = getParam ("gcVersion" , gcVersion , JAVA_DEFAULT_GC_VERSION );
66+
67+ this .javaSemver = SystemUtilities .getJavaVersion (this .javaVersion );
5168
52- if (needsDownload (SystemUtilities .getJavaVersion (JAVA_VERSION ), Constants .JAVA_VERSION )) {
53- log .warn ("Java versions are incompatible, locating a suitable runtime for Java " + JAVA_MAJOR + "..." );
54- String hostArch = System .getProperty ("os.arch" );
55- String hostJdk = downloadJdk (JAVA_VENDOR , null , hostArch , gcEngine );
69+ if (needsDownload (javaSemver , Constants .JAVA_VERSION )) {
70+ log .warn ("Java versions are incompatible, locating a suitable runtime for Java " + javaSemver .getMajorVersion () + "..." );
71+ String hostJdk = downloadJdk (this .hostArch , this .hostPlatform );
5672 calculateToolPaths (Paths .get (hostJdk ));
5773 } else {
5874 calculateToolPaths (null );
5975 }
6076
61- String extractedJdk = downloadJdk (javaVendor , targetPlatform , arch , gcEngine );
62- jmodsPath = Paths .get (extractedJdk , "jmods" );
77+ String targetJdk = downloadJdk (this . targetArch , this . targetPlatform );
78+ jmodsPath = Paths .get (targetJdk , "jmods" );
6379 log .info ("Selecting jmods folder: {}" , jmodsPath );
6480
6581 calculateJarPath ()
@@ -69,16 +85,7 @@ public JLink(String targetPlatform, String arch, String gcEngine) throws IOExcep
6985 }
7086
7187 public static void main (String ... args ) throws IOException {
72- JLink jlink = new JLink (null , null , null ).calculateJarPath ();
73- System .out .println (jlink .jarPath );
74- if (true ) {
75- System .exit (0 );
76- }
77-
78-
79- new JLink (args .length > 0 ? args [0 ] : null ,
80- args .length > 1 ? args [1 ] : null ,
81- args .length > 2 ? args [2 ] : null );
88+ new JLink (null , null , null , null , null , null ).calculateJarPath ();
8289 }
8390
8491 /**
@@ -103,53 +110,18 @@ private static boolean needsDownload(Version want, Version installed) {
103110 /**
104111 * Download the JDK and return the path it was extracted to
105112 */
106- private static String downloadJdk (String javaVendor , String platform , String arch , String gcEngine ) throws IOException {
107- if (platform == null ) {
108- // Must match ArgValue.JLINK --platform values
109- switch (SystemUtilities .getOsType ()) {
110- case MAC :
111- platform = "mac" ;
112- break ;
113- case WINDOWS :
114- platform = "windows" ;
115- break ;
116- default :
117- platform = "linux" ;
118- }
119-
120- log .info ("No platform specified, assuming '{}'" , platform );
121- }
122-
123- arch = VendorArch .match (javaVendor , arch , JAVA_DEFAULT_ARCH );
124- platform = VendorOs .match (javaVendor , platform );
125-
126-
127- if (gcEngine == null ) {
128- gcEngine = JAVA_DEFAULT_GC_ENGINE ;
129- log .info ("No garbage collector specified, assuming '{}'" , gcEngine );
130- }
131-
132- String fileExt ;
133- switch (VendorUrlPattern .getVendor (javaVendor )) {
134- case BELL :
135- fileExt = platform .equals ("linux" ) ? "tar.gz" : "zip" ;
136- break ;
137- case ADOPT :
138- default :
139- fileExt = platform .equals ("windows" ) ? "zip" : "tar.gz" ;
140- }
141-
142- String url = VendorUrlPattern .format (javaVendor , arch , platform , gcEngine , JAVA_MAJOR , JAVA_VERSION , JAVA_VERSION_FILE , fileExt );
113+ private String downloadJdk (Arch arch , Platform platform ) throws IOException {
114+ String url = new Url (this .javaVendor ).format (arch , platform , this .gcEngine , this .javaSemver , this .gcVersion );
143115
144116 // Saves to out e.g. "out/jlink/jdk-AdoptOpenjdk-amd64-platform-11_0_7"
145- String extractedJdk = new Fetcher (String .format ("jlink/jdk-%s-%s-%s-%s" , javaVendor .toLowerCase ( Locale . ENGLISH ), arch , platform , JAVA_VERSION_FILE ), url )
117+ String extractedJdk = new Fetcher (String .format ("jlink/jdk-%s-%s-%s-%s" , javaVendor .value ( ), arch . value () , platform . value (), javaSemver . toString (). replaceAll ( " \\ +" , "_" ) ), url )
146118 .fetch ()
147119 .uncompress ();
148120
149121 // Get first subfolder, e.g. jdk-11.0.7+10
150122 for (File subfolder : new File (extractedJdk ).listFiles (pathname -> pathname .isDirectory ())) {
151123 extractedJdk = subfolder .getPath ();
152- if (platform . equals ( "mac" ) && Paths .get (extractedJdk , "/Contents/Home" ).toFile ().isDirectory ()) {
124+ if (platform == Platform . MAC && Paths .get (extractedJdk , "/Contents/Home" ).toFile ().isDirectory ()) {
153125 extractedJdk += "/Contents/Home" ;
154126 }
155127 log .info ("Selecting JDK home: {}" , extractedJdk );
@@ -165,18 +137,20 @@ private JLink calculateJarPath() {
165137 } else {
166138 // Detect out/dist/qz-tray.jar for IDE usage
167139 jarPath = SystemUtilities .getJarParentPath ()
168- .resolve ("../../ " )
140+ .resolve ("dist " )
169141 .resolve (Constants .PROPS_FILE + ".jar" );
170142 }
171143 log .info ("Assuming jar path: {}" , jarPath );
172144 return this ;
173145 }
174146
175147 private JLink calculateOutPath () {
176- if (targetPlatform .equals ("mac" )) {
177- outPath = jarPath .resolve ("../Java.runtime/Contents/Home" ).normalize ();
178- } else {
179- outPath = jarPath .resolve ("../runtime" ).normalize ();
148+ switch (targetPlatform ) {
149+ case MAC :
150+ outPath = jarPath .resolve ("../Java.runtime/Contents/Home" ).normalize ();
151+ break ;
152+ default :
153+ outPath = jarPath .resolve ("../runtime" ).normalize ();
180154 }
181155 log .info ("Assuming output path: {}" , outPath );
182156 return this ;
@@ -231,7 +205,7 @@ private JLink calculateDepList() throws IOException {
231205 }
232206
233207 private JLink deployJre () throws IOException {
234- if (targetPlatform . equals ( "mac" ) ) {
208+ if (targetPlatform == Platform . MAC ) {
235209 // Deploy Contents/MacOS/libjli.dylib
236210 Path macOS = Files .createDirectories (outPath .resolve ("../MacOS" ).normalize ());
237211 Path jliLib = macOS .resolve ("libjli.dylib" );
@@ -247,9 +221,10 @@ private JLink deployJre() throws IOException {
247221 // Deploy Contents/Info.plist
248222 HashMap <String , String > fieldMap = new HashMap <>();
249223 fieldMap .put ("%BUNDLE_ID%" , MacUtilities .getBundleId () + ".jre" ); // e.g. io.qz.qz-tray.jre
250- fieldMap .put ("%BUNDLE_VERSION%" , String .format ("%s.%s.%s" , JAVA_MAJOR , JAVA_MINOR , JAVA_PATCH ));
251- fieldMap .put ("%BUNDLE_VERSION_FULL%" , JAVA_VERSION );
252- fieldMap .put ("%BUNDLE_VENDOR%" , javaVendor );
224+ fieldMap .put ("%BUNDLE_VERSION%" , String .format ("%s.%s.%s" , javaSemver .getMajorVersion (), javaSemver .getMinorVersion (), javaSemver .getPatchVersion ()));
225+ fieldMap .put ("%BUNDLE_VERSION_FULL%" , javaSemver .toString ());
226+ fieldMap .put ("%BUNDLE_VENDOR%" , javaVendor .getVendorName ());
227+ fieldMap .put ("%BUNDLE_PRODUCT%" , javaVendor .getProductName ());
253228 log .info ("Deploying {}/Info.plist" , macOS .getParent ());
254229 FileUtilities .configureAssetFile ("assets/mac-runtime.plist.in" , macOS .getParent ().resolve ("Info.plist" ), fieldMap , JLink .class );
255230 }
@@ -270,13 +245,15 @@ private JLink deployJre() throws IOException {
270245 // Remove all but java/javaw
271246 String [] keepFiles ;
272247 String keepExt ;
273- if (targetPlatform .equals ("windows" )) {
274- keepFiles = new String []{ "java.exe" , "javaw.exe" };
275- // Windows stores ".dll" files in bin
276- keepExt = ".dll" ;
277- } else {
278- keepFiles = new String []{ "java" };
279- keepExt = null ;
248+ switch (targetPlatform ) {
249+ case WINDOWS :
250+ keepFiles = new String []{ "java.exe" , "javaw.exe" };
251+ // Windows stores ".dll" files in bin
252+ keepExt = ".dll" ;
253+ break ;
254+ default :
255+ keepFiles = new String []{ "java" };
256+ keepExt = null ;
280257 }
281258
282259 Files .list (outPath .resolve ("bin" )).forEach (binFile -> {
@@ -300,4 +277,11 @@ private JLink deployJre() throws IOException {
300277 throw new IOException ("An error occurred deploying the jre. Please check the logs for details." );
301278 }
302279
280+ public static String getParam (String paramName , String value , String fallback ) {
281+ if (value != null && !value .isEmpty () && !value .trim ().isEmpty ()) {
282+ return value ;
283+ }
284+ log .info ("No {} specified, assuming '{}'" , paramName , fallback );
285+ return fallback ;
286+ }
303287}
0 commit comments