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.qos.ca;
028:
029: import java.util.ArrayList;
030:
031: import org.cougaar.core.component.ServiceBroker;
032:
033: /**
034: * This class represents the base implementation of a {@link
035: * CoordinationArtifactProvider}. It registers the Provider with the
036: * broker and provides basic bookkeepiing for the {@link
037: * CoordinationArtifact}s. Instantiation of CoordinationArtifactss
038: * must be handled in subclasses, via the makeArtifact method.
039: *
040: */
041: abstract public class CoordinationArtifactProviderImpl implements
042: CoordinationArtifactProvider {
043:
044: private ArrayList artifacts;
045: private ServiceBroker sb;
046: private String kind;
047:
048: /**
049: * Instantiable subclasses must provide this method. Its job is
050: * to create new CoordinationArtifacts, given a ConnectionSpec.
051: */
052: abstract public CoordinationArtifact makeArtifact(
053: ConnectionSpec spec);
054:
055: protected CoordinationArtifactProviderImpl(String kind,
056: ServiceBroker sb) {
057: this .artifacts = new ArrayList();
058: this .sb = sb;
059: this .kind = kind;
060: CoordinationArtifactBroker cab = (CoordinationArtifactBroker) sb
061: .getService(this , CoordinationArtifactBroker.class,
062: null);
063: cab.registerCoordinationArtifactProvider(this );
064: sb.releaseService(this , CoordinationArtifactBroker.class, cab);
065: }
066:
067: protected ServiceBroker getServiceBroker() {
068: return sb;
069: }
070:
071: public String getArtifactKind() {
072: return kind;
073: }
074:
075: // By default handle all specs of the right kind
076: public boolean supports(ConnectionSpec spec) {
077: return spec.ca_kind.equals(getArtifactKind());
078: }
079:
080: public void provideFacet(ConnectionSpec spec, RolePlayer player) {
081: CoordinationArtifact artifact = findOrMakeArtifact(spec);
082: if (artifact != null)
083: artifact.provideFacet(spec, player);
084: }
085:
086: private CoordinationArtifact findOrMakeArtifact(ConnectionSpec spec) {
087: synchronized (artifacts) {
088: for (int i = 0; i < artifacts.size(); i++) {
089: CoordinationArtifact ca = (CoordinationArtifact) artifacts
090: .get(i);
091: if (ca.matches(spec))
092: return ca;
093: }
094:
095: // None around yet; make a new one
096: CoordinationArtifact ca = makeArtifact(spec);
097: artifacts.add(ca);
098: return ca;
099: }
100: }
101:
102: }
|