1
1
package com .commonwealthrobotics ;
2
2
3
3
import java .io .BufferedInputStream ;
4
+ import java .io .BufferedReader ;
4
5
import java .io .File ;
5
6
import java .io .FileInputStream ;
6
7
import java .io .FileNotFoundException ;
7
8
import java .io .FileOutputStream ;
8
9
import java .io .IOException ;
9
10
import java .io .InputStream ;
11
+ import java .io .InputStreamReader ;
10
12
import java .io .OutputStream ;
11
13
import java .lang .reflect .Type ;
12
14
import java .net .MalformedURLException ;
13
15
import java .net .URL ;
14
16
import java .net .URLConnection ;
15
17
import java .nio .charset .Charset ;
18
+ import java .nio .charset .StandardCharsets ;
16
19
import java .nio .file .Files ;
17
20
import java .nio .file .Path ;
18
21
import java .nio .file .Paths ;
21
24
import java .util .HashMap ;
22
25
import java .util .List ;
23
26
import 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;
26
30
27
31
import org .apache .commons .compress .archivers .examples .Archiver ;
28
32
import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
29
33
import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
30
34
import 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 ;
31
37
import org .apache .commons .compress .compressors .gzip .GzipCompressorInputStream ;
32
38
import org .apache .commons .compress .utils .IOUtils ;
33
39
import org .apache .commons .io .FilenameUtils ;
@@ -43,8 +49,11 @@ public class JvmManager {
43
49
44
50
public static String getCommandString (String project , String repo , String version , String downloadJsonURL ,
45
51
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" );
48
57
Type TT_mapStringString = new TypeToken <HashMap <String , Object >>() {
49
58
}.getType ();
50
59
// chreat the gson object, this is the parsing factory
@@ -53,28 +62,7 @@ public static String getCommandString(String project, String repo, String versio
53
62
54
63
HashMap <String , Object > database = gson .fromJson (jsonText , TT_mapStringString );
55
64
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 );
78
66
Map <String , Object > vm = (Map <String , Object >) database .get (key );
79
67
String baseURL = vm .get ("url" ).toString ();
80
68
String type = vm .get ("type" ).toString ();
@@ -87,16 +75,16 @@ public static String getCommandString(String project, String repo, String versio
87
75
jvmargs = new ArrayList <String >();
88
76
String jvmURL = baseURL + name + "." + type ;
89
77
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 ()) {
92
80
if (type .toLowerCase ().contains ("zip" )) {
93
81
unzip (jvmArchive , bindir );
94
82
}
95
83
if (type .toLowerCase ().contains ("tar.gz" )) {
96
84
untar (jvmArchive , bindir );
97
85
}
98
- }else {
99
- System .out .println ("Not extraction, VM exists " + dest .getAbsolutePath ());
86
+ } else {
87
+ System .out .println ("Not extraction, VM exists " + dest .getAbsolutePath ());
100
88
}
101
89
String cmd = bindir + name + "/bin/java" + (LatestFromGithubLaunchUI .isWin () ? ".exe" : "" ) + " " ;
102
90
for (String s : jvmargs ) {
@@ -105,24 +93,75 @@ public static String getCommandString(String project, String repo, String versio
105
93
return cmd + " -jar " ;
106
94
}
107
95
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
+
108
130
private static void unzip (File path , String dir ) throws Exception {
109
131
String fileBaseName = FilenameUtils .getBaseName (path .getName ().toString ());
110
132
Path destFolderPath = new File (dir ).toPath ();
111
133
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 ();
114
136
while (entries .hasMoreElements ()) {
115
- ZipEntry entry = entries .nextElement ();
137
+ ZipArchiveEntry entry = entries .nextElement ();
116
138
Path entryPath = destFolderPath .resolve (entry .getName ());
117
139
if (entryPath .normalize ().startsWith (destFolderPath .normalize ())) {
118
140
if (entry .isDirectory ()) {
119
141
Files .createDirectories (entryPath );
120
142
} else {
121
143
Files .createDirectories (entryPath .getParent ());
122
144
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
+ }
123
159
try (OutputStream out = new FileOutputStream (entryPath .toFile ())) {
124
160
IOUtils .copy (in , out );
125
161
}
162
+ if (isExecutable (entry )) {
163
+ entryPath .toFile ().setExecutable (true );
164
+ }
126
165
}
127
166
}
128
167
}
@@ -141,7 +180,7 @@ private static void untar(File tarFile, String dir) throws Exception {
141
180
// tarIn is a TarArchiveInputStream
142
181
while (tarEntry != null ) {// create a file with the same name as the tarEntry
143
182
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());
145
184
if (tarEntry .isDirectory ()) {
146
185
destPath .mkdirs ();
147
186
} else {
@@ -152,54 +191,60 @@ private static void untar(File tarFile, String dir) throws Exception {
152
191
fout .write (b );
153
192
fout .close ();
154
193
int mode = tarEntry .getMode ();
155
- b = new byte [5 ];
194
+ b = new byte [5 ];
156
195
TarUtils .formatUnsignedOctalString (mode , b , 0 , 4 );
157
- if (bits (b [1 ]).endsWith ("1" )) {
196
+ if (bits (b [1 ]).endsWith ("1" )) {
158
197
destPath .setExecutable (true );
159
198
}
160
199
}
161
200
tarEntry = tarIn .getNextTarEntry ();
162
201
}
163
202
tarIn .close ();
164
203
}
204
+
165
205
private static String bits (byte b ) {
166
206
return String .format ("%6s" , Integer .toBinaryString (b & 0xFF )).replace (' ' , '0' );
167
207
}
168
208
169
209
private static File download (String version , String downloadJsonURL , long sizeOfJson , ProgressBar progress ,
170
210
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
- });
184
211
File folder = new File (bindir + version + "/" );
185
212
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
+ });
186
227
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 );
196
243
}
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 ();
202
246
}
247
+ System .out .println ("Using JVM " +exe .getAbsolutePath ());
203
248
return exe ;
204
249
}
205
250
}
0 commit comments