1 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 ; 15 import java.util.Date ; 16 import java.util.HashMap ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.StringTokenizer ; 21 import java.util.Vector ; 22 23 31 public class Admin { 32 33 private static final Log LOG = LogFactory.getLog(Admin.class); 34 35 private Log log; 36 37 private Map statsRollers = new HashMap (); 38 39 private CompositeStatisticsListener compositeStatisticsListener = new CompositeStatisticsListener(); 40 41 45 public Admin(ConnectionPoolDefinitionIF definition) throws ProxoolException { 46 log = LogFactory.getLog("org.logicalcobwebs.proxool.stats." + definition.getAlias()); 47 48 StringTokenizer st = new StringTokenizer (definition.getStatistics(), ","); 49 while (st.hasMoreTokens()) { 50 String 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 69 public void connectionReturned(long activeTime) { 70 try { 71 Iterator i = statsRollers.values().iterator(); 72 while (i.hasNext()) { 73 StatsRoller statsRoller = (StatsRoller) i.next(); 74 statsRoller.connectionReturned(activeTime); 75 } 76 } catch (Throwable e) { 77 LOG.error("Stats connectionReturned call failed. Ignoring.", e); 78 } 79 } 80 81 84 public void connectionRefused() { 85 try { 86 Iterator i = statsRollers.values().iterator(); 87 while (i.hasNext()) { 88 StatsRoller statsRoller = (StatsRoller) i.next(); 89 statsRoller.connectionRefused(); 90 } 91 } catch (Exception e) { 92 LOG.error("Stats connectionRefused call failed. Ignoring.", e); 93 } 94 } 95 96 100 public StatisticsIF getStatistics(String token) { 101 try { 102 return ((StatsRoller) statsRollers.get(token)).getCompleteStatistics(); 103 } catch (NullPointerException e) { 104 return null; 105 } 106 } 107 108 111 public void cancelAll() { 112 Iterator 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 statistics = new Vector (); 121 Iterator 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 138 public static SnapshotIF getSnapshot(ConnectionPoolStatisticsIF cps, ConnectionPoolDefinitionIF cpd, Collection connectionInfos) { 139 Snapshot s = new Snapshot(new Date ()); 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 162 163 return s; 164 } 165 166 } 167 168 169 | Popular Tags |