001: /*
002: * <copyright>
003: *
004: * Copyright 2002-2007 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: package org.cougaar.qos.qrs.example;
027:
028: import java.io.FileInputStream;
029: import java.io.InputStream;
030: import java.net.URL;
031: import java.util.Properties;
032:
033: import org.cougaar.qos.ResourceStatus.ResourceDescriptionParseException;
034: import org.cougaar.qos.ResourceStatus.ResourceNode;
035: import org.cougaar.qos.qrs.BoundDataFormula;
036: import org.cougaar.qos.qrs.CorbaUtils;
037: import org.cougaar.qos.qrs.DataValue;
038: import org.cougaar.qos.qrs.Logging;
039: import org.cougaar.qos.qrs.NullFormulaException;
040: import org.cougaar.qos.qrs.PathParser;
041: import org.cougaar.qos.qrs.RSS;
042: import org.cougaar.util.log.Logger;
043:
044: public class SimpleRSSExample {
045:
046: public DataValue blockingQuery(String rssQueryString) {
047: ResourceNode[] path = null;
048: try {
049: path = PathParser.parsePath(rssQueryString);
050: } catch (ResourceDescriptionParseException e) {
051: System.out.println(e.getMessage());
052: e.printStackTrace();
053: return new DataValue();
054: }
055: BoundDataFormula bdf = null;
056: try {
057: bdf = new BoundDataFormula(path, false, null);
058: } catch (NullFormulaException e) {
059: System.out.println(e.getMessage());
060: e.printStackTrace();
061: return new DataValue();
062: }
063: DataValue value = (DataValue) bdf.getCurrentValue();
064: return value;
065: }
066:
067: /**
068: * @param args
069: */
070: public static void main(String[] args) {
071:
072: SimpleRSSExample impl = new SimpleRSSExample();
073: String config = null;
074: boolean gui = false;
075:
076: int i = 0;
077: while (i < args.length) {
078: String arg = args[i++];
079: if (arg.equals("-gui")) {
080: gui = true;
081: } else if (arg.equals("-rss.config")) {
082: config = args[i++];
083: } else {
084: System.out.println("Do not understand switch " + arg);
085: }
086: }
087:
088: Logger logger = Logging.getLogger(CorbaUtils.class);
089: if (logger.isDebugEnabled()) {
090: logger.debug("Arg Switches " + " -gui=" + gui
091: + " -rss.config=" + config);
092: }
093:
094: // make RSS, using conf
095: Properties props = new Properties();
096: if (gui) {
097: String gui_title = "RSS on " + CorbaUtils.hostname();
098: props.setProperty(RSS.getGUI_PROPERTY(), gui_title);
099: }
100:
101: if (config != null) {
102: InputStream is = null;
103: try {
104: URL url = new URL(config);
105: is = url.openStream();
106: } catch (Exception ex) {
107: // try it as a filename
108: try {
109: is = new FileInputStream(config);
110: } catch (Exception e) {
111: logger.error("Error opening " + config + ": " + ex);
112: }
113: }
114:
115: if (is != null) {
116: try {
117: props.load(is);
118: is.close();
119: } catch (java.io.IOException ex) {
120: logger.error("Error loading RSS properties from "
121: + config + ": " + ex);
122: }
123: }
124: }
125:
126: if (logger.isDebugEnabled()) {
127: logger.debug("Properties=" + props);
128: }
129:
130: RSS.makeInstance(props);
131:
132: CorbaUtils.registerContexts();
133:
134: System.out.println("RSS ready");
135:
136: String queryString = new String("Host(" + CorbaUtils.hostname()
137: + "):Jips");
138: System.out.println("RSS Query String" + queryString);
139: DataValue bbnValueObj = impl.blockingQuery(queryString);
140: System.out.println(queryString + "=" + bbnValueObj);
141: bbnValueObj = impl.blockingQuery(queryString);
142: System.out.println(queryString + "=" + bbnValueObj);
143: bbnValueObj = impl.blockingQuery(queryString);
144: System.out.println(queryString + "=" + bbnValueObj);
145: bbnValueObj = impl.blockingQuery(queryString);
146: System.out.println(queryString + "=" + bbnValueObj);
147: }
148: }
|