View Javadoc

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