View Javadoc

1   /*
2    * $Id: RouterStatistics.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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_BINDING = 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 targets
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(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(Object endpoint)
124     {
125         if (endpoint == null)
126         {
127             return;
128         }
129 
130         String name;
131         if (endpoint instanceof ImmutableEndpoint)
132         {
133             name = ((ImmutableEndpoint)endpoint).getName();
134         }
135         else
136         {
137             name = endpoint.toString();
138         }
139 
140         Long cpt = (Long) routed.get(name);
141         long count = 0;
142 
143         if (cpt != null)
144         {
145             count = cpt.longValue();
146         }
147 
148         // TODO we should probably use a MutableLong here,
149         // but that might be problematic for remote MBean access (serialization)
150         routed.put(name, new Long(++count));
151 
152         totalRouted++;
153         totalReceived++;
154     }
155 
156     /**
157      * Increment no routed message
158      */
159     public synchronized void incrementNoRoutedMessage()
160     {
161         notRouted++;
162         totalReceived++;
163     }
164 
165     /**
166      * Increment no routed message
167      */
168     public synchronized void incrementCaughtMessage()
169     {
170         caughtInCatchAll++;
171     }
172 
173     /**
174      * @return Returns the notRouted.
175      */
176     public final long getCaughtMessages()
177     {
178         return caughtInCatchAll;
179     }
180 
181     /**
182      * @return Returns the notRouted.
183      */
184     public final long getNotRouted()
185     {
186         return notRouted;
187     }
188 
189     /**
190      * @return Returns the totalReceived.
191      */
192     public final long getTotalReceived()
193     {
194         return totalReceived;
195     }
196 
197     /**
198      * @return Returns the totalRouted.
199      */
200     public final long getTotalRouted()
201     {
202         return totalRouted;
203     }
204 
205     /**
206      * @return Returns the totalRouted.
207      */
208     public final long getRouted(String endpointName)
209     {
210         Long l = (Long) routed.get(endpointName);
211 
212         if (l == null)
213         {
214             return 0;
215         }
216         else
217         {
218             return l.longValue();
219         }
220     }
221 
222     public boolean isInbound()
223     {
224         return type == TYPE_INBOUND;
225     }
226 
227     public Map getRouted()
228     {
229         return routed;
230     }
231 }