001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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.plugin;
028:
029: import org.cougaar.core.blackboard.TimestampEntry;
030: import org.cougaar.core.blackboard.TimestampSubscription;
031: import org.cougaar.core.component.ServiceBroker;
032: import org.cougaar.core.component.ServiceProvider;
033: import org.cougaar.core.service.BlackboardTimestampService;
034: import org.cougaar.core.util.UID;
035: import org.cougaar.core.util.UniqueObject;
036: import org.cougaar.util.UnaryPredicate;
037:
038: /**
039: * This component advertises the {@link BlackboardTimestampService}
040: * for use by other plugins.
041: * <p>
042: * This classes provides access to a shared {@link
043: * TimestampSubscription}'s contents.
044: */
045: public final class TimestampServicePlugin extends ComponentPlugin {
046:
047: private static final UnaryPredicate PRED = new UnaryPredicate() {
048: public boolean execute(Object o) {
049: // for now accept all unique objects
050: //
051: // could filter for just Tasks and PlanElements
052: return (o instanceof UniqueObject);
053: }
054: };
055:
056: private final TimestampSubscription timeSub = new TimestampSubscription(
057: PRED);
058:
059: private BlackboardTimestampServiceProvider btSP;
060:
061: public void load() {
062: super .load();
063: if (btSP == null) {
064: btSP = new BlackboardTimestampServiceProvider();
065: getServiceBroker().addService(
066: BlackboardTimestampService.class, btSP);
067: }
068: }
069:
070: public void unload() {
071: if (btSP != null) {
072: getServiceBroker().revokeService(
073: BlackboardTimestampService.class, btSP);
074: btSP = null;
075: }
076: super .unload();
077: }
078:
079: protected void setupSubscriptions() {
080: Object sub = blackboard.subscribe(timeSub);
081: if (sub != timeSub) {
082: throw new RuntimeException(
083: "Subscribe returned a different subscription?");
084: }
085: }
086:
087: protected void execute() {
088: // never, since we never register to watch the changes
089: }
090:
091: private class BlackboardTimestampServiceProvider implements
092: ServiceProvider {
093:
094: // for now we can share a single service instance
095: private BlackboardTimestampServiceImpl INSTANCE = new BlackboardTimestampServiceImpl();
096:
097: public Object getService(ServiceBroker sb, Object requestor,
098: Class serviceClass) {
099: if (serviceClass == BlackboardTimestampService.class) {
100: return INSTANCE;
101: } else {
102: throw new IllegalArgumentException(this
103: + " does not provide a service for: "
104: + serviceClass);
105: }
106: }
107:
108: public void releaseService(ServiceBroker sb, Object requestor,
109: Class serviceClass, Object service) {
110: // ignore
111: }
112:
113: private class BlackboardTimestampServiceImpl implements
114: BlackboardTimestampService {
115: public long getCreationTime(UID uid) {
116: return timeSub.getCreationTime(uid);
117: }
118:
119: public long getModificationTime(UID uid) {
120: return timeSub.getModificationTime(uid);
121: }
122:
123: public TimestampEntry getTimestampEntry(UID uid) {
124: return timeSub.getTimestampEntry(uid);
125: }
126: }
127: }
128:
129: }
|