KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > RequestAnalyser


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23
24 package org.infoglue.deliver.util;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.infoglue.cms.util.CmsPropertyHandler;
30
31 /**
32  * @author Mattias Bogeblad
33  */

34 public class RequestAnalyser
35 {
36     private static RequestAnalyser instance = new RequestAnalyser();
37     //public Integer numberOfCurrentRequests = new Integer(0);
38

39     //private static List currentRequests = new ArrayList();
40
private static HttpServletRequest JavaDoc lastRequest = null;
41     private static HttpServletResponse JavaDoc lastResponse = null;
42     
43     private static int maxClientsInt = 0;
44     private static boolean blockRequests = false;
45     
46     static
47     {
48         final String JavaDoc maxClients = CmsPropertyHandler.getMaxClients();
49         if(maxClients != null && !maxClients.equals("") && maxClients.indexOf("@") == -1)
50         {
51             try
52             {
53                 maxClientsInt = new Integer JavaDoc(maxClients).intValue();
54             }
55             catch(Exception JavaDoc e)
56             {
57                 e.printStackTrace();
58             }
59         }
60
61     }
62
63     
64     public static RequestAnalyser getRequestAnalyser()
65     {
66         return instance;
67     }
68     
69     public int getNumberOfCurrentRequests()
70     {
71         return Counter.getNumberOfCurrentRequests();
72     }
73     
74     public void incNumberOfCurrentRequests()
75     {
76         Counter.incNumberOfCurrentRequests();
77         /*
78         synchronized(numberOfCurrentRequests)
79         {
80             numberOfCurrentRequests = new Integer(numberOfCurrentRequests.intValue() + 1);
81         }
82         */

83     }
84
85     public synchronized void decNumberOfCurrentRequests()
86     {
87         Counter.decNumberOfCurrentRequests();
88         /*
89         synchronized(numberOfCurrentRequests)
90         {
91             numberOfCurrentRequests = new Integer(numberOfCurrentRequests.intValue() - 1);
92         }
93         */

94     }
95
96     /*
97     public static int getNumberOfCurrentRequests()
98     {
99         synchronized(currentRequests)
100         {
101             return currentRequests.size();
102         }
103     }
104
105     public static HttpServletRequest getLongestRequests()
106     {
107         HttpServletRequest longestRequest = null;
108         
109         long firstStart = System.currentTimeMillis();
110         synchronized(currentRequests)
111         {
112             Iterator i = currentRequests.iterator();
113             while(i.hasNext())
114             {
115                 HttpServletRequest request = (HttpServletRequest)i.next();
116                 Long startTime = (Long)request.getAttribute("startTime");
117                 if(startTime.longValue() < firstStart)
118                     longestRequest = request;
119             }
120         }
121         
122         return longestRequest;
123     }
124
125     public static int getAverageTimeSpentOnOngoingRequests()
126     {
127         if(getNumberOfCurrentRequests() > 0)
128         {
129             long elapsedTime = 0;
130             long now = System.currentTimeMillis();
131             synchronized(currentRequests)
132             {
133                 Iterator i = currentRequests.iterator();
134                 while(i.hasNext())
135                 {
136                     HttpServletRequest request = (HttpServletRequest)i.next();
137                     Long startTime = (Long)request.getAttribute("startTime");
138                     elapsedTime = elapsedTime + (now - startTime.longValue());
139                 }
140             }
141             
142             return (int)elapsedTime / getNumberOfCurrentRequests();
143         }
144         return 0;
145     }
146
147     public static long getMaxTimeSpentOnOngoingRequests()
148     {
149         HttpServletRequest request = getLongestRequests();
150         if(request != null)
151         {
152             Long firstStart = (Long)request.getAttribute("startTime");
153             long now = System.currentTimeMillis();
154             
155             return (now - firstStart.longValue());
156         }
157         
158         return 0;
159     }
160     
161     public static int getMaxClients()
162     {
163         return maxClientsInt;
164     }
165     
166     public static void setMaxClients(int maxClientsInt)
167     {
168         RequestAnalyser.maxClientsInt = maxClientsInt;
169     }
170     */

171     
172     /*
173     public static List getCurrentRequests()
174     {
175         return currentRequests;
176     }
177     */

178     
179     /*
180     public static boolean getBlockRequests()
181     {
182         return blockRequests;
183     }
184     */

185     
186     public boolean getBlockRequests()
187     {
188         return Blocker.getIsBlocking();
189     }
190
191     public void setBlockRequests(boolean blockRequests)
192     {
193         //try { throw new Exception("Apa"); }catch(Exception e) {e.printStackTrace(); }
194
Blocker.setBlocking(blockRequests);
195     }
196
197 /*
198     public static void setBlockRequests(boolean blockRequests)
199     {
200         //try { throw new Exception("Apa"); }catch(Exception e) {e.printStackTrace(); }
201         RequestAnalyser.blockRequests = blockRequests;
202     }
203 */

204     public static HttpServletRequest JavaDoc getLastRequest()
205     {
206         return lastRequest;
207     }
208     
209     public static void setLastRequest(HttpServletRequest JavaDoc lastRequest)
210     {
211         RequestAnalyser.lastRequest = lastRequest;
212     }
213     
214     public static HttpServletResponse JavaDoc getLastResponse()
215     {
216         return lastResponse;
217     }
218     
219     public static void setLastResponse(HttpServletResponse JavaDoc lastResponse)
220     {
221         RequestAnalyser.lastResponse = lastResponse;
222     }
223 }
224
Popular Tags