Source Code Cross Referenced for EvilSingletonsUnregisterer.java in  » Profiler » MessAdmin » clime » messadmin » providers » lifecycle » 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 » Profiler » MessAdmin » clime.messadmin.providers.lifecycle 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * 
003:         */package clime.messadmin.providers.lifecycle;
004:
005:        import java.beans.Introspector;
006:        import java.lang.reflect.Method;
007:        import java.sql.Driver;
008:        import java.sql.DriverManager;
009:        import java.sql.SQLException;
010:        import java.util.Enumeration;
011:
012:        import javax.servlet.ServletContext;
013:        import javax.servlet.ServletContextEvent;
014:
015:        import clime.messadmin.providers.spi.ApplicationLifeCycleProvider;
016:
017:        /**
018:         * Takes care of deregistering (some of) the evil Singletons
019:         * when the app shuts down, thereby avoiding (well, trying to avoid)
020:         * OOM (java.lang.OutOfMemoryError) on hot restart...
021:         * 
022:         * @see <a href="http://opensource2.atlassian.com/confluence/spring/pages/viewpage.action?pageId=2669">Memory leaks where the classloader cannot be garbage collected</a>
023:         * @author C&eacute;drik LIME
024:         */
025:        public class EvilSingletonsUnregisterer implements 
026:                ApplicationLifeCycleProvider {
027:
028:            /**
029:             * 
030:             */
031:            public EvilSingletonsUnregisterer() {
032:                super ();
033:            }
034:
035:            /**
036:             * {@inheritDoc}
037:             */
038:            public int getPriority() {
039:                return Integer.MAX_VALUE;
040:            }
041:
042:            /**
043:             * {@inheritDoc}
044:             */
045:            public void contextDestroyed(ServletContext servletContext) {
046:                // ThreadLocals - With Great Power, comes Great Responsibility
047:                // Can't do anything here. You need to manually set all your ThreadLocals to null yourself (myThreadLocal.remove() or myThreadLocal.set(null))...
048:
049:                final ClassLoader this ClassLoader = Thread.currentThread()
050:                        .getContextClassLoader();//this.getClass().getClassLoader();
051:
052:                // java.beans.Introspector - Slightly less Evil singleton
053:                // Flushes the cache of classes
054:                // Note this is going to clear ALL the classes, regardless of what ClassLoader or application they came from, but it's all that's available.
055:                Introspector.flushCaches();
056:
057:                // Jakarta Commons IO >= 1.3
058:                // org.apache.commons.io.FileCleaner.exitWhenFinished();
059:                try {
060:                    Class fileCleanerClass = this ClassLoader
061:                            .loadClass("org.apache.commons.io.FileCleaner");//$NON-NLS-1$
062:                    if (fileCleanerClass != null
063:                            && fileCleanerClass.getClassLoader() == this ClassLoader) {
064:                        // If the given classloader is the same as the classloader of FileCleaner, this means that the lib
065:                        // has been deployed inside the war, so the thread should be stopped and all resources released.
066:                        // If the classloader is different, then the thread is owned by the container, so don't stop it.
067:                        Method ewfMethod = fileCleanerClass.getMethod(
068:                                "exitWhenFinished", null);//$NON-NLS-1$
069:                        ewfMethod.invoke(null, null);
070:                    }
071:                } catch (Throwable t) {
072:                    // ignore
073:                }
074:
075:                // Jakarta Commons Logging <= 1.0.4
076:                // org.apache.commons.logging.LogFactory.release(Thread.currentThread().getContextClassLoader());
077:                try {
078:                    ClassLoader loader = this ClassLoader;
079:                    while (loader != null) {
080:                        Class logFactoryClass = loader
081:                                .loadClass("org.apache.commons.logging.LogFactory");//$NON-NLS-1$
082:                        Method releaseMethod = logFactoryClass.getMethod(
083:                                "release", new Class[] { ClassLoader.class });//$NON-NLS-1$
084:                        releaseMethod.invoke(null,
085:                                new Object[] { this ClassLoader });
086:                        loader = logFactoryClass.getClassLoader().getParent();
087:                    }
088:                } catch (Throwable t) {
089:                    // ignore
090:                }
091:
092:                // LOGBack
093:                // new ch.qos.logback.classic.selector.servlet.ContextDetachingSCL().contextDestroyed(ServletContextEvent sce)
094:                try {
095:                    Class cdClass = this ClassLoader
096:                            .loadClass("ch.qos.logback.classic.selector.servlet.ContextDetachingSCL");//$NON-NLS-1$
097:                    Object instance = cdClass.newInstance();
098:                    Method destroyMethod = cdClass
099:                            .getMethod(
100:                                    "contextDestroyed", new Class[] { ServletContextEvent.class });//$NON-NLS-1$
101:                    destroyMethod.invoke(instance,
102:                            new Object[] { new ServletContextEvent(
103:                                    servletContext) });
104:                } catch (Throwable t) {
105:                    // ignore
106:                }
107:
108:                // Apache Logging Log4J 1.x
109:                // org.apache.log4j.LogManager.shutdown();
110:                try {
111:                    Class lmClass = this ClassLoader
112:                            .loadClass("org.apache.log4j.LogManager");//$NON-NLS-1$
113:                    Method shutdownMethod = lmClass.getMethod("shutdown", null);//$NON-NLS-1$
114:                    shutdownMethod.invoke(null, null);
115:                } catch (Throwable t) {
116:                    // ignore
117:                }
118:
119:                // java.sql.DriverManager - Evil Singleton
120:                // Although registering the JDBC driver in your web app is a horrible, horrible thing to do (the container should always manage your connections), some apps do just that.
121:                // Unregister JDBC drivers during shutdown: remove any drivers that were loaded by the same classloader that loaded the web app.
122:                Enumeration drivers = DriverManager.getDrivers();
123:                while (drivers.hasMoreElements()) {
124:                    Driver o = (Driver) drivers.nextElement();
125:                    if (o.getClass().getClassLoader() == this ClassLoader) {
126:                        // Current driver 'o' is being deregistered
127:                        try {
128:                            DriverManager.deregisterDriver(o);
129:                        } catch (SQLException sqle) {
130:                            //throw new RuntimeException(sqle.getMessage());//new RuntimeException(sqle);
131:                            System.err
132:                                    .println("Failed to cleanup DriverManager for webapp:");
133:                            sqle.printStackTrace(System.err);
134:                        }
135:                    } else {
136:                        // Driver 'o' wasn't loaded by this webapp, so no touching it
137:                    }
138:                }
139:            }
140:
141:            /**
142:             * {@inheritDoc}
143:             */
144:            public void contextInitialized(ServletContext servletContext) {
145:            }
146:
147:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.