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.agent;
028:
029: import org.cougaar.core.component.Component;
030: import org.cougaar.core.component.NullService;
031: import org.cougaar.core.component.Service;
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.component.ServiceProvider;
034: import org.cougaar.core.component.ServiceRevokedListener;
035: import org.cougaar.core.mts.MessageAddress;
036: import org.cougaar.core.node.NodeControlService;
037: import org.cougaar.core.node.NodeIdentificationService;
038: import org.cougaar.core.service.AgentIdentificationService;
039: import org.cougaar.util.GenericStateModelAdapter;
040:
041: /**
042: * This component blocks the {@link NodeControlService} if the
043: * agent's address does not match the node's address. This is
044: * typically one of the first component loaded in all agents.
045: */
046: public final class NodeControlBlocker extends GenericStateModelAdapter
047: implements Component {
048:
049: private ServiceBroker sb;
050:
051: private ServiceProvider ncsp;
052:
053: public void setServiceBroker(ServiceBroker sb) {
054: this .sb = sb;
055: }
056:
057: public void load() {
058: super .load();
059:
060: MessageAddress localAgent = find_local_agent();
061: MessageAddress localNode = find_local_node();
062: boolean isNode = (localAgent == null || localAgent
063: .equals(localNode));
064:
065: if (!isNode) {
066: // block the NodeControlService!
067: ncsp = new BlockSP();
068: if (!sb.addService(NodeControlService.class, ncsp)) {
069: throw new RuntimeException(
070: "Unable to block NodeControlService");
071: }
072:
073: // verify
074: NodeControlService ncs = (NodeControlService) sb
075: .getService(this , NodeControlService.class, null);
076: if (ncs != null) {
077: throw new RuntimeException(
078: "Could not block NodeControlService");
079: }
080: }
081: }
082:
083: public void unload() {
084: super .unload();
085:
086: if (ncsp != null) {
087: sb.revokeService(NodeControlService.class, ncsp);
088: ncsp = null;
089: }
090: }
091:
092: private MessageAddress find_local_agent() {
093: AgentIdentificationService ais = (AgentIdentificationService) sb
094: .getService(this , AgentIdentificationService.class,
095: null);
096: if (ais == null) {
097: return null;
098: }
099: MessageAddress ret = ais.getMessageAddress();
100: sb.releaseService(this , AgentIdentificationService.class, ais);
101: return ret;
102: }
103:
104: private MessageAddress find_local_node() {
105: NodeIdentificationService nis = (NodeIdentificationService) sb
106: .getService(this , NodeIdentificationService.class, null);
107: if (nis == null) {
108: return null;
109: }
110: MessageAddress ret = nis.getMessageAddress();
111: sb.releaseService(this , NodeIdentificationService.class, nis);
112: return ret;
113: }
114:
115: private static final class BlockSP implements ServiceProvider {
116: // we are not required to implement our service API if the
117: // instance implements NullService
118: private final Service NULL = new NullService() {
119: };
120:
121: public Object getService(ServiceBroker sb, Object requestor,
122: Class serviceClass) {
123: if (NodeControlService.class.isAssignableFrom(serviceClass)) {
124: return NULL; // service blocker!
125: } else {
126: return null;
127: }
128: }
129:
130: public void releaseService(ServiceBroker sb, Object requestor,
131: Class serviceClass, Object service) {
132: }
133: }
134: }
|