View Javadoc

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