001: /*
002: * <copyright>
003: *
004: * Copyright 1997-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:
027: package org.cougaar.core.agent.service.id;
028:
029: import java.util.List;
030:
031: import org.cougaar.core.component.Component;
032: import org.cougaar.core.component.ServiceBroker;
033: import org.cougaar.core.component.ServiceProvider;
034: import org.cougaar.core.mts.MessageAddress;
035: import org.cougaar.core.service.AgentIdentificationService;
036: import org.cougaar.util.GenericStateModelAdapter;
037:
038: /**
039: * This component advertises the AgentIdentificationService.
040: */
041: public final class AgentIdentificationServiceComponent extends
042: GenericStateModelAdapter implements Component {
043: private ServiceBroker sb;
044:
045: private MessageAddress addr;
046: private AgentIdentificationService aiS;
047: private AgentIdentificationServiceProvider aiSP;
048:
049: public void setServiceBroker(ServiceBroker sb) {
050: this .sb = sb;
051: }
052:
053: public void setParameter(Object o) {
054: List l = (List) o;
055: String name = (String) l.get(0);
056: this .addr = MessageAddress.getMessageAddress(name);
057: }
058:
059: public void load() {
060: super .load();
061:
062: // create a single per-agent ai service instance
063: this .aiS = new AgentIdentificationServiceImpl(addr);
064:
065: // create and advertise our service
066: this .aiSP = new AgentIdentificationServiceProvider();
067: sb.addService(AgentIdentificationService.class, aiSP);
068: }
069:
070: public void unload() {
071: // revoke our service
072: if (aiSP != null) {
073: sb.revokeService(AgentIdentificationService.class, aiSP);
074: aiSP = null;
075: }
076: super .unload();
077: }
078:
079: private class AgentIdentificationServiceProvider implements
080: ServiceProvider {
081: public Object getService(ServiceBroker sb, Object requestor,
082: Class serviceClass) {
083: if (AgentIdentificationService.class
084: .isAssignableFrom(serviceClass)) {
085: return aiS;
086: } else {
087: return null;
088: }
089: }
090:
091: public void releaseService(ServiceBroker sb, Object requestor,
092: Class serviceClass, Object service) {
093: }
094: }
095:
096: private static class AgentIdentificationServiceImpl implements
097: AgentIdentificationService {
098: private final MessageAddress addr;
099:
100: public AgentIdentificationServiceImpl(MessageAddress addr) {
101: this .addr = addr;
102: if (addr == null) {
103: throw new IllegalArgumentException(
104: "Agent address is null");
105: }
106: }
107:
108: public MessageAddress getMessageAddress() {
109: return addr;
110: }
111:
112: public String getName() {
113: return addr.getAddress();
114: }
115:
116: public String toString() {
117: return getName();
118: }
119: }
120: }
|