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.rss;
028:
029: import java.io.InputStream;
030: import java.net.URI;
031:
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.qos.metrics.DataFeedRegistrationService;
034: import org.cougaar.core.qos.metrics.QosComponent;
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.qos.qrs.PropertiesDataFeed;
037: import org.cougaar.util.ConfigFinder;
038: import org.cougaar.util.log.Logger;
039: import org.cougaar.util.log.Logging;
040:
041: /**
042: * This Components uses the {@link DataFeedRegistrationService} to register an
043: * RSS properties feed that uses the {@link ConfigFinder} to resolve the URL.
044: */
045: public class ConfigFinderDataFeedComponent extends QosComponent {
046: public static final String CONFIG_PROTOCOL = "cougaarconfig";
047:
048: public ConfigFinderDataFeedComponent() {
049: }
050:
051: public void load() {
052: super .load();
053:
054: String urlString = getParameter("url");
055: String name = getParameter("name");
056:
057: ServiceBroker sb = getServiceBroker();
058: LoggingService logging = sb.getService(this ,
059: LoggingService.class, null);
060:
061: DataFeedRegistrationService svc = sb.getService(this ,
062: DataFeedRegistrationService.class, null);
063:
064: Feed feed = new Feed(urlString);
065: svc.registerFeed(feed, name);
066:
067: // special Feed that has the URL for the sites file
068: if (name.equalsIgnoreCase("sites")) {
069: if (logging.isDebugEnabled()) {
070: logging
071: .debug("Populating Sites with url="
072: + urlString);
073: }
074: svc.populateSites(urlString);
075: }
076: sb.releaseService(this , DataFeedRegistrationService.class, svc);
077:
078: }
079:
080: private static class Feed extends PropertiesDataFeed {
081:
082: public Feed(String properties_url) {
083: super (properties_url);
084: }
085:
086: protected InputStream openURL(String urlString) {
087: Logger logging = Logging
088: .getLogger(ConfigFinderDataFeedComponent.class);
089: URI uri = null;
090: try {
091: uri = new URI(urlString);
092: } catch (java.net.URISyntaxException ex) {
093: // log it
094: if (logging.isErrorEnabled()) {
095: logging.error(null, ex);
096: }
097: return null;
098: }
099:
100: String scheme = uri.getScheme();
101: String path = uri.getSchemeSpecificPart();
102:
103: if (logging.isDebugEnabled()) {
104: logging.debug("scheme=" + scheme + " Path=" + path);
105: }
106:
107: if (scheme.equals(CONFIG_PROTOCOL)) {
108: try {
109: if (logging.isDebugEnabled()) {
110: logging.debug("Opening configuration URL="
111: + urlString);
112: }
113: ConfigFinder finder = ConfigFinder.getInstance();
114: return finder.open(path);
115: } catch (java.io.IOException io_ex) {
116: if (logging.isErrorEnabled()) {
117: logging.error("Could not open Config URL="
118: + urlString);
119: }
120: return null;
121: }
122: } else {
123: if (logging.isDebugEnabled()) {
124: logging.debug("Opening external URL=" + urlString);
125: }
126: return super.openURL(urlString);
127: }
128: }
129:
130: }
131:
132: }
|