2323 */
2424package jenkins .plugins .openstack .compute .internal ;
2525
26- import java .io .File ;
27- import java .io .IOException ;
28- import java .nio .charset .Charset ;
29- import java .security .interfaces .RSAPublicKey ;
30- import java .util .ArrayList ;
31- import java .util .Collection ;
32- import java .util .Collections ;
33- import java .util .Comparator ;
34- import java .util .Date ;
35- import java .util .HashMap ;
36- import java .util .HashSet ;
37- import java .util .List ;
38- import java .util .Map ;
39- import java .util .NoSuchElementException ;
40- import java .util .Objects ;
41- import java .util .Optional ;
42- import java .util .TreeMap ;
43- import java .util .TreeSet ;
44- import java .util .concurrent .Callable ;
45- import java .util .concurrent .ExecutionException ;
46- import java .util .concurrent .TimeUnit ;
47- import java .util .logging .Level ;
48- import java .util .logging .Logger ;
49- import java .util .stream .Collectors ;
50-
51- import javax .annotation .CheckForNull ;
52- import javax .annotation .Nonnegative ;
53- import javax .annotation .Nonnull ;
54- import javax .annotation .concurrent .ThreadSafe ;
55-
5626import com .cloudbees .plugins .credentials .common .PasswordCredentials ;
27+ import com .github .benmanes .caffeine .cache .Cache ;
28+ import com .github .benmanes .caffeine .cache .Caffeine ;
5729import com .google .common .annotations .VisibleForTesting ;
58- import com .google .common .base .Charsets ;
59- import com .google .common .base .MoreObjects ;
60- import com .google .common .cache .Cache ;
61- import com .google .common .cache .CacheBuilder ;
62- import com .google .common .util .concurrent .UncheckedExecutionException ;
6330import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
6431import hudson .Extension ;
6532import hudson .ExtensionList ;
6633import hudson .ExtensionPoint ;
6734import hudson .Util ;
68- import hudson .remoting .Which ;
6935import hudson .util .FormValidation ;
36+ import jenkins .model .Jenkins ;
7037import jenkins .plugins .openstack .compute .auth .OpenstackCredential ;
38+ import org .apache .commons .codec .Charsets ;
7139import org .apache .commons .codec .binary .Base64 ;
7240import org .apache .commons .codec .digest .DigestUtils ;
7341import org .jenkinsci .main .modules .instance_identity .InstanceIdentity ;
7442import org .kohsuke .accmod .Restricted ;
7543import org .kohsuke .accmod .restrictions .NoExternalUse ;
7644import org .openstack4j .api .Builders ;
77- import org .openstack4j .api .networking .NetFloatingIPService ;
78- import org .openstack4j .api .networking .NetworkingService ;
79- import org .openstack4j .core .transport .Config ;
8045import org .openstack4j .api .OSClient ;
8146import org .openstack4j .api .client .IOSClientBuilder ;
8247import org .openstack4j .api .compute .ServerService ;
8348import org .openstack4j .api .exceptions .ResponseException ;
49+ import org .openstack4j .api .networking .NetFloatingIPService ;
50+ import org .openstack4j .api .networking .NetworkingService ;
51+ import org .openstack4j .core .transport .Config ;
8452import org .openstack4j .model .common .ActionResponse ;
8553import org .openstack4j .model .compute .Address ;
8654import org .openstack4j .model .compute .Fault ;
9462import org .openstack4j .model .image .v2 .Image ;
9563import org .openstack4j .model .network .NetFloatingIP ;
9664import org .openstack4j .model .network .Network ;
97- import org .openstack4j .model .network .Router ;
9865import org .openstack4j .model .network .Port ;
66+ import org .openstack4j .model .network .Router ;
9967import org .openstack4j .model .network .ext .NetworkIPAvailability ;
10068import org .openstack4j .model .network .options .PortListOptions ;
10169import org .openstack4j .model .storage .block .Volume ;
10270import org .openstack4j .model .storage .block .Volume .Status ;
10371import org .openstack4j .model .storage .block .VolumeSnapshot ;
10472import org .openstack4j .openstack .OSFactory ;
10573
106- import jenkins .model .Jenkins ;
74+ import javax .annotation .CheckForNull ;
75+ import javax .annotation .Nonnegative ;
76+ import javax .annotation .Nonnull ;
77+ import javax .annotation .concurrent .ThreadSafe ;
78+ import java .util .ArrayList ;
79+ import java .util .Collection ;
80+ import java .util .Collections ;
81+ import java .util .Comparator ;
82+ import java .util .Date ;
83+ import java .util .HashMap ;
84+ import java .util .HashSet ;
85+ import java .util .List ;
86+ import java .util .Map ;
87+ import java .util .NoSuchElementException ;
88+ import java .util .Objects ;
89+ import java .util .Optional ;
90+ import java .util .TreeMap ;
91+ import java .util .TreeSet ;
92+ import java .util .concurrent .TimeUnit ;
93+ import java .util .function .Function ;
94+ import java .util .logging .Level ;
95+ import java .util .logging .Logger ;
96+ import java .util .stream .Collectors ;
10797
10898/**
10999 * Encapsulate {@link OSClient}.
@@ -858,7 +848,7 @@ private static void debug(@Nonnull String msg, @Nonnull String... args) {
858848
859849 @ Restricted (NoExternalUse .class ) // Extension point just for testing
860850 public static abstract class FactoryEP implements ExtensionPoint {
861- private final transient @ Nonnull Cache <String , Openstack > cache = CacheBuilder .newBuilder ()
851+ private final transient @ Nonnull Cache <String , Openstack > cache = Caffeine .newBuilder ()
862852 // There is no clear reasoning behind particular expiration policy except that individual instances can
863853 // have different token expiration time, which is something guava does not support. This expiration needs
864854 // to be implemented separately.
@@ -885,12 +875,18 @@ public static abstract class FactoryEP implements ExtensionPoint {
885875 + (auth instanceof PasswordCredentials ? ((PasswordCredentials ) auth ).getPassword ().getEncryptedValue () + '\n' : "" )
886876 + region );
887877 final FactoryEP ep = ExtensionList .lookup (FactoryEP .class ).get (0 );
888- final Callable <Openstack > cacheMissFunction = () -> ep .getOpenstack (endPointUrl , ignoreSsl , auth , region );
878+ final Function <String , Openstack > cacheMissFunction = (String unused ) -> {
879+ try {
880+ return ep .getOpenstack (endPointUrl , ignoreSsl , auth , region );
881+ } catch (FormValidation ex ) {
882+ throw new RuntimeException (ex );
883+ }
884+ };
889885
890- // Get an instance, creating a new one if necessary.
891886 try {
892- return ep .cache .get (fingerprint , cacheMissFunction );
893- } catch (UncheckedExecutionException | ExecutionException e ) {
887+ // cacheMissFunction is guaranteed to return nonnull
888+ return Objects .requireNonNull (ep .cache .get (fingerprint , cacheMissFunction ));
889+ } catch (RuntimeException e ) { // Propagated from cacheMissFunction
894890 // Exception was thrown when creating a new instance.
895891 final Throwable cause = e .getCause ();
896892 if (cause instanceof FormValidation ) {
@@ -899,7 +895,7 @@ public static abstract class FactoryEP implements ExtensionPoint {
899895 if (cause instanceof RuntimeException ) {
900896 throw (RuntimeException ) cause ;
901897 }
902- throw new RuntimeException ( e ) ;
898+ throw e ;
903899 }
904900 }
905901
@@ -1012,16 +1008,4 @@ private SessionClientV3Provider(OSClient.OSClientV3 toStore, String usedRegion,
10121008 }
10131009 }
10141010 }
1015-
1016- static {
1017- // Log where guava is coming from. This can not be reliably tested as jenkins-test-harness, hpi:run and actual
1018- // jenkins deployed plugin have different classloader environments. Messing around with maven-hpi-plugin opts can
1019- // fix or break any of that and there is no regression test to catch that.
1020- try {
1021- File path = Which .jarFile (MoreObjects .ToStringHelper .class );
1022- LOGGER .info ("com.google.common.base.Objects loaded from " + path );
1023- } catch (IOException e ) {
1024- LOGGER .log (Level .WARNING , "Unable to get source of com.google.common.base.Objects" , e );
1025- }
1026- }
10271011}
0 commit comments