hanicz commented on code in PR #1053:
URL: https://github.com/apache/knox/pull/1053#discussion_r2129015664


##########
gateway-provider-security-shiro/src/main/java/org/apache/knox/gateway/shirorealm/KnoxCacheManager.java:
##########
@@ -99,16 +108,54 @@ private synchronized org.ehcache.Cache<Object, Object> 
createCache(String name)
 
   private org.ehcache.CacheManager ensureCacheManager() throws 
MalformedURLException {
     if (manager == null) {
-      manager = CacheManagerBuilder.newCacheManager(getConfiguration());
-      manager.init();
+      XmlConfiguration xmlConfiguration = getConfiguration();
+      manager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
+      try {
+        manager.init();
+      } catch (StateTransitionException e) {
+        LOG.errorCreatingCacheManager(e.getMessage());
+        this.resolveLockConflict(xmlConfiguration);
+        if(manager.getStatus() != Status.UNINITIALIZED) {
+            manager.close();
+        }
+        manager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
+        manager.init();
+      }
 
       cacheManagerImplicitlyCreated = true;
     }
 
     return manager;
   }
 
-  private URL getResource() {
+  /**
+   * Resolves lock conflicts by changing the persistence directory of the 
cache manager.
+   * This is necessary when multiple instances of the cache manager are 
created with the same configuration file,
+   * which can lead to lock conflicts.
+   *
+   * @param xmlConfiguration the XML configuration of the cache manager
+   */
+  private void resolveLockConflict(XmlConfiguration xmlConfiguration) {
+    Optional<ServiceCreationConfiguration<?>> serviceConfig = 
xmlConfiguration.getServiceCreationConfigurations().stream()
+            .filter(service -> service instanceof 
CacheManagerPersistenceConfiguration).findFirst();
+
+    if (serviceConfig.isPresent()) {
+      CacheManagerPersistenceConfiguration cachePersistenceConfig = 
(CacheManagerPersistenceConfiguration) serviceConfig.get();
+      String path = cachePersistenceConfig.getRootDirectory().getPath();
+      
xmlConfiguration.getServiceCreationConfigurations().remove(cachePersistenceConfig);
+      String newFolder = DEFAULT_FOLDER_NAME + 
UUID.randomUUID().toString().substring(0, 4);
+      String newRootDirectory = 
Paths.get(path).getParent().resolve(newFolder).toAbsolutePath().toString();
+      xmlConfiguration.getServiceCreationConfigurations()

Review Comment:
   No, the class only holds one attribute the rootDirectory. I create an 
instance of it the same way as the Ehcache code does.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@knox.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to