~ubuntu-branches/ubuntu/oneiric/tomcat6/oneiric

« back to all changes in this revision

Viewing changes to java/org/apache/catalina/core/JreMemoryLeakPreventionListener.java

  • Committer: Bazaar Package Importer
  • Author(s): Thierry Carrez
  • Date: 2010-07-20 14:36:48 UTC
  • mfrom: (2.2.17 sid)
  • Revision ID: james.westby@ubuntu.com-20100720143648-23y81x6cq1kv1z00
Tags: 6.0.28-2
* Add debconf questions for user, group and Java options.
* Use ucf to install /etc/default/tomcat6 from a template
* Drop CATALINA_BASE and CATALINA_HOME from /etc/default/tomcat6 since we
  shouldn't encourage users to change those anyway

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
        this.appContextProtection = appContextProtection;
68
68
    }
69
69
 
 
70
     /**
 
71
      * Protect against the memory leak caused when the first call to
 
72
      * <code>sun.net.www.http.HttpClient</code> is triggered by a web
 
73
      * application. This first call will start a KeepAlive thread with the
 
74
      * thread's context class loader configured to be the web application class
 
75
      * loader. Defaults to <code>true</code>.
 
76
      */
 
77
     private boolean keepAliveProtection = true;
 
78
     public boolean isKeepAliveProtection() { return keepAliveProtection; }
 
79
     public void setKeepAliveProtection(boolean keepAliveProtection) {
 
80
         this.keepAliveProtection = keepAliveProtection;
 
81
     }
 
82
    
70
83
    /**
71
84
     * Protect against resources being read for JAR files and, as a side-effect,
72
85
     * the JAR file becoming locked. Note this disables caching for all
126
139
            }
127
140
            
128
141
            /*
 
142
             * Several components end up calling:
 
143
             * sun.misc.GC.requestLatency(long)
 
144
             * 
 
145
             * Those libraries / components known to trigger memory leaks due to
 
146
             * eventual calls to requestLatency(long) are:
 
147
             * - javax.management.remote.rmi.RMIConnectorServer.start()
 
148
             */
 
149
            if (gcDaemonProtection) {
 
150
                try {
 
151
                    Class<?> clazz = Class.forName("sun.misc.GC");
 
152
                    Method method = clazz.getDeclaredMethod("requestLatency",
 
153
                            new Class[] {long.class});
 
154
                    method.invoke(null, Long.valueOf(3600000));
 
155
                } catch (ClassNotFoundException e) {
 
156
                    if (System.getProperty("java.vendor").startsWith("Sun")) {
 
157
                        log.error(sm.getString(
 
158
                                "jreLeakListener.gcDaemonFail"), e);
 
159
                    } else {
 
160
                        log.debug(sm.getString(
 
161
                                "jreLeakListener.gcDaemonFail"), e);
 
162
                    }
 
163
                } catch (SecurityException e) {
 
164
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
 
165
                } catch (NoSuchMethodException e) {
 
166
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
 
167
                } catch (IllegalArgumentException e) {
 
168
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
 
169
                } catch (IllegalAccessException e) {
 
170
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
 
171
                } catch (InvocationTargetException e) {
 
172
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
 
173
                }
 
174
            }
 
175
 
 
176
            /*
 
177
             * When a servlet opens a connection using a URL it will use
 
178
             * sun.net.www.http.HttpClient which keeps a static reference to a
 
179
             * keep-alive cache which is loaded using the web application class
 
180
             * loader.
 
181
             */
 
182
            if (keepAliveProtection) {
 
183
                try {
 
184
                    Class.forName("sun.net.www.http.HttpClient");
 
185
                } catch (ClassNotFoundException e) {
 
186
                    if (System.getProperty("java.vendor").startsWith("Sun")) {
 
187
                        log.error(sm.getString(
 
188
                                "jreLeakListener.keepAliveFail"), e);
 
189
                    } else {
 
190
                        log.debug(sm.getString(
 
191
                                "jreLeakListener.keepAliveFail"), e);
 
192
                    }
 
193
                }
 
194
            }
 
195
            
 
196
            /*
129
197
             * Several components end up opening JarURLConnections without first
130
198
             * disabling caching. This effectively locks the file. Whilst more
131
199
             * noticeable and harder to ignore on Windows, it affects all
167
235
                    log.error(sm.getString("jreLeakListener.xmlParseFail"), e);
168
236
                }
169
237
            }
170
 
            
171
 
            /*
172
 
             * Several components end up calling:
173
 
             * sun.misc.GC.requestLatency(long)
174
 
             * 
175
 
             * Those libraries / components known to trigger memory leaks due to
176
 
             * eventual calls to requestLatency(long) are:
177
 
             * - javax.management.remote.rmi.RMIConnectorServer.start()
178
 
             */
179
 
            if (gcDaemonProtection) {
180
 
                try {
181
 
                    Class<?> clazz = Class.forName("sun.misc.GC");
182
 
                    Method method = clazz.getDeclaredMethod("requestLatency",
183
 
                            new Class[] {long.class});
184
 
                    method.invoke(null, Long.valueOf(3600000));
185
 
                } catch (ClassNotFoundException e) {
186
 
                    if (System.getProperty("java.vendor").startsWith("Sun")) {
187
 
                        log.error(sm.getString(
188
 
                                "jreLeakListener.gcDaemonFail"), e);
189
 
                    } else {
190
 
                        log.debug(sm.getString(
191
 
                                "jreLeakListener.gcDaemonFail"), e);
192
 
                    }
193
 
                } catch (SecurityException e) {
194
 
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
195
 
                } catch (NoSuchMethodException e) {
196
 
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
197
 
                } catch (IllegalArgumentException e) {
198
 
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
199
 
                } catch (IllegalAccessException e) {
200
 
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
201
 
                } catch (InvocationTargetException e) {
202
 
                    log.error(sm.getString("jreLeakListener.gcDaemonFail"), e);
203
 
                }
204
 
            }
205
238
        }
206
239
    }
207
 
 
208
240
}