View Javadoc

1   /*
2    * SymmetricDS is an open source database synchronization solution.
3    *   
4    * Copyright (C) Chris Henson <chenson42@users.sourceforge.net>
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, see
18   * <http://www.gnu.org/licenses/>.
19   */
20  package org.jumpmind.symmetric.statistic;
21  
22  import java.util.Date;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import javax.management.Notification;
28  
29  import org.jumpmind.symmetric.common.ParameterConstants;
30  import org.jumpmind.symmetric.model.Node;
31  import org.jumpmind.symmetric.model.StatisticAlertThresholds;
32  import org.jumpmind.symmetric.service.INodeService;
33  import org.jumpmind.symmetric.service.INotificationService;
34  import org.jumpmind.symmetric.service.IParameterService;
35  import org.jumpmind.symmetric.service.IStatisticService;
36  
37  public class StatisticManager implements IStatisticManager {
38  
39      Map<StatisticName, Statistic> statistics;
40  
41      INodeService nodeService;
42  
43      IStatisticService statisticService;
44  
45      INotificationService notificationService;
46  
47      IParameterService parameterService;
48  
49      synchronized public void init() {
50          if (statistics == null) {
51              refresh(new Date());
52          }
53      }
54  
55      synchronized public void flush() {
56          Date captureEndTime = new Date();
57          if (statistics != null) {
58              statisticService.save(statistics.values(), captureEndTime);
59              publishAlerts();
60          }
61          refresh(captureEndTime);
62      }
63  
64      /***
65       * Compare the statistics we have cached against the configured statistic
66       * alert thresholds and publish alerts if we fall outside the range.
67       */
68      synchronized protected void publishAlerts() {
69          if (parameterService.is(ParameterConstants.STATISTIC_THRESHOLD_ALERTS_ENABLED) && statistics != null) {
70              List<StatisticAlertThresholds> thresholds = statisticService.getAlertThresholds();
71              if (thresholds != null) {
72                  for (StatisticAlertThresholds statisticAlertThresholds : thresholds) {
73                      StatisticName name = StatisticName.valueOf(statisticAlertThresholds.getStatisticName());
74                      if (name != null) {
75                          Notification event = statisticAlertThresholds.outsideOfBoundsNotification(statistics.get(name));
76                          if (event != null) {
77                              notificationService.sendNotification(event);
78                          }
79                      }
80                  }
81              }
82          }
83      }
84  
85      synchronized protected void refresh(Date lastCaptureEndTime) {
86          if (statistics == null) {
87              statistics = new HashMap<StatisticName, Statistic>();
88          }
89  
90          statistics.clear();
91  
92          Node node = nodeService.findIdentity();
93          String nodeId = "Unknown";
94          if (node != null) {
95              nodeId = node.getNodeId();
96          }
97  
98          StatisticName[] all = StatisticName.values();
99          for (StatisticName statisticName : all) {
100             statistics.put(statisticName, new Statistic(statisticName, nodeId, lastCaptureEndTime == null ? new Date()
101                     : lastCaptureEndTime));
102         }
103 
104     }
105 
106     public Statistic getStatistic(StatisticName statisticName) {
107         this.init();
108         return statistics.get(statisticName);
109     }
110 
111     public void setNodeService(INodeService nodeService) {
112         this.nodeService = nodeService;
113     }
114 
115     public void setStatisticService(IStatisticService statisticService) {
116         this.statisticService = statisticService;
117     }
118 
119     public void setNotificationService(INotificationService notificationService) {
120         this.notificationService = notificationService;
121     }
122 
123     public void setParameterService(IParameterService parameterService) {
124         this.parameterService = parameterService;
125     }
126 }