You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlbeans.apache.org by er...@apache.org on 2004/09/10 10:45:18 UTC
cvs commit: xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema SchemaTypeLoaderImpl.java
ericvas 2004/09/10 01:45:18
Modified: v2/src/typeimpl/org/apache/xmlbeans/impl/schema
SchemaTypeLoaderImpl.java
Log:
Cache schematype systems with soft reference to avoid out of memory
Revision Changes Path
1.4 +40 -17 xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeLoaderImpl.java
Index: SchemaTypeLoaderImpl.java
===================================================================
RCS file: /home/cvs/xml-xmlbeans/v2/src/typeimpl/org/apache/xmlbeans/impl/schema/SchemaTypeLoaderImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- SchemaTypeLoaderImpl.java 3 Mar 2004 22:56:39 -0000 1.3
+++ SchemaTypeLoaderImpl.java 10 Sep 2004 08:45:18 -0000 1.4
@@ -35,6 +35,8 @@
import java.util.List;
import java.util.IdentityHashMap;
+import java.lang.ref.SoftReference;
+
public class SchemaTypeLoaderImpl extends SchemaTypeLoaderBase
{
private ResourceLoader _resourceLoader;
@@ -52,31 +54,52 @@
private Map _documentCache;
private Map _classnameCache;
- private static SchemaTypeLoaderImpl buildContextTypeLoader()
+ // The following maintains a cache of SchemaTypeLoaders per ClassLoader per Thread.
+ // I use soft references to allow the garbage collector to reclain the type loaders
+ // and/pr class loaders at will.
+
+ private static ThreadLocal _cachedTypeSystems =
+ new ThreadLocal() { protected Object initialValue() { return new ArrayList(); } };
+
+ public static SchemaTypeLoaderImpl getContextTypeLoader ( )
{
- return new SchemaTypeLoaderImpl(new SchemaTypeLoader[] { BuiltinSchemaTypeSystem.get() } , null, Thread.currentThread().getContextClassLoader());
- }
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
- private static ThreadLocal _threadTypeSystems =
- new ThreadLocal()
+ ArrayList a = (ArrayList) _cachedTypeSystems.get();
+
+ int candidate = -1;
+ SchemaTypeLoaderImpl result = null;
+
+ for ( int i = 0 ; i < a.size() ; i++ )
{
- protected Object initialValue() { return new HashMap(); }
- };
+ SchemaTypeLoaderImpl tl = (SchemaTypeLoaderImpl) ((SoftReference) a.get( i )).get();
- public static SchemaTypeLoaderImpl getContextTypeLoader()
- {
- HashMap candidates = (HashMap) _threadTypeSystems.get();
- ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
- SchemaTypeLoaderImpl candidate = (SchemaTypeLoaderImpl) candidates.get( threadContextClassLoader );
+ if (tl == null)
+ a.remove( i-- );
+ else if (tl._classLoader == cl)
+ {
+ candidate = i;
+ result = tl;
+ }
+ }
+
+ if (candidate == -1)
+ {
+ result = new SchemaTypeLoaderImpl( new SchemaTypeLoader[] { BuiltinSchemaTypeSystem.get() } , null, cl );
+ a.add( new SoftReference( result ) );
+ candidate = a.size() - 1;
+ }
- if (candidate == null)
+ if (candidate > 0)
{
- candidate = buildContextTypeLoader();
- candidates.put( threadContextClassLoader, candidate );
+ Object t = a.get( 0 );
+ a.set( 0, a.get( candidate ) );
+ a.set( candidate, t );
}
- return candidate;
+
+ return result;
}
-
+
public static SchemaTypeLoader build(SchemaTypeLoader[] searchPath, ResourceLoader resourceLoader, ClassLoader classLoader)
{
if (searchPath == null)
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlbeans.apache.org
For additional commands, e-mail: commits-help@xmlbeans.apache.org