KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > admin > Admin


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.admin;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF;
11 import org.logicalcobwebs.proxool.ConnectionPoolStatisticsIF;
12 import org.logicalcobwebs.proxool.ProxoolException;
13
14 import java.util.Collection JavaDoc;
15 import java.util.Date JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21 import java.util.Vector JavaDoc;
22
23 /**
24  * Provides statistics about the performance of a pool.
25  *
26  * @version $Revision: 1.9 $, $Date: 2006/01/18 14:39:57 $
27  * @author bill
28  * @author $Author: billhorsman $ (current maintainer)
29  * @since Proxool 0.7
30  */

31 public class Admin {
32
33     private static final Log LOG = LogFactory.getLog(Admin.class);
34
35     private Log log;
36
37     private Map JavaDoc statsRollers = new HashMap JavaDoc();
38
39     private CompositeStatisticsListener compositeStatisticsListener = new CompositeStatisticsListener();
40
41     /**
42      * @param definition gives access to pool definition
43      * @param definition see {@link org.logicalcobwebs.proxool.ConnectionPoolDefinitionIF#getStatistics definition}
44      */

45     public Admin(ConnectionPoolDefinitionIF definition) throws ProxoolException {
46         log = LogFactory.getLog("org.logicalcobwebs.proxool.stats." + definition.getAlias());
47
48         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(definition.getStatistics(), ",");
49         while (st.hasMoreTokens()) {
50             String JavaDoc token = st.nextToken();
51             statsRollers.put(token, new StatsRoller(definition.getAlias(), compositeStatisticsListener, token));
52         }
53
54         if (definition.getStatisticsLogLevel() != null) {
55             compositeStatisticsListener.addListener(new StatisticsLogger(log, definition.getStatisticsLogLevel()));
56         }
57
58     }
59
60     public void addStatisticsListener(StatisticsListenerIF statisticsListener) {
61         this.compositeStatisticsListener.addListener(statisticsListener);
62     }
63
64
65     /**
66      * Call this every time an active connection is returned to the pool
67      * @param activeTime how long the connection was active
68      */

69     public void connectionReturned(long activeTime) {
70         try {
71             Iterator JavaDoc i = statsRollers.values().iterator();
72             while (i.hasNext()) {
73                 StatsRoller statsRoller = (StatsRoller) i.next();
74                 statsRoller.connectionReturned(activeTime);
75             }
76         } catch (Throwable JavaDoc e) {
77             LOG.error("Stats connectionReturned call failed. Ignoring.", e);
78         }
79     }
80
81     /**
82      * Call this every time a connection is refused
83      */

84     public void connectionRefused() {
85         try {
86             Iterator JavaDoc i = statsRollers.values().iterator();
87             while (i.hasNext()) {
88                 StatsRoller statsRoller = (StatsRoller) i.next();
89                 statsRoller.connectionRefused();
90             }
91         } catch (Exception JavaDoc e) {
92             LOG.error("Stats connectionRefused call failed. Ignoring.", e);
93         }
94     }
95
96     /**
97      * Returns the most recent sample that has completed its period
98      * @return sample (or null if no statistics are complete yet)
99      */

100     public StatisticsIF getStatistics(String JavaDoc token) {
101         try {
102             return ((StatsRoller) statsRollers.get(token)).getCompleteStatistics();
103         } catch (NullPointerException JavaDoc e) {
104             return null;
105         }
106     }
107
108     /**
109      * Cancels the timer that outputs the stats
110      */

111     public void cancelAll() {
112         Iterator JavaDoc i = statsRollers.values().iterator();
113         while (i.hasNext()) {
114             StatsRoller statsRoller = (StatsRoller) i.next();
115             statsRoller.cancel();
116         }
117     }
118
119     public StatisticsIF[] getStatistics() {
120         List JavaDoc statistics = new Vector JavaDoc();
121         Iterator JavaDoc i = statsRollers.values().iterator();
122         while (i.hasNext()) {
123             StatsRoller statsRoller = (StatsRoller) i.next();
124             StatisticsIF s = statsRoller.getCompleteStatistics();
125             if (s != null) {
126                 statistics.add(s);
127             }
128         }
129         return (StatisticsIF[]) statistics.toArray(new StatisticsIF[statistics.size()]);
130     }
131
132     /**
133      * Get a new snapshot
134      * @param cps used to help populate the snapshot
135      * @param cpd used to help populate the snapshot
136      * @return snapshot
137      */

138     public static SnapshotIF getSnapshot(ConnectionPoolStatisticsIF cps, ConnectionPoolDefinitionIF cpd, Collection JavaDoc connectionInfos) {
139         Snapshot s = new Snapshot(new Date JavaDoc());
140
141         s.setDateStarted(cps.getDateStarted());
142         s.setActiveConnectionCount(cps.getActiveConnectionCount());
143         s.setAvailableConnectionCount(cps.getAvailableConnectionCount());
144         s.setOfflineConnectionCount(cps.getOfflineConnectionCount());
145         s.setMaximumConnectionCount(cpd.getMaximumConnectionCount());
146         s.setServedCount(cps.getConnectionsServedCount());
147         s.setRefusedCount(cps.getConnectionsRefusedCount());
148         s.setConnectionInfos(connectionInfos);
149         s.setConnectionCount(cps.getConnectionCount());
150
151         /*
152         if (s.getActiveConnectionCount() != getCount(s.getConnectionInfos(), ConnectionInfoIF.STATUS_ACTIVE)) {
153             LOG.error("activeCount disparity: " + s.getActiveConnectionCount() + " != " + getCount(s.getConnectionInfos(), ConnectionInfoIF.STATUS_ACTIVE));
154         }
155         if (s.getAvailableConnectionCount() != getCount(s.getConnectionInfos(), ConnectionInfoIF.STATUS_AVAILABLE)) {
156             LOG.error("activeCount disparity: " + s.getAvailableConnectionCount() + " != " + getCount(s.getConnectionInfos(), ConnectionInfoIF.STATUS_AVAILABLE));
157         }
158         if (s.getOfflineConnectionCount() != getCount(s.getConnectionInfos(), ConnectionInfoIF.STATUS_OFFLINE)) {
159             LOG.error("offlineCount disparity: " + s.getOfflineConnectionCount() + " != " + getCount(s.getConnectionInfos(), ConnectionInfoIF.STATUS_OFFLINE));
160         }
161         */

162
163         return s;
164     }
165
166 }
167
168
169 /*
170  Revision history:
171  $Log: Admin.java,v $
172  Revision 1.9 2006/01/18 14:39:57 billhorsman
173  Unbundled Jakarta's Commons Logging.
174
175  Revision 1.8 2005/10/02 12:32:01 billhorsman
176  Make connectionCount available to statistics
177
178  Revision 1.7 2003/10/27 20:26:19 billhorsman
179  connectionReturned() and connectionRefused() calls will now log any errors and continue rather than
180  possibly throwing RuntimeExceptions back to the caller. In principle, stats should not cause problems
181  to the core code. (No evidence of this happening - but it's more robust now.)
182
183  Revision 1.6 2003/08/30 14:54:04 billhorsman
184  Checkstyle
185
186  Revision 1.5 2003/03/11 14:51:55 billhorsman
187  more concurrency fixes relating to snapshots
188
189  Revision 1.4 2003/03/10 23:43:14 billhorsman
190  reapplied checkstyle that i'd inadvertently let
191  IntelliJ change...
192
193  Revision 1.3 2003/03/10 15:26:50 billhorsman
194  refactoringn of concurrency stuff (and some import
195  optimisation)
196
197  Revision 1.2 2003/03/03 11:11:58 billhorsman
198  fixed licence
199
200  Revision 1.1 2003/02/19 23:36:51 billhorsman
201  renamed monitor package to admin
202
203  Revision 1.8 2003/02/07 15:08:51 billhorsman
204  removed redundant accessor
205
206  Revision 1.7 2003/02/07 14:16:45 billhorsman
207  support for StatisticsListenerIF
208
209  Revision 1.6 2003/02/06 17:41:05 billhorsman
210  now uses imported logging
211
212  Revision 1.5 2003/02/05 00:20:27 billhorsman
213  getSnapshot is now static (because it can be)
214
215  Revision 1.4 2003/02/04 15:59:49 billhorsman
216  finalize now shuts down StatsRoller timer
217
218  Revision 1.3 2003/01/31 16:53:21 billhorsman
219  checkstyle
220
221  Revision 1.2 2003/01/31 16:38:51 billhorsman
222  doc (and removing public modifier for classes where possible)
223
224  Revision 1.1 2003/01/31 11:35:57 billhorsman
225  improvements to servlet (including connection details)
226
227  Revision 1.2 2003/01/31 00:28:57 billhorsman
228  now handles multiple statistics
229
230  Revision 1.1 2003/01/30 17:20:19 billhorsman
231  fixes, improvements and doc
232
233  */
Popular Tags