remove WeakReference hack.
It turned out that a proper management needs to release the config properly anyway.
And having a WeakReference did lead to re-creating the Config Discovery
again and again under heavy gc load.
diff --git a/impl/src/main/java/org/apache/geronimo/config/DefaultConfigProvider.java b/impl/src/main/java/org/apache/geronimo/config/DefaultConfigProvider.java
index a339962..627903f 100644
--- a/impl/src/main/java/org/apache/geronimo/config/DefaultConfigProvider.java
+++ b/impl/src/main/java/org/apache/geronimo/config/DefaultConfigProvider.java
@@ -21,6 +21,7 @@
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;
+import java.util.concurrent.ConcurrentHashMap;
import javax.enterprise.inject.Typed;
import javax.enterprise.inject.Vetoed;
@@ -37,8 +38,7 @@
@Vetoed
public class DefaultConfigProvider extends ConfigProviderResolver {
- private static Map<ClassLoader, WeakReference<Config>> configs
- = Collections.synchronizedMap(new WeakHashMap<ClassLoader, WeakReference<Config>>());
+ private static Map<ClassLoader, Config> configs = new ConcurrentHashMap<>();
@Override
@@ -68,15 +68,14 @@
}
Config existingConfig(ClassLoader forClassLoader) {
- WeakReference<Config> configRef = configs.get(forClassLoader);
- return configRef != null ? configRef.get() : null;
+ return configs.get(forClassLoader);
}
@Override
public void registerConfig(Config config, ClassLoader forClassLoader) {
synchronized (DefaultConfigProvider.class) {
- configs.put(forClassLoader, new WeakReference<>(config));
+ configs.put(forClassLoader, config);
}
}
@@ -99,24 +98,25 @@
if (config != null) {
synchronized (DefaultConfigProvider.class) {
- Iterator<Map.Entry<ClassLoader, WeakReference<Config>>> it = configs.entrySet().iterator();
+ Iterator<Map.Entry<ClassLoader, Config>> it = configs.entrySet().iterator();
while (it.hasNext()) {
- Map.Entry<ClassLoader, WeakReference<Config>> entry = it.next();
- if (entry.getValue().get() != null && entry.getValue().get() == config) {
+ Map.Entry<ClassLoader, Config> entry = it.next();
+ if (entry.getValue() != null && entry.getValue() == config) {
it.remove();
break;
}
}
+ }
- if (config instanceof AutoCloseable) {
- try {
- ((AutoCloseable) config).close();
- }
- catch (Exception e) {
- throw new RuntimeException("Error while closing Config", e);
- }
+ if (config instanceof AutoCloseable) {
+ try {
+ ((AutoCloseable) config).close();
+ }
+ catch (Exception e) {
+ throw new RuntimeException("Error while closing Config", e);
}
}
+
}
}
}