Java Doc for ContextClassLoaderLocal.java in  » Library » Apache-commons-beanutils-1.8.0-BETA-src » org » apache » commons » beanutils » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache commons beanutils 1.8.0 BETA src » org.apache.commons.beanutils 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


java.lang.Object
   org.apache.commons.beanutils.ContextClassLoaderLocal

ContextClassLoaderLocal
public class ContextClassLoaderLocal (Code)
An instance of this class represents a value that is provided per (thread) context classloader.

Occasionally it is necessary to store data in "global" variables (including uses of the Singleton pattern). In applications which have only a single classloader such data can simply be stored as "static" members on some class. When multiple classloaders are involved, however, this approach can fail; in particular, this doesn't work when the code may be run within a servlet container or a j2ee container, and the class on which the static member is defined is loaded via a "shared" classloader that is visible to all components running within the container. This class provides a mechanism for associating data with a ClassLoader instance, which ensures that when the code runs in such a container each component gets its own copy of the "global" variable rather than unexpectedly sharing a single copy of the variable with other components that happen to be running in the same container at the same time (eg servlets or EJBs.)

This class is strongly patterned after the java.lang.ThreadLocal class, which performs a similar task in allowing data to be associated with a particular thread.

When code that uses this class is run as a "normal" application, ie not within a container, the effect is identical to just using a static member variable to store the data, because Thread.getContextClassLoader always returns the same classloader (the system classloader).

Expected usage is as follows:

 public class SomeClass {
 private static final ContextClassLoaderLocal global 
 = new ContextClassLoaderLocal() {
 protected Object initialValue() {
 return new String("Initial value");
 };
 public void testGlobal() {
 String s = (String) global.get();
 System.out.println("global value:" + s);
 buf.set("New Value");
 }
 

Note: This class takes some care to ensure that when a component which uses this class is "undeployed" by a container the component-specific classloader and all its associated classes (and their static variables) are garbage-collected. Unfortunately there is one scenario in which this does not work correctly and there is unfortunately no known workaround other than ensuring that the component (or its container) calls the "unset" method on this class for each instance of this class when the component is undeployed. The problem occurs if:

  • the class containing a static instance of this class was loaded via a shared classloader, and
  • the value stored in the instance is an object whose class was loaded via the component-specific classloader (or any of the objects it refers to were loaded via that classloader).
The result is that the map managed by this object still contains a strong reference to the stored object, which contains a strong reference to the classloader that loaded it, meaning that although the container has "undeployed" the component the component-specific classloader and all the related classes and static variables cannot be garbage-collected. This is not expected to be an issue with the commons-beanutils library as the only classes which use this class are BeanUtilsBean and ConvertUtilsBean and there is no obvious reason for a user of the beanutils library to subclass either of those classes.

Note: A WeakHashMap bug in several 1.3 JVMs results in a memory leak for those JVMs.

Note: Of course all of this would be unnecessary if containers required each component to load the full set of classes it needs, ie avoided providing classes loaded via a "shared" classloader.


See Also:   java.lang.Thread.getContextClassLoader
See Also:   
author:
   Eric Pabst



Constructor Summary
public  ContextClassLoaderLocal()
    

Method Summary
public synchronized  Objectget()
     Gets the instance which provides the functionality for 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.
protected  ObjectinitialValue()
     Returns the initial value for this ContextClassLoaderLocal variable.
public synchronized  voidset(Object value)
     Sets the value - a value is provided per (thread) context classloader. This mechanism provides isolation for web apps deployed in the same container.
public synchronized  voidunset()
    
public synchronized  voidunset(ClassLoader classLoader)
    


Constructor Detail
ContextClassLoaderLocal
public ContextClassLoaderLocal()(Code)
Construct a context classloader instance




Method Detail
get
public synchronized Object get()(Code)
Gets the instance which provides the functionality for 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. the object currently associated with the context-classloader of the current thread.



initialValue
protected Object initialValue()(Code)
Returns the initial value for this ContextClassLoaderLocal variable. This method will be called once per Context ClassLoader for each ContextClassLoaderLocal, the first time it is accessed with get or set. If the programmer desires ContextClassLoaderLocal variables to be initialized to some value other than null, ContextClassLoaderLocal must be subclassed, and this method overridden. Typically, an anonymous inner class will be used. Typical implementations of initialValue will call an appropriate constructor and return the newly constructed object. a new Object to be used as an initial value for this ContextClassLoaderLocal



set
public synchronized void set(Object value)(Code)
Sets the value - a value is provided per (thread) context classloader. This mechanism provides isolation for web apps deployed in the same container.
Parameters:
  value - the object to be associated with the entrant thread's context classloader



unset
public synchronized void unset()(Code)
Unsets the value associated with the current thread's context classloader



unset
public synchronized void unset(ClassLoader classLoader)(Code)
Unsets the value associated with the given classloader
Parameters:
  classLoader - The classloader to unset for



Methods inherited from java.lang.Object
native protected Object clone() throws CloneNotSupportedException(Code)(Java Doc)
public boolean equals(Object obj)(Code)(Java Doc)
protected void finalize() throws Throwable(Code)(Java Doc)
final native public Class getClass()(Code)(Java Doc)
native public int hashCode()(Code)(Java Doc)
final native public void notify()(Code)(Java Doc)
final native public void notifyAll()(Code)(Java Doc)
public String toString()(Code)(Java Doc)
final native public void wait(long timeout) throws InterruptedException(Code)(Java Doc)
final public void wait(long timeout, int nanos) throws InterruptedException(Code)(Java Doc)
final public void wait() throws InterruptedException(Code)(Java Doc)

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.