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.dataretrievers;
22  
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.Map;
26  
27  import org.apache.commons.lang.exception.ExceptionUtils;
28  import org.apache.log4j.Logger;
29  
30  import com.ebay.carad.os.vitalsigns.IDashboardAgent;
31  import com.ebay.carad.os.vitalsigns.IDataRetriever;
32  import com.ebay.carad.os.vitalsigns.dao.ISqlDAO;
33  import com.ebay.carad.os.vitalsigns.templates.ITemplatizable;
34  import com.ebay.carad.os.vitalsigns.util.MessageFormatUtil;
35  
36  
37  /***
38   * Takes the output of a simple one-cell query (column name "data") 
39   * from the production DB and logs it.
40   * 
41   * @author Jeremy Kraybill
42   * @author Jeremy Thomerson
43   * @version $Id$
44   */
45  public class QueryLogger implements IDataRetriever, ITemplatizable {
46  	
47  	private static final String COLUMN_NAME = "data";
48  	/*** Class logger. */
49  	private static final Logger LOGGER = Logger.getLogger(QueryLogger.class);
50      public static final Float ZERO = new Float(0);
51      
52      private ISqlDAO mSqlDAO;
53      private String mQuery; // this query must contain a column called "data"
54  	
55  	/***
56  	 * Sets the SQL query for this logger.  Must return the column "data", which 
57  	 * is the column that will be logged.
58  	 * 
59  	 * @param query the SQL query to set
60  	 */
61  	public void setQuery(String query) {
62  		if (query.toUpperCase(Locale.getDefault()).indexOf("AS DATA") == -1) {
63  			LOGGER.warn("the phrase 'as " + COLUMN_NAME + "' is not in the SQL [" + query + "] anywhere.  This may indicate that the column will not be returned, and the query is invalid for logging a data point");
64  		}
65  		mQuery = query;
66  	}
67  	
68      /***
69       * @return Returns the query.
70       */
71      public String getQuery() {
72          return mQuery;
73      }
74  
75  	protected Float getValue() {
76  		LOGGER.debug("running query: " + mQuery);
77  		List result = mSqlDAO.findBySqlQuery(mQuery);
78  		if (result.size() == 0) { // no rows returned
79  			return null;
80  		}
81  		if (!((Map) result.get(0)).containsKey(COLUMN_NAME)) {
82  			LOGGER.error("column '" + COLUMN_NAME + "' is not included in results of query, therfore logging null [" + mQuery + "]");
83  		}
84  		Object data = ((Map)result.get(0)).get(COLUMN_NAME);
85  		return (data == null) ? null : Float.valueOf(data.toString());			
86  	}
87  
88  	public Float getData() {
89  		Float value = ZERO;
90  		try {
91  			value = getValue();
92  		} catch (Exception e) {
93  			LOGGER.error(ExceptionUtils.getStackTrace(e));
94  		}
95  		return value;
96  	}
97  
98      /***
99       * This implementation treats the query set on this instance as a MessageFormat,
100      * and applies the substitutions to that format using <tt>MessageFormatUtil</tt>.
101      * 
102      * @see java.text.MessageFormat
103      * @see MessageFormatUtil#format(String, Object[])
104      * @see com.ebay.carad.os.vitalsigns.templates.ITemplatizable#templatize(java.lang.Object[])
105      */
106     public void templatize(Object[] substitutions) {
107     	mQuery = MessageFormatUtil.format(mQuery, substitutions);
108     }
109 
110     public Float getData(IDashboardAgent agent) {
111         return getData();
112     }
113 
114     public ISqlDAO getSqlDAO() {
115         return mSqlDAO;
116     }
117 
118     public void setSqlDAO(ISqlDAO sqlDAO) {
119         mSqlDAO = sqlDAO;
120     }
121     public Object clone() throws CloneNotSupportedException {
122         return super.clone();
123     }
124 }
125