44
44
*/
45
45
public final class AsyncProfilerLoader {
46
46
47
+ private static class OSNotSupportedException extends Exception {
48
+ OSNotSupportedException (String message ) {
49
+ super (message );
50
+ }
51
+ }
52
+
47
53
private static final String EXTRACTION_PROPERTY_NAME = "ap_loader_extraction_dir" ;
48
54
private static String librarySuffix ;
49
55
private static Path extractedAsyncProfiler ;
@@ -130,7 +136,7 @@ public static Path getExtractionDirectory() throws IOException {
130
136
/**
131
137
* @throws IllegalStateException if OS or Arch not supported
132
138
*/
133
- private static String getLibrarySuffix () {
139
+ private static String getLibrarySuffix () throws OSNotSupportedException {
134
140
if (librarySuffix == null ) {
135
141
String version = getVersion ();
136
142
String os = System .getProperty ("os.name" ).toLowerCase ();
@@ -145,12 +151,12 @@ private static String getLibrarySuffix() {
145
151
librarySuffix = version + "-linux-x64.so" ;
146
152
}
147
153
} else {
148
- throw new IllegalStateException ("Async-profiler does not work on Linux " + arch );
154
+ throw new OSNotSupportedException ("Async-profiler does not work on Linux " + arch );
149
155
}
150
156
} else if (os .startsWith ("macosx" ) || os .startsWith ("mac os x" )) {
151
157
librarySuffix = version + "-macos.so" ;
152
158
} else {
153
- throw new IllegalStateException ("Async-profiler does not work on " + os );
159
+ throw new OSNotSupportedException ("Async-profiler does not work on " + os );
154
160
}
155
161
}
156
162
return librarySuffix ;
@@ -210,11 +216,11 @@ private static boolean isOnMusl() {
210
216
}
211
217
}
212
218
213
- private static String getAsyncProfilerFileName () {
219
+ private static String getAsyncProfilerFileName () throws OSNotSupportedException {
214
220
return "libasyncProfiler-" + getLibrarySuffix ();
215
221
}
216
222
217
- private static String getJattachFileName () {
223
+ private static String getJattachFileName () throws OSNotSupportedException {
218
224
return "jattach-" + getLibrarySuffix ().replace (".so" , "" );
219
225
}
220
226
@@ -253,7 +259,11 @@ private static URL getUrl(String fileName) {
253
259
* @return true if a library is available, false otherwise
254
260
*/
255
261
public static boolean isSupported () {
256
- return hasFileInResources (getAsyncProfilerFileName ());
262
+ try {
263
+ return hasFileInResources (getAsyncProfilerFileName ());
264
+ } catch (OSNotSupportedException e ) {
265
+ return false ;
266
+ }
257
267
}
258
268
259
269
/** Copy from resources if needed */
@@ -287,8 +297,12 @@ private static Path copyFromResources(String fileName, Path destination) throws
287
297
*/
288
298
public static Path getJattachPath () throws IOException {
289
299
if (extractedJattach == null ) {
290
- Path path =
291
- copyFromResources (getJattachFileName (), getExtractionDirectory ().resolve ("jattach" ));
300
+ Path path = null ;
301
+ try {
302
+ path = copyFromResources (getJattachFileName (), getExtractionDirectory ().resolve ("jattach" ));
303
+ } catch (OSNotSupportedException e ) {
304
+ throw new IllegalStateException (e .getMessage ());
305
+ }
292
306
if (!path .toFile ().setExecutable (true )) {
293
307
throw new IOException ("Could not make jattach (" + path + ") executable" );
294
308
}
@@ -325,9 +339,13 @@ private static Path getProfilerPath() throws IOException {
325
339
*/
326
340
public static Path getAsyncProfilerPath () throws IOException {
327
341
if (extractedAsyncProfiler == null ) {
328
- extractedAsyncProfiler =
329
- copyFromResources (
330
- getAsyncProfilerFileName (), getExtractionDirectory ().resolve ("libasyncProfiler.so" ));
342
+ try {
343
+ extractedAsyncProfiler =
344
+ copyFromResources (
345
+ getAsyncProfilerFileName (), getExtractionDirectory ().resolve ("libasyncProfiler.so" ));
346
+ } catch (OSNotSupportedException e ) {
347
+ throw new IllegalStateException (e .getMessage ());
348
+ }
331
349
}
332
350
return extractedAsyncProfiler ;
333
351
}
0 commit comments