1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.container.servlet

File SetThreadNameServletRequestListener.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
12
2
1
68
32
5
0.42
6
2
2.5

Classes

Class Line # Actions
SetThreadNameServletRequestListener 35 12 0% 5 2
0.990%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.container.servlet;
21   
22    import javax.servlet.ServletRequest;
23    import javax.servlet.ServletRequestEvent;
24    import javax.servlet.ServletRequestListener;
25    import javax.servlet.http.HttpServletRequest;
26   
27    /**
28    * Make threads names created by the application server more meaningful.
29    *
30    * TODO When it will be possible it would be better to do this a component like a RequestInitializer component to work
31    * for any kind of container. Right now component can't really access the initial URL.
32    * @version $Id: cdad2f21f67ec346f1b875537e03afcd2fb81ed2 $
33    * @since 2.0M3
34    */
 
35    public class SetThreadNameServletRequestListener implements ServletRequestListener
36    {
37    /** The name of the servlet request attribute holding the original name of the processing thread. */
38    private static final String ORIGINAL_THREAD_NAME_ATTRIBUTE = "xwiki.thread.originalName";
39   
 
40  14119 toggle @Override
41    public void requestInitialized(ServletRequestEvent sre)
42    {
43  14117 ServletRequest servletRequest = sre.getServletRequest();
44   
45  14047 if (servletRequest instanceof HttpServletRequest) {
46  14065 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
47   
48  14059 String threadName = httpServletRequest.getRequestURL().toString();
49   
50  14097 if (httpServletRequest.getQueryString() != null) {
51  9440 threadName += "?" + httpServletRequest.getQueryString();
52    }
53   
54  14075 httpServletRequest.setAttribute(ORIGINAL_THREAD_NAME_ATTRIBUTE, Thread.currentThread().getName());
55  14044 Thread.currentThread().setName(threadName);
56    }
57    }
58   
 
59  14122 toggle @Override
60    public void requestDestroyed(ServletRequestEvent sre)
61    {
62  14112 ServletRequest servletRequest = sre.getServletRequest();
63  14115 if (servletRequest instanceof HttpServletRequest) {
64  14102 HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
65  14108 Thread.currentThread().setName("" + httpServletRequest.getAttribute(ORIGINAL_THREAD_NAME_ATTRIBUTE));
66    }
67    }
68    }