1717import java .util .List ;
1818import java .util .concurrent .TimeUnit ;
1919import java .util .concurrent .locks .ReentrantReadWriteLock ;
20+ import java .util .logging .Level ;
21+ import java .util .logging .Logger ;
22+
2023import org .apache .commons .io .IOUtils ;
2124import org .apache .commons .lang .StringUtils ;
2225
2326public final class LibraryCachingConfiguration extends AbstractDescribableImpl <LibraryCachingConfiguration > {
27+
28+ private static final Logger LOGGER = Logger .getLogger (LibraryCachingConfiguration .class .getName ());
29+
2430 private int refreshTimeMinutes ;
2531 private String excludedVersionsStr ;
2632
@@ -83,7 +89,7 @@ public static FilePath getGlobalLibrariesCacheDir() {
8389 }
8490
8591 @ Extension public static class DescriptorImpl extends Descriptor <LibraryCachingConfiguration > {
86- public FormValidation doClearCache (@ QueryParameter String name ) throws InterruptedException {
92+ public FormValidation doClearCache (@ QueryParameter String name , @ QueryParameter boolean forceDelete ) throws InterruptedException {
8793 Jenkins .get ().checkPermission (Jenkins .ADMINISTER );
8894
8995 try {
@@ -94,19 +100,26 @@ public FormValidation doClearCache(@QueryParameter String name) throws Interrupt
94100 try (InputStream stream = libraryNamePath .read ()) {
95101 cacheName = IOUtils .toString (stream , StandardCharsets .UTF_8 );
96102 }
97- if (libraryNamePath . readToString () .equals (name )) {
103+ if (cacheName .equals (name )) {
98104 FilePath libraryCachePath = LibraryCachingConfiguration .getGlobalLibrariesCacheDir ()
99105 .child (libraryNamePath .getName ().replace ("-name.txt" , "" ));
100- ReentrantReadWriteLock retrieveLock = LibraryAdder .getReadWriteLockFor (libraryCachePath .getName ());
101- if (retrieveLock .writeLock ().tryLock (10 , TimeUnit .SECONDS )) {
102- try {
103- libraryCachePath .deleteRecursive ();
104- libraryNamePath .delete ();
105- } finally {
106- retrieveLock .writeLock ().unlock ();
107- }
106+ if (forceDelete ) {
107+ LOGGER .log (Level .FINER , "Force deleting cache for {0}" , name );
108+ libraryCachePath .deleteRecursive ();
109+ libraryNamePath .delete ();
108110 } else {
109- return FormValidation .error ("The cache dir is currently used by another thread, so deletion was not possibly. Please try again" );
111+ LOGGER .log (Level .FINER , "Safe deleting cache for {0}" , name );
112+ ReentrantReadWriteLock retrieveLock = LibraryAdder .getReadWriteLockFor (libraryCachePath .getName ());
113+ if (retrieveLock .writeLock ().tryLock (10 , TimeUnit .SECONDS )) {
114+ try {
115+ libraryCachePath .deleteRecursive ();
116+ libraryNamePath .delete ();
117+ } finally {
118+ retrieveLock .writeLock ().unlock ();
119+ }
120+ } else {
121+ return FormValidation .error ("The cache dir could not be deleted because it is currently being used by another thread. Please try again." );
122+ }
110123 }
111124 }
112125 }
0 commit comments