6
6
package io .flutter .jxbrowser ;
7
7
8
8
import com .google .common .annotations .VisibleForTesting ;
9
+ import com .intellij .openapi .application .ApplicationInfo ;
9
10
import com .intellij .openapi .application .PathManager ;
10
11
import com .intellij .openapi .diagnostic .Logger ;
11
12
import com .intellij .openapi .progress .ProgressIndicator ;
32
33
import java .io .File ;
33
34
import java .io .FileNotFoundException ;
34
35
import java .io .IOException ;
36
+ import java .lang .reflect .Constructor ;
37
+ import java .lang .reflect .InvocationTargetException ;
35
38
import java .nio .file .Path ;
36
39
import java .nio .file .Paths ;
37
40
import java .util .ArrayList ;
38
41
import java .util .Collections ;
39
42
import java .util .List ;
43
+ import java .util .Objects ;
40
44
import java .util .concurrent .CompletableFuture ;
41
45
import java .util .concurrent .ExecutionException ;
42
46
import java .util .concurrent .TimeUnit ;
51
55
public class JxBrowserManager {
52
56
private static JxBrowserManager manager ;
53
57
58
+ private static String getPluginLoaderDir () {
59
+ try {
60
+ final ApplicationInfo info = ApplicationInfo .getInstance ();
61
+ assert info != null ;
62
+ if (Objects .equals (info .getMajorVersion (), "2021" )) {
63
+ if (Objects .equals (info .getMinorVersion (), "3" )) {
64
+ return "flutter-idea" ;
65
+ }
66
+ else {
67
+ return "flutter-intellij" ;
68
+ }
69
+ }
70
+ else if (Objects .equals (info .getMajorVersion (), "2020" )) {
71
+ return "flutter-intellij" ;
72
+ }
73
+ } catch (NullPointerException ex ) {
74
+ // ignored; unit tests
75
+ }
76
+ return "flutter-idea" ;
77
+ }
78
+
54
79
@ NotNull
55
80
protected static final String DOWNLOAD_PATH =
56
- PathManager .getPluginsPath () + File .separatorChar + "flutter-intellij" + File .separatorChar + "jxbrowser" ;
81
+ PathManager .getPluginsPath () + File .separatorChar + getPluginLoaderDir () + File .separatorChar + "jxbrowser" ;
57
82
@ NotNull
58
83
private static final AtomicReference <JxBrowserStatus > status = new AtomicReference <>(JxBrowserStatus .NOT_INSTALLED );
59
84
@ NotNull
@@ -157,7 +182,8 @@ private void setStatusFailed(@NotNull InstallationFailedReason reason, @Nullable
157
182
158
183
if (time != null ) {
159
184
analytics .sendEventMetric (ANALYTICS_CATEGORY , eventName .toString (), time .intValue ());
160
- } else {
185
+ }
186
+ else {
161
187
analytics .sendEvent (ANALYTICS_CATEGORY , eventName .toString ());
162
188
}
163
189
@@ -283,12 +309,13 @@ public void run(@NotNull ProgressIndicator indicator) {
283
309
}
284
310
285
311
analytics .sendEvent (ANALYTICS_CATEGORY , "filesDownloaded" );
286
- loadClasses (fileNames );
312
+ loadClasses2021 (fileNames );
287
313
}
288
314
catch (IOException e ) {
289
315
final long elapsedTime = System .currentTimeMillis () - startTime ;
290
316
LOG .info (project .getName () + ": JxBrowser file downloaded failed: " + currentFileName );
291
- setStatusFailed (new InstallationFailedReason (FailureType .FILE_DOWNLOAD_FAILED , currentFileName + ":" + e .getMessage ()), elapsedTime );
317
+ setStatusFailed (new InstallationFailedReason (FailureType .FILE_DOWNLOAD_FAILED , currentFileName + ":" + e .getMessage ()),
318
+ elapsedTime );
292
319
}
293
320
}
294
321
};
@@ -298,28 +325,37 @@ public void run(@NotNull ProgressIndicator indicator) {
298
325
}
299
326
300
327
private void loadClasses (@ NotNull String [] fileNames ) {
301
- for (String fileName : fileNames ) {
302
- assert fileName != null ;
303
- final String fullPath = getFilePath (fileName );
304
-
328
+ final ClassLoader current = Thread .currentThread ().getContextClassLoader ();
329
+ try {
330
+ //noinspection ConstantConditions
331
+ Thread .currentThread ().setContextClassLoader (this .getClass ().getClassLoader ());
332
+ //noinspection ConstantConditions
333
+ for (String fileName : fileNames ) {
334
+ final String fullPath = getFilePath (fileName );
335
+ try {
336
+ //noinspection ConstantConditions
337
+ fileUtils .loadClass (this .getClass ().getClassLoader (), fullPath );
338
+ }
339
+ catch (Exception ex ) {
340
+ LOG .info ("Failed to load JxBrowser file" , ex );
341
+ setStatusFailed (new InstallationFailedReason (FailureType .CLASS_LOAD_FAILED ));
342
+ return ;
343
+ }
344
+ LOG .info ("Loaded JxBrowser file successfully: " + fullPath );
345
+ }
305
346
try {
306
- //noinspection ConstantConditions
307
- fileUtils .loadClass (this .getClass ().getClassLoader (), fullPath );
308
- } catch (Exception ex ) {
309
- LOG .info ("Failed to load JxBrowser file" , ex );
310
- setStatusFailed (new InstallationFailedReason (FailureType .CLASS_LOAD_FAILED ));
347
+ final Class <?> clazz = Class .forName ("com.teamdev.jxbrowser.browser.UnsupportedRenderingModeException" );
348
+ final Constructor <?> constructor = clazz .getConstructor (RenderingMode .class );
349
+ constructor .newInstance (RenderingMode .HARDWARE_ACCELERATED );
350
+ }
351
+ catch (NoClassDefFoundError | ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e ) {
352
+ LOG .info ("Failed to find JxBrowser class: " + e .getMessage ());
353
+ setStatusFailed (new InstallationFailedReason (FailureType .CLASS_NOT_FOUND ));
311
354
return ;
312
-
313
355
}
314
- LOG .info ("Loaded JxBrowser file successfully: " + fullPath );
315
356
}
316
- try {
317
- //noinspection ThrowableNotThrown
318
- final UnsupportedRenderingModeException test = new UnsupportedRenderingModeException (RenderingMode .HARDWARE_ACCELERATED );
319
- } catch (NoClassDefFoundError e ) {
320
- LOG .info ("Failed to find JxBrowser class" );
321
- setStatusFailed (new InstallationFailedReason (FailureType .CLASS_NOT_FOUND ));
322
- return ;
357
+ finally {
358
+ Thread .currentThread ().setContextClassLoader (current );
323
359
}
324
360
analytics .sendEvent (ANALYTICS_CATEGORY , "installed" );
325
361
status .set (JxBrowserStatus .INSTALLED );
@@ -328,27 +364,38 @@ private void loadClasses(@NotNull String[] fileNames) {
328
364
329
365
private void loadClasses2021 (@ NotNull String [] fileNames ) {
330
366
final List <Path > paths = new ArrayList <>();
331
-
367
+ final ClassLoader current = Thread . currentThread (). getContextClassLoader ();
332
368
try {
333
- for (String fileName : fileNames ) {
334
- assert fileName != null ;
335
- paths .add (Paths .get (getFilePath (fileName )));
369
+ Thread .currentThread ().setContextClassLoader (this .getClass ().getClassLoader ());
370
+ try {
371
+ for (String fileName : fileNames ) {
372
+ assert fileName != null ;
373
+ paths .add (Paths .get (getFilePath (fileName )));
374
+ }
375
+ //noinspection ConstantConditions
376
+ fileUtils .loadPaths (this .getClass ().getClassLoader (), paths );
336
377
}
337
- //noinspection ConstantConditions
338
- fileUtils .loadPaths (this .getClass ().getClassLoader (), paths );
339
- } catch (Exception ex ) {
340
- LOG .info ("Failed to load JxBrowser file" , ex );
341
- setStatusFailed (new InstallationFailedReason (FailureType .CLASS_LOAD_FAILED ));
342
- return ;
343
- }
378
+ catch (Exception ex ) {
379
+ LOG .info ("Failed to load JxBrowser file" , ex );
380
+ setStatusFailed (new InstallationFailedReason (FailureType .CLASS_LOAD_FAILED ));
381
+ return ;
382
+ }
383
+ LOG .info ("Loaded JxBrowser files successfully: " + paths );
344
384
345
- try {
346
- //noinspection ThrowableNotThrown
347
- final UnsupportedRenderingModeException test = new UnsupportedRenderingModeException (RenderingMode .HARDWARE_ACCELERATED );
348
- } catch (NoClassDefFoundError e ) {
349
- LOG .info ("Failed to find JxBrowser class" );
350
- setStatusFailed (new InstallationFailedReason (FailureType .CLASS_NOT_FOUND ));
351
- return ;
385
+ try {
386
+ final Class <?> clazz = Class .forName ("com.teamdev.jxbrowser.browser.UnsupportedRenderingModeException" );
387
+ final Constructor <?> constructor = clazz .getConstructor (RenderingMode .class );
388
+ constructor .newInstance (RenderingMode .HARDWARE_ACCELERATED );
389
+ //noinspection ThrowableNotThrown
390
+ final UnsupportedRenderingModeException test = new UnsupportedRenderingModeException (RenderingMode .HARDWARE_ACCELERATED );
391
+ }
392
+ catch (NoClassDefFoundError | ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e ) {
393
+ LOG .info ("Failed to find JxBrowser class: " , e );
394
+ setStatusFailed (new InstallationFailedReason (FailureType .CLASS_NOT_FOUND ));
395
+ return ;
396
+ }
397
+ } finally {
398
+ Thread .currentThread ().setContextClassLoader (current );
352
399
}
353
400
analytics .sendEvent (ANALYTICS_CATEGORY , "installed" );
354
401
status .set (JxBrowserStatus .INSTALLED );
0 commit comments