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.io.IOException;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.io.StringWriter;
28  import java.net.MalformedURLException;
29  import java.net.URL;
30  import java.net.URLConnection;
31  import java.util.regex.Matcher;
32  import java.util.regex.Pattern;
33  
34  import org.apache.log4j.Logger;
35  
36  import com.ebay.carad.os.vitalsigns.IDashboardAgent;
37  import com.ebay.carad.os.vitalsigns.IDataRetriever;
38  import com.ebay.carad.os.vitalsigns.ReportingException;
39  
40  /***
41   * @author Jeremy Thomerson
42   * @version $Id$
43   */
44  /***
45   * @author Jeremy Thomerson
46   * @version $Id$
47   */
48  public class UrlRegexDataRetriever implements IDataRetriever {
49  
50      private static final Logger LOGGER = Logger.getLogger(UrlRegexDataRetriever.class);
51  
52      private URL mUrl;
53      private Pattern mRegex;
54      
55      public Float getData(IDashboardAgent agent) {
56          String contents = getURLContents();
57          Matcher matcher = mRegex.matcher(contents);
58          if (matcher.find()) {
59              String grp = matcher.group(1);
60              try {
61              	grp = grp.replaceAll(",", "");
62                  return Float.valueOf(grp);
63              } catch (NumberFormatException nfe) {
64                  String message = "error parsing result of regex [" + grp + "]: " + nfe.getMessage();
65                  LOGGER.error(message, nfe);
66                  throw new ReportingException(message, nfe);
67              }
68          }
69          LOGGER.warn("regex did not match [regex: " + mRegex + "; contents: " + contents);
70          return IDataRetriever.ZERO;
71      }
72      
73      private String getURLContents() {
74          try {
75              URLConnection conn = mUrl.openConnection();
76              InputStream is = conn.getInputStream();
77              Reader reader = new InputStreamReader(is);
78              StringWriter writer = new StringWriter();
79              
80              // Transfer bytes from in to out
81              char[] buf = new char[1024];
82              int len;
83              while ((len = reader.read(buf)) > 0) {
84                  writer.write(buf, 0, len);
85              }
86              
87              is.close();
88              writer.flush();
89              writer.close();
90              
91              return writer.toString();
92          } catch (IOException ioe) {
93              throw new ReportingException("Exception while pulling URL contents: " + ioe.getMessage(), ioe);
94          }
95      }
96      
97      public void setUrl(String url) {
98          try {
99              mUrl = new URL(url);
100         } catch (MalformedURLException mfue) {
101             throw new RuntimeException("Exception while creating URL: " + mfue.getMessage(), mfue);
102         }
103     }
104     
105     public void setRegex(String regex) {
106         mRegex = Pattern.compile(regex);
107     }
108 
109     public Object clone() throws CloneNotSupportedException {
110         return super.clone();
111     }
112 }
113