rdonkin 2003/07/07 15:00:03 Modified: beanutils/src/java/org/apache/commons/beanutils BeanUtilsBean.java Log: Refactored context thread loader logic into separate class. Added new setInstance method which works on a per-thread-context-classloader basis. Patch contributed by Eric Pabst Revision Changes Path 1.11 +22 -35 jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtilsBean.java Index: BeanUtilsBean.java =================================================================== RCS file: /home/cvs/jakarta-commons/beanutils/src/java/org/apache/commons/beanutils/BeanUtilsBean.java,v retrieving revision 1.10 retrieving revision 1.11 diff -u -r1.10 -r1.11 --- BeanUtilsBean.java 24 May 2003 08:14:23 -0000 1.10 +++ BeanUtilsBean.java 7 Jul 2003 22:00:02 -0000 1.11 @@ -103,15 +103,16 @@ // ------------------------------------------------------ Private Class Variables - /** Singleton instance */ - private static final BeanUtilsBean singleton = new BeanUtilsBean(); /** - * Map contains <code>BeanUtilsBean</code> instances indexed by context classloader. - * <strong>Note:</strong> A WeakHashMap bug in several 1.3 JVMs results in a memory leak - * for those JVMs. + * Contains <code>BeanUtilsBean</code> instances indexed by context classloader. */ - private static final Map beansByClassLoader - = new WeakHashMap(); + private static final ContextClassLoaderLocal + beansByClassLoader = new ContextClassLoaderLocal() { + // Creates the default instance used when the context classloader is unavailable + protected Object initialValue() { + return new BeanUtilsBean(); + } + }; /** * Gets the instance which provides the functionality for [EMAIL PROTECTED] BeanUtils}. @@ -119,30 +120,16 @@ * This mechanism provides isolation for web apps deployed in the same container. */ public synchronized static BeanUtilsBean getInstance() { - // synchronizing the whole method is a bit slower - // but guarentees no subtle threading problems - - // make sure that the map is given a change to purge itself - beansByClassLoader.isEmpty(); - try { - - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - if (contextClassLoader != null) { - - BeanUtilsBean instance = (BeanUtilsBean) beansByClassLoader.get(contextClassLoader); - - if (instance == null) { - instance = new BeanUtilsBean(); - beansByClassLoader.put(contextClassLoader, instance); - } - return instance; - - } - - } catch (SecurityException e) { /* SWALLOW - should we log this? */ } - - // if in doubt, return the basic - return singleton; + return (BeanUtilsBean) beansByClassLoader.get(); + } + + /** + * Sets the instance which provides the functionality for [EMAIL PROTECTED] BeanUtils}. + * This is a pseudo-singleton - an single instance is provided per (thread) context classloader. + * This mechanism provides isolation for web apps deployed in the same container. + */ + public synchronized static void setInstance(BeanUtilsBean newInstance) { + beansByClassLoader.set(newInstance); } // --------------------------------------------------------- Attributes
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]