View Javadoc

1   /*
2    * The contents of this file are subject to the terms 
3    * of the Common Development and Distribution License 
4    * (the "License").  You may not use this file except 
5    * in compliance with the License.
6    * 
7    * You can obtain a copy of the license at 
8    * http://www.sun.com/cddl/cddl.html. 
9    * See the License for the specific language governing 
10   * permissions and limitations under the License.
11   * 
12   * When distributing Covered Code, include this CDDL 
13   * HEADER in each file and include the License file at 
14   * license.txt.  If applicable, add the following below 
15   * this CDDL HEADER, with the fields enclosed by brackets 
16   * "[]" replaced with your own identifying information: 
17   * Portions Copyright [yyyy] [name of copyright owner]
18   * 
19   * Portions Copyright 2004 eBay, Inc.
20   */
21  package com.ebay.carad.os.vitalsigns;
22  
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.Date;
26  import java.util.HashSet;
27  import java.util.List;
28  
29  import org.apache.log4j.Logger;
30  
31  import com.ebay.carad.os.vitalsigns.dao.IDataDAO;
32  import com.ebay.carad.os.vitalsigns.util.NanoHTTPD;
33  
34  /***
35   * @author Jeremy Thomerson
36   * @version $Id$
37   */
38  public class DashboardAgent extends AbstractListenerContainer implements IDashboardAgent, Runnable {
39  
40      private static final Logger LOGGER = Logger.getLogger(DashboardAgent.class);
41  
42      private IDataDAO mDefaultDataDAO = IDataDAO.NO_OP_INSTANCE;
43      private List /*<IDashboardReportContainer>*/ mReports;
44      private String mDestinationPath;
45      private boolean mRunHTTPServer = false;
46      private int mRunHTTPServerPort = 8080;
47      private String mTitle;
48  
49      public DashboardAgent() {
50          super();
51      }
52  
53      public void run() {
54          if (isRunHTTPServer()) {
55              try {
56                  LOGGER.info("starting HTTP server");
57                  new NanoHTTPD(mRunHTTPServerPort, mDestinationPath);
58                  LOGGER.info("HTTP Server License:\n" + NanoHTTPD.LICENCE + "\n");
59              } catch (Exception ex) {
60                  LOGGER.error("ERROR starting HTTP server [proceeding with dashboard execution]: " + ex.getMessage());
61              }
62          }
63          try {
64              runAllReports();
65          } catch(Exception ex) {
66              ReportingException rex = new ReportingException("error while running reports that was uncaught by any previous ReportingException: " + ex.getMessage(), ex);
67              LOGGER.error(rex.getMessage(), rex);
68              fireListenersException(this, rex, null);
69          }
70          LOGGER.info("DashboardAgent#run() method is complete.  If you used the example config, the app will keep running because the cron job bean has daemon threads running in the background.");
71      }
72  
73      public void runAllReports() {
74          long now = System.currentTimeMillis();
75          LOGGER.info("starting reporting [for timestamp: " + new Date(now) + "]");
76          fireListenersStarted(this);
77          fireReportsListenersStarted();
78          for (ReportContainerIterator it = new ReportContainerIterator(mReports); it.hasNext(); ) { // run logs for each report.
79              IDashboardReport rep = it.next();
80              try {
81                  rep.run(this, now);
82                  fireListenersReportRan(this, rep);
83                  if (rep instanceof AbstractListenerContainer) {
84                  	((AbstractListenerContainer) rep).fireListenersReportRan(this, rep);
85                  }
86  //                fireReportsListenersReportRan(rep);
87              } catch(ReportingException re) {
88                  fireListenersException(this, re, rep);
89              }
90          }
91          fireListenersCompleted(this);
92          fireReportsListenersCompleted();
93          LOGGER.info("completed reporting [for timestamp: " + new Date(now) + "]");
94      }
95  
96      private void fireReportsListenersStarted() {
97          for (ReportContainerIterator it = new ReportContainerIterator(mReports); it.hasNext(); ) { // run logs for each report.
98              IDashboardReport rep = it.next();
99              try {
100                 if (rep instanceof AbstractListenerContainer) {
101                     ((AbstractListenerContainer) rep).fireListenersStarted(this);
102                 }
103             } catch(ReportingException re) {
104                 fireListenersException(this, re, rep);
105             }
106         }
107     }
108     protected void fireListenersException(IReportingListenerContainer agent, ReportingException re, IDashboardReport rep) {
109     	// first call super to call our listeners
110     	super.fireListenersException(agent, re, rep);
111     	// now fire reports listeners
112     	fireReportsListenersReportingException(rep, re);
113     }
114     private void fireReportsListenersReportingException(IDashboardReport report, ReportingException rex) {
115         for (ReportContainerIterator it = new ReportContainerIterator(mReports); it.hasNext(); ) { // run logs for each report.
116             IDashboardReport rep = it.next();
117             try {
118                 if (rep instanceof AbstractListenerContainer) {
119                     ((AbstractListenerContainer) rep).fireListenersException(this, rex, report);
120                 }
121             } catch(ReportingException re) {
122                 fireListenersException(this, re, rep);
123             }
124         }
125     }
126     private void fireReportsListenersCompleted() {
127         for (ReportContainerIterator it = new ReportContainerIterator(mReports); it.hasNext(); ) { // run logs for each report.
128             IDashboardReport rep = it.next();
129             try {
130                 if (rep instanceof AbstractListenerContainer) {
131                     ((AbstractListenerContainer) rep).fireListenersCompleted(this);
132                 }
133             } catch(ReportingException re) {
134                 fireListenersException(this, re, rep);
135             }
136         }
137     }
138     
139     public IDataDAO getDefaultDataDAO() {
140         return mDefaultDataDAO;
141     }
142 
143     public List getReports() {
144         return Collections.unmodifiableList(mReports);
145     }
146 
147     /* IoC Methods */
148     public void setDefaultDataDAO(IDataDAO dao) {
149         mDefaultDataDAO = dao;
150     }
151     public void setReports(List reports) {
152         mReports = reports;
153         checkConsistency();
154     }
155 
156     private void checkConsistency() {
157         Collection containedIDs = new HashSet();
158         for (ReportContainerIterator it = new ReportContainerIterator(mReports); it.hasNext(); ) {
159             IDashboardReport rep = it.next();
160             Integer id = new Integer(rep.getID());
161             if (containedIDs.contains(id)) {
162                 throw new IllegalStateException("Duplicate report ID: " + id);
163             }
164             containedIDs.add(id);
165         }
166     }
167 
168     public String getDestinationPath() {
169         return mDestinationPath;
170     }
171     
172     public void setDestinationPath(String destinationPath) {
173         mDestinationPath = destinationPath;
174     }
175 
176     public String getTitle() {
177         return mTitle;
178     }
179 
180     public void setTitle(String title) {
181         mTitle = title;
182     }
183 
184     public boolean isRunHTTPServer() {
185         return mRunHTTPServer;
186     }
187 
188     public void setRunHTTPServer(boolean runHTTPServer) {
189         mRunHTTPServer = runHTTPServer;
190     }
191 
192     public int getRunHTTPServerPort() {
193         return mRunHTTPServerPort;
194     }
195 
196     public void setRunHTTPServerPort(int runHTTPServerPort) {
197         mRunHTTPServerPort = runHTTPServerPort;
198     }
199     
200 }
201