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