35
35
import java .net .URLClassLoader ;
36
36
import java .util .logging .Level ;
37
37
import java .util .logging .Logger ;
38
+ import org .apache .ivy .core .cache .DefaultRepositoryCacheManager ;
39
+ import org .apache .ivy .core .cache .RepositoryCacheManager ;
40
+ import org .apache .ivy .core .settings .IvySettings ;
41
+ import org .apache .ivy .plugins .lock .LockStrategy ;
42
+ import org .apache .ivy .plugins .lock .NoLockStrategy ;
38
43
39
44
public class GrapeHack {
40
45
41
46
private static final Logger LOGGER = Logger .getLogger (GrapeHack .class .getName ());
42
47
48
+ @ SuppressFBWarnings (value = "MS_SHOULD_BE_FINAL" , justification = "for script console" )
49
+ public static boolean DISABLE_NIO_FILE_LOCK =
50
+ Boolean .getBoolean (GrapeHack .class .getName () + ".DISABLE_NIO_FILE_LOCK" );
51
+
43
52
@ SuppressFBWarnings (value ="DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED" , justification ="the least of our concerns" )
44
53
@ Initializer (after =InitMilestone .PLUGINS_PREPARED , fatal =false )
45
54
public static void hack () throws Exception {
@@ -63,6 +72,32 @@ public static void hack() throws Exception {
63
72
l = engine .getClass ().getClassLoader ();
64
73
LOGGER .log (Level .FINE , "was also able to load {0}" , l .loadClass (ivyGrabRecordName ));
65
74
LOGGER .log (Level .FINE , "linked to {0}" , l .loadClass ("org.apache.ivy.core.module.id.ModuleRevisionId" ).getProtectionDomain ().getCodeSource ().getLocation ());
75
+ if (!DISABLE_NIO_FILE_LOCK ) {
76
+ try {
77
+ /*
78
+ * We must use reflection instead of simply casting to GrapeIvy and invoking directly due to the use of
79
+ * MaskingClassLoader a few lines above.
80
+ */
81
+ IvySettings settings = (IvySettings ) c .getMethod ("getSettings" ).invoke (instance .get (null ));
82
+ RepositoryCacheManager repositoryCacheManager = settings .getDefaultRepositoryCacheManager ();
83
+ if (repositoryCacheManager instanceof DefaultRepositoryCacheManager ) {
84
+ DefaultRepositoryCacheManager defaultRepositoryCacheManager =
85
+ (DefaultRepositoryCacheManager ) repositoryCacheManager ;
86
+ LockStrategy lockStrategy = defaultRepositoryCacheManager .getLockStrategy ();
87
+ LOGGER .log (Level .FINE , "default lock strategy {0}" , lockStrategy );
88
+ if (lockStrategy == null || lockStrategy instanceof NoLockStrategy ) {
89
+ lockStrategy = settings .getLockStrategy ("artifact-lock-nio" );
90
+ if (lockStrategy != null ) {
91
+ defaultRepositoryCacheManager .setLockStrategy (lockStrategy .getName ());
92
+ defaultRepositoryCacheManager .setLockStrategy (lockStrategy );
93
+ }
94
+ }
95
+ LOGGER .log (Level .FINE , "using lock strategy {0}" , defaultRepositoryCacheManager .getLockStrategy ());
96
+ }
97
+ } catch (RuntimeException | LinkageError x ) {
98
+ LOGGER .log (Level .FINE , "failed to enable NIO file lock" , x );
99
+ }
100
+ }
66
101
}
67
102
68
103
private GrapeHack () {}
0 commit comments