001: /*
002: * <copyright>
003: *
004: * Copyright 2000-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: package org.cougaar.core.component;
027:
028: import java.util.Iterator;
029:
030: import org.cougaar.util.ChainingIterator;
031:
032: /** A Simple ServiceBroker which does a simple delegation
033: * of service requests for most purposes, except that it has
034: * an escape hatch where extending classes may, in effect, themselves
035: * offer services directly to the requestors.
036: * <p>
037: * @note Unlike DelegatingServiceBroker, no attempt at hiding the delegate is made.
038: **/
039:
040: public class AddonServiceBroker implements ExtendedServiceBroker {
041: public AddonServiceBroker(ServiceBroker delegate) {
042: if (delegate == null)
043: throw new IllegalArgumentException(
044: "Delegate must be non-null");
045: this .delegate = delegate;
046: }
047:
048: private ServiceBroker delegate;
049:
050: protected final ServiceBroker getDelegate() {
051: return delegate;
052: }
053:
054: public final void addServiceListener(final ServiceListener sl) {
055: delegate.addServiceListener(sl);
056: }
057:
058: public final void removeServiceListener(ServiceListener sl) {
059: delegate.removeServiceListener(sl);
060: }
061:
062: public final boolean addService(Class serviceClass,
063: ServiceProvider serviceProvider) {
064: return addService(serviceClass, serviceProvider, 0, null);
065: }
066:
067: public final boolean addService(Class serviceClass,
068: ServiceProvider serviceProvider, int providerId,
069: ComponentDescription providerDesc) {
070: if (delegate instanceof ExtendedServiceBroker) {
071: ExtendedServiceBroker esb = (ExtendedServiceBroker) delegate;
072: return esb.addService(serviceClass, serviceProvider,
073: providerId, providerDesc);
074: } else {
075: return delegate.addService(serviceClass, serviceProvider);
076: }
077: }
078:
079: public final void revokeService(Class serviceClass,
080: ServiceProvider serviceProvider) {
081: delegate.revokeService(serviceClass, serviceProvider);
082: }
083:
084: public final void revokeService(Class serviceClass,
085: ServiceProvider serviceProvider, int providerId,
086: ComponentDescription providerDesc) {
087: if (delegate instanceof ExtendedServiceBroker) {
088: ExtendedServiceBroker esb = (ExtendedServiceBroker) delegate;
089: esb.revokeService(serviceClass, serviceProvider,
090: providerId, providerDesc);
091: } else {
092: delegate.revokeService(serviceClass, serviceProvider);
093: }
094: }
095:
096: public final boolean hasService(Class serviceClass) {
097: return hasLocalService(serviceClass)
098: || delegate.hasService(serviceClass);
099: }
100:
101: public final Iterator getCurrentServiceClasses() {
102: return new ChainingIterator(new Iterator[] {
103: getCurrentLocalServiceClasses(),
104: delegate.getCurrentServiceClasses() });
105: }
106:
107: public final <T> T getService(Object requestor,
108: final Class<T> serviceClass,
109: final ServiceRevokedListener srl) {
110: ServiceResult sr = getService(0, null, requestor, serviceClass,
111: srl, true);
112: return (sr == null ? null : (T) sr.getService());
113: }
114:
115: public final ServiceResult getService(int requestorId,
116: ComponentDescription requestorDesc, Object requestor,
117: Class serviceClass, ServiceRevokedListener srl,
118: boolean recordInView) {
119: Object s = getLocalService(requestor, serviceClass, srl);
120: if (s != null) {
121: if (s instanceof NullService) {
122: s = null;
123: }
124: } else if (delegate instanceof ExtendedServiceBroker) {
125: ExtendedServiceBroker esb = (ExtendedServiceBroker) delegate;
126: ServiceResult sr = esb.getService(requestorId,
127: requestorDesc, requestor, serviceClass, srl,
128: recordInView);
129: if (sr != null) {
130: if (sr.getService() instanceof NullService) {
131: sr = new ServiceResult(sr.getProviderId(), sr
132: .getProviderComponentDescription(), null);
133: }
134: return sr;
135: }
136: s = null;
137: } else {
138: s = delegate.getService(requestor, serviceClass, srl);
139: }
140: return (s == null ? null : new ServiceResult(0, null, s));
141: }
142:
143: public final void releaseService(Object requestor,
144: Class serviceClass, Object service) {
145: releaseService(0, null, requestor, serviceClass, service, true);
146: }
147:
148: public final void releaseService(int requestorId,
149: ComponentDescription requestorDesc, Object requestor,
150: Class serviceClass, Object service, boolean recordInView) {
151: boolean wasReleased = releaseLocalService(requestor,
152: serviceClass, service);
153: if (!wasReleased) {
154: if (delegate instanceof ExtendedServiceBroker) {
155: ExtendedServiceBroker esb = (ExtendedServiceBroker) delegate;
156: esb.releaseService(requestorId, requestorDesc,
157: requestor, serviceClass, service, recordInView);
158: } else {
159: delegate.releaseService(requestor, serviceClass,
160: service);
161: }
162: }
163: }
164:
165: /** Defined by extending classes to respond to hasService calls from
166: * clients. A service need not be advertised here for getLocalService
167: * to work. The default implementation just returns false.
168: **/
169: protected boolean hasLocalService(Class serviceClass) {
170: return false;
171: }
172:
173: /** Defined by extending classes to advertise a service to the
174: * clients. A service need not be advertised here for getLocalService
175: * to work. The default implementation returns null.
176: **/
177: protected Iterator getCurrentLocalServiceClasses() {
178: return null;
179: }
180:
181: /** Defined by extending classes to provide services to
182: * clients. The default implementation returns null.
183: **/
184: protected Object getLocalService(Object requestor,
185: final Class serviceClass, final ServiceRevokedListener srl) {
186: return null;
187: }
188:
189: /** Defined by extending classes to release previously granted
190: * services. Should return true IFF the service was released.
191: * The default implementation returns false.
192: **/
193: protected boolean releaseLocalService(Object requestor,
194: Class serviceClass, Object service) {
195: return false;
196: }
197: }
|