001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.wp.resolver;
028:
029: import org.cougaar.core.component.Component;
030: import org.cougaar.core.component.ServiceBroker;
031: import org.cougaar.core.component.ServiceProvider;
032: import org.cougaar.core.component.ServiceRevokedListener;
033: import org.cougaar.core.mts.MessageAddress;
034: import org.cougaar.core.node.NodeIdentificationService;
035: import org.cougaar.core.service.AgentIdentificationService;
036: import org.cougaar.core.service.LoggingService;
037: import org.cougaar.core.service.wp.WhitePagesService;
038: import org.cougaar.util.GenericStateModelAdapter;
039:
040: /**
041: * This component an optional proxy to the {@link WhitePagesService}
042: * resolver that obtains the local agent's name and tags all requests
043: * with that name.
044: * <p>
045: * This should be loaded into all agents. All WhitePagesService
046: * requests from within the agent will passed through a proxy
047: * service. From the node's point of view there will only be one
048: * client per agent (ie. this proxy).
049: * <p>
050: * The proxy is disabled if this component is loaded into the
051: * node agent.
052: */
053: public class ResolverProxy extends GenericStateModelAdapter implements
054: Component {
055: private ServiceBroker sb;
056:
057: private LoggingService logger;
058: private MessageAddress agentId;
059: private MessageAddress nodeId;
060: private WhitePagesService proxyWP;
061:
062: private WhitePagesSP proxySP;
063:
064: private final ResolverClient myClient = new ResolverClient() {
065: public String getAgent() {
066: return (agentId == null ? null : agentId.getAddress());
067: }
068: };
069:
070: public void setServiceBroker(ServiceBroker sb) {
071: this .sb = sb;
072: }
073:
074: public void setLoggingService(LoggingService logger) {
075: this .logger = logger;
076: }
077:
078: public void load() {
079: super .load();
080:
081: if (logger.isDebugEnabled()) {
082: logger.debug("Loading resolver proxy");
083: }
084:
085: // which agent are we in?
086: AgentIdentificationService ais = (AgentIdentificationService) sb
087: .getService(this , AgentIdentificationService.class,
088: null);
089: agentId = ais.getMessageAddress();
090: sb.releaseService(this , AgentIdentificationService.class, ais);
091:
092: // which node are we in?
093: NodeIdentificationService nis = (NodeIdentificationService) sb
094: .getService(this , NodeIdentificationService.class, null);
095: nodeId = nis.getMessageAddress();
096: sb.releaseService(this , NodeIdentificationService.class, nis);
097:
098: if (agentId == null || agentId.equals(nodeId)) {
099: // we're in the node agent
100: return;
101: }
102:
103: // get the node's wp service
104: proxyWP = (WhitePagesService) sb.getService(myClient,
105: WhitePagesService.class, null);
106: if (proxyWP == null) {
107: throw new RuntimeException(
108: "Unable to obtain the WhitePagesService,"
109: + " proxy failed for agent " + agentId
110: + " on node " + nodeId);
111: }
112:
113: // advertize our service proxy
114: proxySP = new WhitePagesSP();
115: sb.addService(WhitePagesService.class, proxySP);
116:
117: if (logger.isInfoEnabled()) {
118: logger.info("Loaded white pages resolver proxy for agent "
119: + agentId + " on node " + nodeId);
120: }
121: }
122:
123: public void unload() {
124: super .unload();
125:
126: // revoke white pages service
127: if (proxySP != null) {
128: sb.revokeService(WhitePagesService.class, proxySP);
129: proxySP = null;
130: }
131:
132: if (proxyWP != null) {
133: sb.releaseService(myClient, WhitePagesService.class,
134: proxyWP);
135: proxyWP = null;
136: }
137:
138: if (logger != null) {
139: sb.releaseService(this , LoggingService.class, logger);
140: logger = null;
141: }
142: }
143:
144: private class WhitePagesSP implements ServiceProvider {
145: public Object getService(ServiceBroker sb, Object requestor,
146: Class serviceClass) {
147: if (WhitePagesService.class.isAssignableFrom(serviceClass)) {
148: // return our proxyWP, where myClient identifies our agent
149: if (logger.isDetailEnabled()) {
150: logger.detail("giving " + agentId + " proxy WP to "
151: + requestor);
152: }
153: return proxyWP;
154: } else {
155: return null;
156: }
157: }
158:
159: public void releaseService(ServiceBroker sb, Object requestor,
160: Class serviceClass, Object service) {
161: }
162: }
163: }
|