Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVioleta Georgieva2014-11-17 17:04:11 -0500
committerVioleta Georgieva2014-11-17 17:04:11 -0500
commite389a75303a63fb760dfc70d17896467f108d411 (patch)
tree4bb1db1ceb92d5c5cb2c23d959e9c086f04b9d23
parent2464416823298a6038aac42d375870a9d937c149 (diff)
downloadorg.eclipse.gemini.naming-e389a75303a63fb760dfc70d17896467f108d411.tar.gz
org.eclipse.gemini.naming-e389a75303a63fb760dfc70d17896467f108d411.tar.xz
org.eclipse.gemini.naming-e389a75303a63fb760dfc70d17896467f108d411.zip
bug 449875: Use WeakHashMap to keep the Context objects thus the Context objects will be automatically removed when they are no longer in ordinary use.
-rw-r--r--framework/src/main/java/org/eclipse/gemini/naming/ContextManagerImpl.java40
-rw-r--r--framework/src/main/java/org/eclipse/gemini/naming/OSGiInitialContextFactoryBuilder.java15
2 files changed, 28 insertions, 27 deletions
diff --git a/framework/src/main/java/org/eclipse/gemini/naming/ContextManagerImpl.java b/framework/src/main/java/org/eclipse/gemini/naming/ContextManagerImpl.java
index 4ee27b5..1ead4be 100644
--- a/framework/src/main/java/org/eclipse/gemini/naming/ContextManagerImpl.java
+++ b/framework/src/main/java/org/eclipse/gemini/naming/ContextManagerImpl.java
@@ -17,10 +17,9 @@ package org.eclipse.gemini.naming;
import java.util.Collections;
import java.util.Hashtable;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
+import java.util.Set;
+import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -40,8 +39,8 @@ class ContextManagerImpl implements CloseableContextManager {
private final OSGiInitialContextFactoryBuilder m_builder;
/* list of Context implementations */
- private final List m_listOfContexts =
- Collections.synchronizedList(new LinkedList());
+ private final Map<Context, Object> m_listOfContexts =
+ Collections.synchronizedMap(new WeakHashMap<Context, Object>());
ContextManagerImpl(Bundle callingBundle, BundleContext implBundleContext) {
// create a new builder for each client bundle
@@ -54,7 +53,7 @@ class ContextManagerImpl implements CloseableContextManager {
public Context newInitialContext() throws NamingException {
synchronized (m_builder) {
final Context initialContext = createNewInitialContext(new Hashtable());
- m_listOfContexts.add(initialContext);
+ m_listOfContexts.put(initialContext, null);
return initialContext;
}
}
@@ -63,7 +62,7 @@ class ContextManagerImpl implements CloseableContextManager {
throws NamingException {
synchronized (m_builder) {
final Context initialContext = createNewInitialContext(environment);
- m_listOfContexts.add(initialContext);
+ m_listOfContexts.put(initialContext, null);
return initialContext;
}
}
@@ -72,7 +71,7 @@ class ContextManagerImpl implements CloseableContextManager {
synchronized (m_builder) {
Context contextToReturn = createNewInitialContext(new Hashtable());
if (contextToReturn instanceof DirContext) {
- m_listOfContexts.add(contextToReturn);
+ m_listOfContexts.put(contextToReturn, null);
return (DirContext) contextToReturn;
}
}
@@ -84,7 +83,7 @@ class ContextManagerImpl implements CloseableContextManager {
synchronized (m_builder) {
Context context = createNewInitialContext(environment);
if (context instanceof DirContext) {
- m_listOfContexts.add(context);
+ m_listOfContexts.put(context, null);
return (DirContext) context;
}
}
@@ -99,17 +98,18 @@ class ContextManagerImpl implements CloseableContextManager {
public void close() {
// close known Context implementations
synchronized (m_listOfContexts) {
- Iterator iterator = m_listOfContexts.iterator();
- // call close() on all known contexts
- while (iterator.hasNext()) {
- Context context = (Context) iterator.next();
- try {
- context.close();
- }
- catch (NamingException e) {
- logger.log(Level.INFO,
- "NamingException occurred while trying to close an existing JNDI Context",
- e);
+ Set<Context> iterator = m_listOfContexts.keySet();
+ if (iterator != null) {
+ // call close() on all known contexts
+ for (Context context : iterator) {
+ try {
+ context.close();
+ } catch (NamingException e) {
+ logger.log(
+ Level.INFO,
+ "NamingException occurred while trying to close an existing JNDI Context",
+ e);
+ }
}
}
}
diff --git a/framework/src/main/java/org/eclipse/gemini/naming/OSGiInitialContextFactoryBuilder.java b/framework/src/main/java/org/eclipse/gemini/naming/OSGiInitialContextFactoryBuilder.java
index a148a9d..3c8e553 100644
--- a/framework/src/main/java/org/eclipse/gemini/naming/OSGiInitialContextFactoryBuilder.java
+++ b/framework/src/main/java/org/eclipse/gemini/naming/OSGiInitialContextFactoryBuilder.java
@@ -27,10 +27,10 @@ import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
-import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
+import java.util.WeakHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -94,7 +94,8 @@ class OSGiInitialContextFactoryBuilder implements
* Map of OSGi services to a List of Contexts created by that service.
* Each service services as a key to a list of Context implementations.
*/
- private final Map m_mapOfServicesToContexts = Collections.synchronizedMap(new HashMap());
+ private final Map<Object, WeakHashMap<Context, Object>> m_mapOfServicesToContexts =
+ Collections.synchronizedMap(new HashMap<Object, WeakHashMap<Context, Object>>());
public OSGiInitialContextFactoryBuilder(BundleContext callerBundleContext, BundleContext implBundleContext) {
@@ -231,13 +232,13 @@ class OSGiInitialContextFactoryBuilder implements
public void associateFactoryService(Object factory, Context createdContext) {
if(m_mapOfServicesToContexts.containsKey(factory)) {
- List listOfContexts =
- (List) m_mapOfServicesToContexts.get(factory);
- listOfContexts.add(createdContext);
+ WeakHashMap<Context, Object> listOfContexts =
+ (WeakHashMap<Context, Object>) m_mapOfServicesToContexts.get(factory);
+ listOfContexts.put(createdContext, null);
m_mapOfServicesToContexts.put(factory, listOfContexts);
} else {
- List listOfContexts = new LinkedList();
- listOfContexts.add(createdContext);
+ WeakHashMap<Context, Object> listOfContexts = new WeakHashMap<Context, Object>();
+ listOfContexts.put(createdContext, null);
m_mapOfServicesToContexts.put(factory, listOfContexts);
}

Back to the top