001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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.servlet;
028:
029: import java.net.URLEncoder;
030: import java.util.Collection;
031:
032: import org.cougaar.core.mts.MessageAddress;
033: import org.cougaar.core.service.BlackboardQueryService;
034: import org.cougaar.core.service.LoggingService;
035: import org.cougaar.util.UnaryPredicate;
036:
037: /**
038: * This is a generic API, provided by "SimpleServletComponent",
039: * that allows a Servlet to access COUGAAR Services.
040: * <p>
041: * The SimpleServletComponent class passes an instance of
042: * SimpleServletSupport to a Servlet's constructor. This API
043: * abstracts away the Component and Service details from the
044: * Servlet.
045: */
046: public class SimpleServletSupportImpl implements SimpleServletSupport {
047: protected String path;
048: protected MessageAddress agentId;
049: protected BlackboardQueryService blackboardQuery;
050: protected LoggingService log;
051:
052: protected String encAgentName;
053:
054: public SimpleServletSupportImpl(String path,
055: MessageAddress agentId,
056: BlackboardQueryService blackboardQuery) {
057: this (path, agentId, blackboardQuery, null);
058: }
059:
060: public SimpleServletSupportImpl(String path,
061: MessageAddress agentId,
062: BlackboardQueryService blackboardQuery, LoggingService log) {
063: this .path = path;
064: this .agentId = agentId;
065: this .blackboardQuery = blackboardQuery;
066: this .log = ((log != null) ? log : LoggingService.NULL);
067: // cache:
068: encAgentName = encodeAgentName(agentId.getAddress());
069: }
070:
071: public String getPath() {
072: return path;
073: }
074:
075: public Collection queryBlackboard(UnaryPredicate pred) {
076: return blackboardQuery.query(pred);
077: }
078:
079: public String getEncodedAgentName() {
080: return encAgentName;
081: }
082:
083: public MessageAddress getAgentIdentifier() {
084: return agentId;
085: }
086:
087: public LoggingService getLog() {
088: return log;
089: }
090:
091: // maybe add a "getAllAgentIdentifiers()"
092:
093: public String encodeAgentName(String name) {
094: try {
095: return URLEncoder.encode(name, "UTF-8");
096: } catch (java.io.UnsupportedEncodingException e) {
097: // should never happen
098: throw new RuntimeException("Unable to encode to UTF-8?");
099: }
100: }
101:
102: // etc to match "SimpleServletSupport"
103: }
|