This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 4.0.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit c8ddae921bcd551f1aeb4982e70a9baa8fab140a
Author: Andriy Redko <[email protected]>
AuthorDate: Wed Mar 12 16:43:32 2025 -0400

    CXF-9117: cxf-rt-ws-security fails if JCache is not on the classpath (#2318)
    
    (cherry picked from commit 7d391f7c8009f2ff083c22dfa41d8a0bd531a79c)
---
 .../security/cache/jcache/JCacheReplayCache.java   |  4 +--
 .../ws/security/tokenstore/TokenStoreFactory.java  |  4 +--
 .../tokenstore/jcache/JCacheTokenStore.java        |  4 +--
 .../utils/{JCacheUtils.java => JCacheSupport.java} | 34 ++--------------------
 .../utils/{JCacheUtils.java => JCacheUtil.java}    | 30 ++-----------------
 .../apache/cxf/ws/security/wss4j/WSS4JUtils.java   |  4 +--
 .../cxf/sts/cache/jcache/JCacheIdentityCache.java  |  4 +--
 .../xkms/cache/jcache/JCacheXKMSClientCache.java   |  4 +--
 8 files changed, 17 insertions(+), 71 deletions(-)

diff --git 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/jcache/JCacheReplayCache.java
 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/jcache/JCacheReplayCache.java
index fca66a8551..435b089512 100644
--- 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/jcache/JCacheReplayCache.java
+++ 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/cache/jcache/JCacheReplayCache.java
@@ -30,7 +30,7 @@ import javax.cache.expiry.Duration;
 import javax.cache.expiry.ExpiryPolicy;
 import javax.cache.spi.CachingProvider;
 
-import org.apache.cxf.ws.security.utils.JCacheUtils;
+import org.apache.cxf.ws.security.utils.JCacheSupport;
 import org.apache.wss4j.common.cache.EHCacheExpiry;
 import org.apache.wss4j.common.cache.EHCacheValue;
 import org.apache.wss4j.common.cache.ReplayCache;
@@ -59,7 +59,7 @@ class JCacheReplayCache implements ReplayCache {
         try {
             final CachingProvider cachingProvider = 
Caching.getCachingProvider();
             cacheManager = cachingProvider.getCacheManager(); 
-            cache = JCacheUtils.getOrCreate(cacheManager, key, String.class, 
EHCacheValue.class,
+            cache = JCacheSupport.getOrCreate(cacheManager, key, String.class, 
EHCacheValue.class,
                 cacheConfiguration -> 
cacheConfiguration.setExpiryPolicyFactory(() -> new ExpiryPolicy() {
                     @Override
                     public Duration getExpiryForCreation() {
diff --git 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStoreFactory.java
 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStoreFactory.java
index 436508d79d..57ae4236c3 100644
--- 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStoreFactory.java
+++ 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/TokenStoreFactory.java
@@ -21,7 +21,7 @@ package org.apache.cxf.ws.security.tokenstore;
 
 import org.apache.cxf.message.Message;
 import org.apache.cxf.ws.security.tokenstore.jcache.JCacheTokenStoreFactory;
-import org.apache.cxf.ws.security.utils.JCacheUtils;
+import org.apache.cxf.ws.security.utils.JCacheUtil;
 import org.apache.wss4j.common.cache.WSS4JCacheUtil;
 
 /**
@@ -32,7 +32,7 @@ public abstract class TokenStoreFactory {
     public static TokenStoreFactory newInstance() {
         if (WSS4JCacheUtil.isEhCacheInstalled()) {
             return new EHCacheTokenStoreFactory();
-        } else if (JCacheUtils.isJCacheInstalled()) {
+        } else if (JCacheUtil.isJCacheInstalled()) {
             return new JCacheTokenStoreFactory();
         } else {
             return new MemoryTokenStoreFactory();
diff --git 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/jcache/JCacheTokenStore.java
 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/jcache/JCacheTokenStore.java
index a179c6ab30..b424134920 100644
--- 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/jcache/JCacheTokenStore.java
+++ 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/tokenstore/jcache/JCacheTokenStore.java
@@ -37,7 +37,7 @@ import org.apache.cxf.common.util.StringUtils;
 import org.apache.cxf.ws.security.tokenstore.SecurityToken;
 import org.apache.cxf.ws.security.tokenstore.TokenStore;
 import org.apache.cxf.ws.security.tokenstore.TokenStoreException;
-import org.apache.cxf.ws.security.utils.JCacheUtils;
+import org.apache.cxf.ws.security.utils.JCacheSupport;
 
 /**
  * An in-memory JCache implementation of the TokenStore interface. The default 
TTL is 60 minutes
@@ -66,7 +66,7 @@ public class JCacheTokenStore implements TokenStore, 
Closeable, BusLifeCycleList
             final CachingProvider cachingProvider = 
Caching.getCachingProvider();
             cacheManager = 
cachingProvider.getCacheManager(configFileURL.toURI(),
                     SecurityToken.class.getClassLoader()); 
-            cache = JCacheUtils.getOrCreate(cacheManager, key, String.class, 
SecurityToken.class);
+            cache = JCacheSupport.getOrCreate(cacheManager, key, String.class, 
SecurityToken.class);
         } catch (Exception e) {
             throw new TokenStoreException(e);
         }
diff --git 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtils.java
 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheSupport.java
similarity index 62%
copy from 
rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtils.java
copy to 
rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheSupport.java
index dcbe901b51..95cedb378c 100644
--- 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtils.java
+++ 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheSupport.java
@@ -18,39 +18,14 @@
  */
 package org.apache.cxf.ws.security.utils;
 
-import java.lang.invoke.MethodHandles;
-import java.lang.invoke.MethodType;
 import java.util.function.Function;
-import java.util.logging.Logger;
 
 import javax.cache.Cache;
 import javax.cache.CacheManager;
 import javax.cache.configuration.MutableConfiguration;
 
-import org.apache.cxf.common.logging.LogUtils;
-
-public final class JCacheUtils {
-    private static final Logger LOG = LogUtils.getL7dLogger(JCacheUtils.class);
-    private static final boolean JCACHE_INSTALLED;
-
-    static {
-        boolean jcacheInstalled = false;
-        try {
-            final Class<?> caching = Class.forName("javax.cache.Caching");
-            final Class<?> cachingProvider = 
Class.forName("javax.cache.spi.CachingProvider");
-            if (caching != null) {
-                jcacheInstalled = MethodHandles
-                    .publicLookup()
-                    .findStatic(caching, "getCachingProvider", 
MethodType.methodType(cachingProvider))
-                    .invoke() != null;
-            }
-        } catch (Throwable e) {
-            LOG.fine("No JCache SPIs detected on classpath: " + 
e.getMessage());
-        }
-        JCACHE_INSTALLED = jcacheInstalled;
-    }
-
-    private JCacheUtils() {
+public final class JCacheSupport {
+    private JCacheSupport() {
     }
     
     public static <K, V> Cache<K, V> getOrCreate(CacheManager cacheManager, 
String name, Class<K> kclass,
@@ -71,9 +46,4 @@ public final class JCacheUtils {
 
         return cacheManager.createCache(name, cacheConfiguration);
     }
-
-    public static boolean isJCacheInstalled() {
-        return JCACHE_INSTALLED;
-    }
-
 }
diff --git 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtils.java
 b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtil.java
similarity index 63%
rename from 
rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtils.java
rename to 
rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtil.java
index dcbe901b51..2f5733bb48 100644
--- 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtils.java
+++ 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/utils/JCacheUtil.java
@@ -20,17 +20,12 @@ package org.apache.cxf.ws.security.utils;
 
 import java.lang.invoke.MethodHandles;
 import java.lang.invoke.MethodType;
-import java.util.function.Function;
 import java.util.logging.Logger;
 
-import javax.cache.Cache;
-import javax.cache.CacheManager;
-import javax.cache.configuration.MutableConfiguration;
-
 import org.apache.cxf.common.logging.LogUtils;
 
-public final class JCacheUtils {
-    private static final Logger LOG = LogUtils.getL7dLogger(JCacheUtils.class);
+public final class JCacheUtil {
+    private static final Logger LOG = LogUtils.getL7dLogger(JCacheUtil.class);
     private static final boolean JCACHE_INSTALLED;
 
     static {
@@ -50,26 +45,7 @@ public final class JCacheUtils {
         JCACHE_INSTALLED = jcacheInstalled;
     }
 
-    private JCacheUtils() {
-    }
-    
-    public static <K, V> Cache<K, V> getOrCreate(CacheManager cacheManager, 
String name, Class<K> kclass,
-            Class<V> vclass) {
-        return getOrCreate(cacheManager, name, kclass, vclass, 
Function.identity());
-    }
-
-    public static <K, V> Cache<K, V> getOrCreate(CacheManager cacheManager, 
String name, Class<K> kclass,
-            Class<V> vclass, Function<MutableConfiguration<K, V>, 
MutableConfiguration<K, V>> customizer) {
-
-        final Cache<K, V> cache = cacheManager.getCache(name, kclass, vclass);
-        if (cache != null) {
-            return cache;
-        }
-
-        MutableConfiguration<K, V> cacheConfiguration = new 
MutableConfiguration<>();
-        cacheConfiguration = 
customizer.apply(cacheConfiguration.setTypes(kclass, vclass));
-
-        return cacheManager.createCache(name, cacheConfiguration);
+    private JCacheUtil() {
     }
 
     public static boolean isJCacheInstalled() {
diff --git 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java
index df6f1b7f1f..0b6af58aaa 100644
--- 
a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java
+++ 
b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/WSS4JUtils.java
@@ -51,7 +51,7 @@ import 
org.apache.cxf.ws.security.cache.jcache.CXFJCacheReplayCache;
 import org.apache.cxf.ws.security.tokenstore.SecurityToken;
 import org.apache.cxf.ws.security.tokenstore.TokenStoreException;
 import org.apache.cxf.ws.security.tokenstore.TokenStoreUtils;
-import org.apache.cxf.ws.security.utils.JCacheUtils;
+import org.apache.cxf.ws.security.utils.JCacheUtil;
 import org.apache.wss4j.common.cache.MemoryReplayCache;
 import org.apache.wss4j.common.cache.ReplayCache;
 import org.apache.wss4j.common.cache.WSS4JCacheUtil;
@@ -148,7 +148,7 @@ public final class WSS4JUtils {
                             throw new 
WSSecurityException(WSSecurityException.ErrorCode.FAILURE, ex);
                         }
                         replayCache = new CXFEHCacheReplayCache(cacheKey, bus, 
diskstoreParent);
-                    } else if (JCacheUtils.isJCacheInstalled()) {
+                    } else if (JCacheUtil.isJCacheInstalled()) {
                         Bus bus = message.getExchange().getBus();
                         replayCache = new CXFJCacheReplayCache(cacheKey, bus);
                     } else {
diff --git 
a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/jcache/JCacheIdentityCache.java
 
b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/jcache/JCacheIdentityCache.java
index cf58dde1ce..6be3cf153c 100644
--- 
a/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/jcache/JCacheIdentityCache.java
+++ 
b/services/sts/sts-core/src/main/java/org/apache/cxf/sts/cache/jcache/JCacheIdentityCache.java
@@ -48,7 +48,7 @@ import org.apache.cxf.sts.IdentityMapper;
 import org.apache.cxf.sts.cache.AbstractIdentityCache;
 import org.apache.cxf.sts.cache.EHCacheIdentityValue;
 import org.apache.cxf.ws.security.tokenstore.TokenStoreFactory;
-import org.apache.cxf.ws.security.utils.JCacheUtils;
+import org.apache.cxf.ws.security.utils.JCacheSupport;
 
 /**
  * A JCache based cache to cache identities in different realms where
@@ -92,7 +92,7 @@ public class JCacheIdentityCache extends AbstractIdentityCache
         final URL xmlConfigURL = configFileURL != null ? configFileURL : 
getDefaultConfigFileURL();
         try {
             cacheManager = 
cachingProvider.getCacheManager(xmlConfigURL.toURI(), 
getClass().getClassLoader());
-            cache = JCacheUtils.getOrCreate(cacheManager, KEY, String.class, 
EHCacheIdentityValue.class);
+            cache = JCacheSupport.getOrCreate(cacheManager, KEY, String.class, 
EHCacheIdentityValue.class);
         } catch (final URISyntaxException ex) {
             throw new IllegalStateException("Unable to convert " + 
xmlConfigURL + " to URI", ex);
         } 
diff --git 
a/services/xkms/xkms-client/src/main/java/org/apache/cxf/xkms/cache/jcache/JCacheXKMSClientCache.java
 
b/services/xkms/xkms-client/src/main/java/org/apache/cxf/xkms/cache/jcache/JCacheXKMSClientCache.java
index c1c078d139..8c8921efaa 100644
--- 
a/services/xkms/xkms-client/src/main/java/org/apache/cxf/xkms/cache/jcache/JCacheXKMSClientCache.java
+++ 
b/services/xkms/xkms-client/src/main/java/org/apache/cxf/xkms/cache/jcache/JCacheXKMSClientCache.java
@@ -33,7 +33,7 @@ import org.apache.cxf.Bus;
 import org.apache.cxf.BusFactory;
 import org.apache.cxf.buslifecycle.BusLifeCycleListener;
 import org.apache.cxf.buslifecycle.BusLifeCycleManager;
-import org.apache.cxf.ws.security.utils.JCacheUtils;
+import org.apache.cxf.ws.security.utils.JCacheSupport;
 import org.apache.cxf.xkms.cache.XKMSCacheToken;
 import org.apache.cxf.xkms.cache.XKMSClientCache;
 import org.apache.cxf.xkms.cache.XKMSClientCacheException;
@@ -65,7 +65,7 @@ public class JCacheXKMSClientCache implements 
XKMSClientCache, BusLifeCycleListe
         final CachingProvider cachingProvider = Caching.getCachingProvider();
         cacheManager = cachingProvider.getCacheManager(); 
 
-        cache = JCacheUtils.getOrCreate(cacheManager, cacheKey, String.class, 
XKMSCacheToken.class,
+        cache = JCacheSupport.getOrCreate(cacheManager, cacheKey, 
String.class, XKMSCacheToken.class,
             cacheConfiguration -> cacheConfiguration.setExpiryPolicyFactory(() 
-> new ExpiryPolicy() {
                 @Override
                 public Duration getExpiryForCreation() {

Reply via email to