001: /*
002:
003: * <copyright>
004: *
005: * Copyright 2002-2007 BBNT Solutions, LLC
006: * under sponsorship of the Defense Advanced Research Projects
007: * Agency (DARPA).
008: *
009: * You can redistribute this software and/or modify it under the
010: * terms of the Cougaar Open Source License as published on the
011: * Cougaar Open Source Website (www.cougaar.org).
012: *
013: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
014: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
015: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
016: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
017: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
018: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
019: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
020: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
021: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
022: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
023: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
024: *
025: * </copyright>
026:
027: */
028:
029: package org.cougaar.qos.qrs;
030:
031: import java.io.InputStream;
032: import java.io.PrintWriter;
033: import java.io.StringWriter;
034: import java.net.URL;
035: import java.util.Properties;
036:
037: import org.cougaar.util.log.Logger;
038:
039: /**
040: * A DataFeed which returns fixed values that it reads from a java properties
041: * data in a url. Handy for supplying defaults.
042: */
043: public class PropertiesDataFeed extends AbstractDataFeed implements
044: Constants {
045: private static final long PROPERTIES_TIME = System
046: .currentTimeMillis();
047: private static final double DEFAULT_VALUE = 0.0;
048: private static final double FEED_DEFAULT_CREDIBILITY = SYS_DEFAULT_CREDIBILITY;
049:
050: private final Properties props = new Properties();
051: private String urlString;
052: private final Logger logger;
053:
054: /**
055: * The args should be command-line style, and should include the '-url'
056: * followed by the url of the property data. These args would typically
057: * appear in a kernel config file.
058: */
059: public PropertiesDataFeed(String[] args) {
060: super ();
061:
062: logger = Logging.getLogger(PropertiesDataFeed.class);
063:
064: if (args == null) {
065: logger.error(this .getName()
066: + ":PropertiesDataFeed: no args");
067: return;
068: }
069:
070: String url = null;
071: int i = 0;
072: for (i = 0; i < args.length; i++) {
073: String arg = args[i];
074: if (arg.equals("-url")) {
075: url = args[++i];
076: } else {
077: logger.error(this .getName()
078: + ":PropertiesDataFeed: unknown argument "
079: + arg);
080: }
081: }
082:
083: initialize(url);
084: }
085:
086: public PropertiesDataFeed(String properties_url) {
087: logger = Logging.getLogger(PropertiesDataFeed.class);
088: initialize(properties_url);
089: }
090:
091: protected InputStream openURL(String urlString) {
092: URL url;
093: try {
094: url = new URL(urlString);
095: } catch (java.net.MalformedURLException bad_url) {
096: logger.error(urlString + ":PropertiesDataFeed:", bad_url);
097: return null;
098: }
099:
100: try {
101: return url.openStream();
102: } catch (java.io.IOException open_error) {
103: logger.error(url + ":PropertiesDataFeed:", open_error);
104: return null;
105: }
106:
107: }
108:
109: private void initialize(String properties_url) {
110:
111: if (properties_url == null) {
112: logger.error(":PropertiesDataFeed: no URL");
113: return;
114: }
115:
116: InputStream stream = openURL(properties_url);
117: if (stream == null) {
118: return;
119: }
120:
121: // good url
122: urlString = properties_url;
123:
124: try {
125: props.load(stream);
126: } catch (java.io.IOException load_error) {
127: logger.error(properties_url + ":PropertiesDataFeed:",
128: load_error);
129: }
130:
131: try {
132: stream.close();
133: } catch (java.io.IOException close_error) {
134: logger.error(properties_url + "PropertiesDataFeed:",
135: close_error);
136: }
137:
138: if (logger.isDebugEnabled()) {
139: StringWriter raw = new StringWriter();
140: PrintWriter writer = new PrintWriter(raw);
141: writer.print(properties_url);
142: writer.print(" Contents:");
143: props.list(writer);
144: logger.debug(raw.toString());
145: writer.close();
146: }
147:
148: }
149:
150: // no-op
151: public void removeListenerForKey(DataFeedListener listener,
152: String key) {
153: }
154:
155: // no-op
156: public void addListenerForKey(DataFeedListener listener, String key) {
157: }
158:
159: public DataValue lookup(String key) {
160: double value = DEFAULT_VALUE;
161: double credibility = FEED_DEFAULT_CREDIBILITY;
162: String provenance = null;
163: String units = null;
164: String value_string = props.getProperty(key + KEY_SEPR
165: + "value");
166: if (value_string != null) {
167: try {
168: value = Double.parseDouble(value_string);
169: } catch (NumberFormatException bad_value) {
170: logger.error(this .getName() + ":PropertiesDataFeed:",
171: bad_value);
172: }
173:
174: String credibility_string = props.getProperty(key
175: + KEY_SEPR + "credibility");
176: // no credibility -> use the default credibility
177: if (credibility_string != null) {
178: try {
179: credibility = Double
180: .parseDouble(credibility_string);
181: } catch (NumberFormatException bad_credibility) {
182: logger.error(this .getName()
183: + ":PropertiesDataFeed:", bad_credibility);
184: }
185: } else {
186: if (logger.isDebugEnabled()) {
187: logger.debug(this .getName()
188: + ":PropertiesDataFeed for " + urlString
189: + ": no credibility for " + key);
190: }
191: }
192: provenance = props.getProperty(key + KEY_SEPR
193: + "provenance", urlString);
194: units = props.getProperty(key + KEY_SEPR + "units");
195: } else {
196: // no value in Properties -> key is invalid
197: if (logger.isDebugEnabled()) {
198: logger.debug(this .getName()
199: + ":PropertiesDataFeed for " + urlString
200: + ": no value for " + key);
201: }
202: credibility = 0.0;
203: }
204:
205: return new DataValue(new Double(value), credibility, units,
206: provenance, PROPERTIES_TIME, 0l);
207:
208: }
209:
210: }
|