11package com .commonwealthrobotics ;
22
33import java .io .BufferedInputStream ;
4+ import java .io .BufferedReader ;
45import java .io .File ;
56import java .io .FileInputStream ;
67import java .io .FileNotFoundException ;
78import java .io .FileOutputStream ;
89import java .io .IOException ;
910import java .io .InputStream ;
11+ import java .io .InputStreamReader ;
1012import java .io .OutputStream ;
1113import java .lang .reflect .Type ;
1214import java .net .MalformedURLException ;
1315import java .net .URL ;
1416import java .net .URLConnection ;
1517import java .nio .charset .Charset ;
18+ import java .nio .charset .StandardCharsets ;
1619import java .nio .file .Files ;
1720import java .nio .file .Path ;
1821import java .nio .file .Paths ;
2124import java .util .HashMap ;
2225import java .util .List ;
2326import java .util .Map ;
24- import java .util .zip .ZipEntry ;
25- import java .util .zip .ZipFile ;
27+ import java .util .stream .Collectors ;
28+ //import java.util.zip.ZipEntry;
29+ //import java.util.zip.ZipFile;
2630
2731import org .apache .commons .compress .archivers .examples .Archiver ;
2832import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
2933import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
3034import org .apache .commons .compress .archivers .tar .TarUtils ;
35+ import org .apache .commons .compress .archivers .zip .ZipArchiveEntry ;
36+ import org .apache .commons .compress .archivers .zip .ZipFile ;
3137import org .apache .commons .compress .compressors .gzip .GzipCompressorInputStream ;
3238import org .apache .commons .compress .utils .IOUtils ;
3339import org .apache .commons .io .FilenameUtils ;
@@ -43,8 +49,11 @@ public class JvmManager {
4349
4450 public static String getCommandString (String project , String repo , String version , String downloadJsonURL ,
4551 long sizeOfJson , ProgressBar progress , String bindir ) throws Exception {
46-
47- File exe = download (version , downloadJsonURL , sizeOfJson , progress , bindir , "jvm.json" );
52+ if (version ==null )
53+ throw new RuntimeException ("Version can not be null" );
54+ File exe ;
55+
56+ exe = download (version , downloadJsonURL , sizeOfJson , progress , bindir , "jvm.json" );
4857 Type TT_mapStringString = new TypeToken <HashMap <String , Object >>() {
4958 }.getType ();
5059 // chreat the gson object, this is the parsing factory
@@ -53,28 +62,7 @@ public static String getCommandString(String project, String repo, String versio
5362
5463 HashMap <String , Object > database = gson .fromJson (jsonText , TT_mapStringString );
5564 String key = "UNKNOWN" ;
56- if (LatestFromGithubLaunchUI .isLin ()) {
57- if (LatestFromGithubLaunchUI .isArm ()) {
58- key = "Linux-aarch64" ;
59- } else {
60- key = "Linux-x64" ;
61- }
62- }
63-
64- if (LatestFromGithubLaunchUI .isMac ()) {
65- if (LatestFromGithubLaunchUI .isArm ()) {
66- key = "Mac-aarch64" ;
67- } else {
68- key = "Mac-x64" ;
69- }
70- }
71- if (LatestFromGithubLaunchUI .isWin ()) {
72- if (LatestFromGithubLaunchUI .isArm ()) {
73- key = "UNKNOWN" ;
74- } else {
75- key = "Windows-x64" ;
76- }
77- }
65+ key = discoverKey (key );
7866 Map <String , Object > vm = (Map <String , Object >) database .get (key );
7967 String baseURL = vm .get ("url" ).toString ();
8068 String type = vm .get ("type" ).toString ();
@@ -87,16 +75,16 @@ public static String getCommandString(String project, String repo, String versio
8775 jvmargs = new ArrayList <String >();
8876 String jvmURL = baseURL + name + "." + type ;
8977 File jvmArchive = download ("" , jvmURL , 185000000 , progress , bindir , name + "." + type );
90- File dest = new File (bindir + name );
91- if (!dest .exists ()) {
78+ File dest = new File (bindir + name );
79+ if (!dest .exists ()) {
9280 if (type .toLowerCase ().contains ("zip" )) {
9381 unzip (jvmArchive , bindir );
9482 }
9583 if (type .toLowerCase ().contains ("tar.gz" )) {
9684 untar (jvmArchive , bindir );
9785 }
98- }else {
99- System .out .println ("Not extraction, VM exists " + dest .getAbsolutePath ());
86+ } else {
87+ System .out .println ("Not extraction, VM exists " + dest .getAbsolutePath ());
10088 }
10189 String cmd = bindir + name + "/bin/java" + (LatestFromGithubLaunchUI .isWin () ? ".exe" : "" ) + " " ;
10290 for (String s : jvmargs ) {
@@ -105,24 +93,75 @@ public static String getCommandString(String project, String repo, String versio
10593 return cmd + " -jar " ;
10694 }
10795
96+ private static String discoverKey (String key ) {
97+ if (LatestFromGithubLaunchUI .isLin ()) {
98+ if (LatestFromGithubLaunchUI .isArm ()) {
99+ key = "Linux-aarch64" ;
100+ } else {
101+ key = "Linux-x64" ;
102+ }
103+ }
104+
105+ if (LatestFromGithubLaunchUI .isMac ()) {
106+ if (LatestFromGithubLaunchUI .isArm ()) {
107+ key = "Mac-aarch64" ;
108+ } else {
109+ key = "Mac-x64" ;
110+ }
111+ }
112+ if (LatestFromGithubLaunchUI .isWin ()) {
113+ if (LatestFromGithubLaunchUI .isArm ()) {
114+ key = "UNKNOWN" ;
115+ } else {
116+ key = "Windows-x64" ;
117+ }
118+ }
119+ return key ;
120+ }
121+
122+ public static boolean isExecutable (ZipArchiveEntry entry ) {
123+ int unixMode = entry .getUnixMode ();
124+ // Check if any of the executable bits are set for user, group, or others.
125+ // User executable: 0100 (0x40), Group executable: 0010 (0x10), Others
126+ // executable: 0001 (0x01)
127+ return (unixMode & 0x49 ) != 0 ;
128+ }
129+
108130 private static void unzip (File path , String dir ) throws Exception {
109131 String fileBaseName = FilenameUtils .getBaseName (path .getName ().toString ());
110132 Path destFolderPath = new File (dir ).toPath ();
111133
112- try (ZipFile zipFile = new ZipFile ( path , ZipFile . OPEN_READ , Charset . defaultCharset () )) {
113- Enumeration <? extends ZipEntry > entries = zipFile .entries ();
134+ try (ZipFile zipFile = ZipFile . builder (). setFile ( path ). get ( )) {
135+ Enumeration <ZipArchiveEntry > entries = zipFile .getEntries ();
114136 while (entries .hasMoreElements ()) {
115- ZipEntry entry = entries .nextElement ();
137+ ZipArchiveEntry entry = entries .nextElement ();
116138 Path entryPath = destFolderPath .resolve (entry .getName ());
117139 if (entryPath .normalize ().startsWith (destFolderPath .normalize ())) {
118140 if (entry .isDirectory ()) {
119141 Files .createDirectories (entryPath );
120142 } else {
121143 Files .createDirectories (entryPath .getParent ());
122144 try (InputStream in = zipFile .getInputStream (entry )) {
145+ try {
146+ // ar.setExternalAttributes(entry.extraAttributes);
147+ if (entry .isUnixSymlink ()) {
148+ String text = new BufferedReader (new InputStreamReader (in , StandardCharsets .UTF_8 ))
149+ .lines ().collect (Collectors .joining ("\n " ));
150+ Path target = Paths .get ("." , text );
151+ System .out .println ("Creating symlink " + entryPath + " with " + target );
152+
153+ Files .createSymbolicLink (entryPath , target );
154+ continue ;
155+ }
156+ } catch (Exception ex ) {
157+ ex .printStackTrace ();
158+ }
123159 try (OutputStream out = new FileOutputStream (entryPath .toFile ())) {
124160 IOUtils .copy (in , out );
125161 }
162+ if (isExecutable (entry )) {
163+ entryPath .toFile ().setExecutable (true );
164+ }
126165 }
127166 }
128167 }
@@ -141,7 +180,7 @@ private static void untar(File tarFile, String dir) throws Exception {
141180 // tarIn is a TarArchiveInputStream
142181 while (tarEntry != null ) {// create a file with the same name as the tarEntry
143182 File destPath = new File (dest .toString () + System .getProperty ("file.separator" ) + tarEntry .getName ());
144- //System.out.println("working: " + destPath.getCanonicalPath());
183+ // System.out.println("working: " + destPath.getCanonicalPath());
145184 if (tarEntry .isDirectory ()) {
146185 destPath .mkdirs ();
147186 } else {
@@ -152,54 +191,60 @@ private static void untar(File tarFile, String dir) throws Exception {
152191 fout .write (b );
153192 fout .close ();
154193 int mode = tarEntry .getMode ();
155- b = new byte [5 ];
194+ b = new byte [5 ];
156195 TarUtils .formatUnsignedOctalString (mode , b , 0 , 4 );
157- if (bits (b [1 ]).endsWith ("1" )) {
196+ if (bits (b [1 ]).endsWith ("1" )) {
158197 destPath .setExecutable (true );
159198 }
160199 }
161200 tarEntry = tarIn .getNextTarEntry ();
162201 }
163202 tarIn .close ();
164203 }
204+
165205 private static String bits (byte b ) {
166206 return String .format ("%6s" , Integer .toBinaryString (b & 0xFF )).replace (' ' , '0' );
167207 }
168208
169209 private static File download (String version , String downloadJsonURL , long sizeOfJson , ProgressBar progress ,
170210 String bindir , String filename ) throws MalformedURLException , IOException , FileNotFoundException {
171- URL url = new URL (downloadJsonURL );
172- URLConnection connection = url .openConnection ();
173- InputStream is = connection .getInputStream ();
174- ProcessInputStream pis = new ProcessInputStream (is , (int ) sizeOfJson );
175- pis .addListener (new Listener () {
176- @ Override
177- public void process (double percent ) {
178- System .out .println ("Download percent " + percent );
179- Platform .runLater (() -> {
180- progress .setProgress (percent );
181- });
182- }
183- });
184211 File folder = new File (bindir + version + "/" );
185212 File exe = new File (bindir + version + "/" + filename );
213+ try {
214+ URL url = new URL (downloadJsonURL );
215+ URLConnection connection = url .openConnection ();
216+ InputStream is = connection .getInputStream ();
217+ ProcessInputStream pis = new ProcessInputStream (is , (int ) sizeOfJson );
218+ pis .addListener (new Listener () {
219+ @ Override
220+ public void process (double percent ) {
221+ System .out .println ("Download percent " + percent );
222+ Platform .runLater (() -> {
223+ progress .setProgress (percent );
224+ });
225+ }
226+ });
186227
187- if (!folder .exists () || !exe .exists ()) {
188- System .out .println ("Start Downloading " + filename );
189- folder .mkdirs ();
190- exe .createNewFile ();
191- byte dataBuffer [] = new byte [1024 ];
192- int bytesRead ;
193- FileOutputStream fileOutputStream = new FileOutputStream (exe .getAbsoluteFile ());
194- while ((bytesRead = pis .read (dataBuffer , 0 , 1024 )) != -1 ) {
195- fileOutputStream .write (dataBuffer , 0 , bytesRead );
228+ if (!folder .exists () || !exe .exists ()) {
229+ System .out .println ("Start Downloading " + filename );
230+ folder .mkdirs ();
231+ exe .createNewFile ();
232+ byte dataBuffer [] = new byte [1024 ];
233+ int bytesRead ;
234+ FileOutputStream fileOutputStream = new FileOutputStream (exe .getAbsoluteFile ());
235+ while ((bytesRead = pis .read (dataBuffer , 0 , 1024 )) != -1 ) {
236+ fileOutputStream .write (dataBuffer , 0 , bytesRead );
237+ }
238+ fileOutputStream .close ();
239+ pis .close ();
240+ System .out .println ("Finished downloading " + filename );
241+ } else {
242+ System .out .println ("Not downloadeing, it existst " + filename );
196243 }
197- fileOutputStream .close ();
198- pis .close ();
199- System .out .println ("Finished downloading " + filename );
200- } else {
201- System .out .println ("Not downloadeing, it existst " + filename );
244+ } catch (Throwable t ) {
245+ t .printStackTrace ();
202246 }
247+ System .out .println ("Using JVM " +exe .getAbsolutePath ());
203248 return exe ;
204249 }
205250}
0 commit comments