001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.core.qos.metrics;
028:
029: import java.io.PrintWriter;
030:
031: import javax.servlet.http.HttpServlet;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.cougaar.core.component.ServiceBroker;
036: import org.cougaar.core.service.ServletService;
037:
038: /**
039: * This Servlet provides url access to the MetricsUpdateService. The
040: * required URI parameters are <code>key</code> and <code>value</code>.
041: */
042: public class MetricsWriterServlet extends HttpServlet implements
043: Constants {
044: private MetricsUpdateService metricsUpdateService;
045:
046: public MetricsWriterServlet(ServiceBroker sb) {
047: // Register our servlet with servlet service
048: ServletService servletService = (ServletService) sb.getService(
049: this , ServletService.class, null);
050: if (servletService == null) {
051: throw new RuntimeException(
052: "Unable to obtain ServletService");
053: }
054: try {
055: servletService.register(getPath(), this );
056: } catch (Exception e) {
057: throw new RuntimeException(
058: "Unable to register servlet at path <" + getPath()
059: + ">: " + e.getMessage());
060: }
061:
062: // get metrics service
063: try {
064: metricsUpdateService = (MetricsUpdateService) sb
065: .getService(this , MetricsUpdateService.class, null);
066: } catch (Exception e) {
067: throw new RuntimeException(
068: "Unable to get MetricsUpdateService: "
069: + e.getMessage());
070: }
071: }
072:
073: public String getPath() {
074: return "/metrics/writer";
075: }
076:
077: public String getTitle() {
078: return "Remote Metrics Writer for Node";
079: }
080:
081: /*
082: * Parses params, and send out either a propertylist of
083: * metrics(java version), or a string of xml-formatted text,
084: * through a serialized byte array.
085: */
086: public void printPage(HttpServletRequest request, PrintWriter out) {
087: try {
088:
089: // parses params
090: String key = request.getParameter("key");
091: String value = request.getParameter("value");
092:
093: if (key == null || value == null) {
094: out.print("Key or Value is null");
095: return;
096: }
097:
098: Metric metric = new MetricImpl(Double.parseDouble(value),
099: USER_DEFAULT_CREDIBILITY, null, request
100: .getRemoteHost());
101: metricsUpdateService.updateValue(key, metric);
102: out.print("Key: " + key + ", Value: " + value + "From: "
103: + request.getRemoteHost() + "\n");
104: } catch (Exception e) {
105: }
106: }
107:
108: // servlet requirement - pass to our print method to handle
109: public void doGet(HttpServletRequest request,
110: HttpServletResponse response) throws java.io.IOException {
111: PrintWriter out = response.getWriter();
112: printPage(request, out);
113: }
114: }
|