From 553a373be707a466b2fdac6eb10568572b6f10a1 Mon Sep 17 00:00:00 2001 From: slaws Date: Sun, 4 Mar 2012 17:48:55 +0000 Subject: TUSCANY-3312 - Copy JavaInterfaceFactory memory leak changes from 1.x + other related changes and some extensions to allow the cache to be reduced when contributions are unloaded in the domain node. git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1296845 13f79535-47bb-0310-9956-ffa450edef68 --- .../interfacedef/java/JavaInterfaceFactory.java | 9 +++++ .../java/impl/JavaInterfaceFactoryImpl.java | 47 ++++++++++++++++++++++ 2 files changed, 56 insertions(+) (limited to 'sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany') diff --git a/sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany/sca/interfacedef/java/JavaInterfaceFactory.java b/sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany/sca/interfacedef/java/JavaInterfaceFactory.java index 55d694d7f8..c1357af9d6 100644 --- a/sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany/sca/interfacedef/java/JavaInterfaceFactory.java +++ b/sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany/sca/interfacedef/java/JavaInterfaceFactory.java @@ -96,4 +96,13 @@ public interface JavaInterfaceFactory { * @return */ List getInterfaceVisitors(); + + /** + * Remove the interfaces that have been registered for + * the contribution identified by the contribution class + * loader provided + * + * @param contributionClassloader + */ + void removeInterfacesForContribution(ClassLoader contributionClassloader); } diff --git a/sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany/sca/interfacedef/java/impl/JavaInterfaceFactoryImpl.java b/sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany/sca/interfacedef/java/impl/JavaInterfaceFactoryImpl.java index 8229c7812d..21c21e075d 100644 --- a/sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany/sca/interfacedef/java/impl/JavaInterfaceFactoryImpl.java +++ b/sca-java-2.x/trunk/modules/interface-java/src/main/java/org/apache/tuscany/sca/interfacedef/java/impl/JavaInterfaceFactoryImpl.java @@ -21,8 +21,10 @@ package org.apache.tuscany.sca.interfacedef.java.impl; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; +import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.WeakHashMap; import org.apache.tuscany.sca.interfacedef.InvalidInterfaceException; @@ -31,6 +33,7 @@ import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceContract; import org.apache.tuscany.sca.interfacedef.java.JavaInterfaceFactory; import org.apache.tuscany.sca.interfacedef.java.JavaOperation; import org.apache.tuscany.sca.interfacedef.java.introspect.JavaInterfaceVisitor; +import org.oasisopen.sca.ServiceRuntimeException; /** * A factory for the Java model. @@ -112,4 +115,48 @@ public abstract class JavaInterfaceFactoryImpl implements JavaInterfaceFactory { op.setName(method.getName()); return op; } + + /** + * Removes all the cached information relating to a contribution. The + * contribution is identified by the contribution classloader passed in + * as a parameter. This is used when a contribution is removed from + * the runtime. + * + * @param contributionClassloader + */ + public void removeInterfacesForContribution(ClassLoader contributionClassloader){ + removeInterfacesFromCache(contributionClassloader, normalCache); + removeInterfacesFromCache(contributionClassloader, forceRemotableCache); + } + + private void removeInterfacesFromCache(ClassLoader contributionClassloader, Map, JavaInterface> cache){ + try { + synchronized(cache) { + Set> clsSet = cache.keySet(); + Iterator> i = clsSet.iterator(); + while (i.hasNext()) { + Class cls = i.next(); + if (cls.getClassLoader() == contributionClassloader) { + i.remove(); + } + } + } + } catch(Exception e) { + throw new ServiceRuntimeException(e); + } + } + + /** + * For testing so we can check that the cache is being cleared + */ + public Map, JavaInterface> getNormalCache(){ + return normalCache; + } + + /** + * For testing so we can check that the cache is being cleared + */ + public Map, JavaInterface> getForceRemotableCache(){ + return forceRemotableCache; + } } -- cgit v1.2.3