KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > object > net > ChannelStatsImpl


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.object.net;
5
6 import EDU.oswego.cs.dl.util.concurrent.ConcurrentReaderHashMap;
7
8 import com.tc.net.protocol.tcm.ChannelID;
9 import com.tc.net.protocol.tcm.MessageChannel;
10 import com.tc.stats.counter.Counter;
11 import com.tc.stats.counter.sampled.SampledCounterConfig;
12 import com.tc.stats.counter.sampled.SampledCounterManager;
13
14 import java.util.Map JavaDoc;
15
16 /**
17  * A helper class to make accessing channel specific stats objects a little
18  * easier. This class is sorta yucky and definitely will need to evolve
19  */

20 public class ChannelStatsImpl implements ChannelStats {
21
22   private static final String JavaDoc DSO_STATS_MAP = "dso_stats_map";
23   private static final SampledCounterConfig DEFAULT_CONFIG = new SampledCounterConfig(1, 300, true, 0L);
24
25   private final SampledCounterManager counterManager;
26   private final DSOChannelManager channelManager;
27
28   public ChannelStatsImpl(SampledCounterManager counterManager, DSOChannelManager channelManager) {
29     this.counterManager = counterManager;
30     this.channelManager = channelManager;
31   }
32
33   public Counter getCounter(MessageChannel channel, String JavaDoc name) {
34     return getCounter(getStatsMap(channel), name);
35   }
36
37   private Counter getCounter(Map statsMap, String JavaDoc name) {
38     Counter rv = (Counter) statsMap.get(name);
39     if (rv != null) return rv;
40
41     synchronized (statsMap) {
42       if (statsMap.containsKey(name)) { return (Counter) statsMap.get(name); }
43
44       // XXX: We'll need a way to override this at some point, we'll probably
45
// want differing configs for the different
46
// counters, and not every counter needs to be one of the sampled type
47
// (probably)
48
rv = counterManager.createCounter(DEFAULT_CONFIG);
49       statsMap.put(name, rv);
50       return rv;
51     }
52
53   }
54
55   private static Map getStatsMap(MessageChannel channel) {
56     Map rv = (Map) channel.getAttachment(DSO_STATS_MAP);
57     if (rv != null) { return rv; }
58     channel.addAttachment(DSO_STATS_MAP, new ConcurrentReaderHashMap(), false);
59     return (Map) channel.getAttachment(DSO_STATS_MAP);
60   }
61
62   public void notifyTransaction(ChannelID channelID) {
63     try {
64       MessageChannel channel = channelManager.getActiveChannel(channelID);
65       getCounter(channel, TXN_RATE).increment();
66     } catch (NoSuchChannelException e) {
67       //
68
}
69   }
70
71 }
72
Popular Tags