View Javadoc

1   /*
2    * $Id: RouterStatistics.java 20813 2010-12-21 11:37:48Z dirk.olmes $
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      public synchronized void clear()
50      {
51          notRouted = 0;
52          totalRouted = 0;
53          totalReceived = 0;
54          caughtInCatchAll = 0;
55          routed.clear();
56      }
57  
58      /**
59       * @see org.mule.api.management.stats.Statistics#isEnabled()
60       */
61      public boolean isEnabled()
62      {
63          return enabled;
64      }
65  
66      public void logSummary()
67      {
68          logSummary(new SimplePrinter(System.out));
69      }
70  
71      public void logSummary(PrintWriter printer)
72      {
73          printer.print(this);
74      }
75  
76      public synchronized void setEnabled(boolean b)
77      {
78          enabled = b;
79      }
80  
81      /**
82       * The constructor
83       */
84      public RouterStatistics(int type)
85      {
86          super();
87          this.type = type;
88          routed = new HashMap();
89      }
90  
91      /**
92       * Increment routed message for multiple targets
93       *
94       * @param endpoints The endpoint collection
95       */
96      public void incrementRoutedMessage(Collection endpoints)
97      {
98          if (endpoints == null || endpoints.isEmpty())
99          {
100             return;
101         }
102         List list = new ArrayList(endpoints);
103         synchronized (list)
104         {
105             for (int i = 0; i < list.size(); i++)
106             {
107                 incrementRoutedMessage(list.get(i));
108             }
109         }
110     }
111 
112     /**
113      * Increment routed message for an endpoint
114      *
115      * @param endpoint The endpoint
116      */
117     public synchronized void incrementRoutedMessage(Object endpoint)
118     {
119         if (endpoint == null)
120         {
121             return;
122         }
123 
124         String name;
125         if (endpoint instanceof ImmutableEndpoint)
126         {
127             name = ((ImmutableEndpoint)endpoint).getName();
128         }
129         else
130         {
131             name = endpoint.toString();
132         }
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 }