View Javadoc

1   /*
2    * $Id: RouterStatistics.java 7963 2007-08-21 08:53:15Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.management.stats;
12  
13  import org.mule.management.stats.printers.SimplePrinter;
14  import org.mule.umo.endpoint.UMOEndpoint;
15  import org.mule.umo.endpoint.UMOImmutableEndpoint;
16  
17  import java.io.PrintWriter;
18  import java.util.ArrayList;
19  import java.util.Collection;
20  import java.util.HashMap;
21  import java.util.List;
22  import java.util.Map;
23  
24  /**
25   * <code>RouterStatistics</code> TODO
26   */
27  public class RouterStatistics implements Statistics
28  {
29  
30      /**
31       * Serial version
32       */
33      private static final long serialVersionUID = 4540482357430845065L;
34  
35      public static final int TYPE_INBOUND = 1;
36      public static final int TYPE_OUTBOUND = 2;
37      public static final int TYPE_RESPONSE = 3;
38      public static final int TYPE_NESTED = 4;
39  
40      private boolean enabled;
41      private long notRouted;
42      private long caughtInCatchAll;
43      private long totalRouted;
44      private long totalReceived;
45      private Map routed;
46      private int type;
47  
48      /**
49       * @see org.mule.management.stats.Statistics#clear()
50       */
51      public synchronized void clear()
52      {
53          notRouted = 0;
54          totalRouted = 0;
55          totalReceived = 0;
56          caughtInCatchAll = 0;
57          routed.clear();
58      }
59  
60      /**
61       * @see org.mule.management.stats.Statistics#isEnabled()
62       */
63      public boolean isEnabled()
64      {
65          return enabled;
66      }
67  
68      public void logSummary()
69      {
70          logSummary(new SimplePrinter(System.out));
71      }
72  
73      public void logSummary(PrintWriter printer)
74      {
75          printer.print(this);
76      }
77  
78      /**
79       * @see org.mule.management.stats.Statistics#setEnabled(boolean)
80       */
81      public synchronized void setEnabled(boolean b)
82      {
83          enabled = b;
84      }
85  
86      /**
87       * The constructor
88       */
89      public RouterStatistics(int type)
90      {
91          super();
92          this.type = type;
93          routed = new HashMap();
94      }
95  
96      /**
97       * Increment routed message for multiple endpoints
98       * 
99       * @param endpoints The endpoint collection
100      */
101     public void incrementRoutedMessage(Collection endpoints)
102     {
103         if (endpoints == null || endpoints.isEmpty())
104         {
105             return;
106         }
107         List list = new ArrayList(endpoints);
108         synchronized (list)
109         {
110             for (int i = 0; i < list.size(); i++)
111             {
112                 incrementRoutedMessage((UMOEndpoint) list.get(i));
113             }
114         }
115     }
116 
117     /**
118      * Increment routed message for an endpoint
119      * 
120      * @param endpoint The endpoint
121      */
122     public synchronized void incrementRoutedMessage(UMOImmutableEndpoint endpoint)
123     {
124         if (endpoint == null)
125         {
126             return;
127         }
128 
129         String name = endpoint.getName();
130 
131         Long cpt = (Long) routed.get(name);
132         long count = 0;
133 
134         if (cpt != null)
135         {
136             count = cpt.longValue();
137         }
138 
139         // TODO we should probably use a MutableLong here,
140         // but that might be problematic for remote MBean access (serialization)
141         routed.put(name, new Long(++count));
142 
143         totalRouted++;
144         totalReceived++;
145     }
146 
147     /**
148      * Increment no routed message
149      */
150     public synchronized void incrementNoRoutedMessage()
151     {
152         notRouted++;
153         totalReceived++;
154     }
155 
156     /**
157      * Increment no routed message
158      */
159     public synchronized void incrementCaughtMessage()
160     {
161         caughtInCatchAll++;
162     }
163 
164     /**
165      * @return Returns the notRouted.
166      */
167     public final long getCaughtMessages()
168     {
169         return caughtInCatchAll;
170     }
171 
172     /**
173      * @return Returns the notRouted.
174      */
175     public final long getNotRouted()
176     {
177         return notRouted;
178     }
179 
180     /**
181      * @return Returns the totalReceived.
182      */
183     public final long getTotalReceived()
184     {
185         return totalReceived;
186     }
187 
188     /**
189      * @return Returns the totalRouted.
190      */
191     public final long getTotalRouted()
192     {
193         return totalRouted;
194     }
195 
196     /**
197      * @return Returns the totalRouted.
198      */
199     public final long getRouted(String endpointName)
200     {
201         Long l = (Long) routed.get(endpointName);
202 
203         if (l == null)
204         {
205             return 0;
206         }
207         else
208         {
209             return l.longValue();
210         }
211     }
212 
213     public boolean isInbound()
214     {
215         return type == TYPE_INBOUND;
216     }
217 
218     public Map getRouted()
219     {
220         return routed;
221     }
222 }