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.thread;
028:
029: import org.cougaar.core.component.Component;
030: import org.cougaar.core.component.ServiceBroker;
031: import org.cougaar.core.component.ServiceProvider;
032: import org.cougaar.core.node.NodeControlService;
033: import org.cougaar.core.service.ThreadService;
034: import org.cougaar.util.GenericStateModelAdapter;
035:
036: /**
037: * The ServiceProvider for the simple {@link ThreadService}s. The
038: * default implementation is TrivialThreadServiceProxy.
039: *
040: */
041: public class TrivialThreadServiceProvider extends
042: GenericStateModelAdapter implements ServiceProvider, Component {
043: private ServiceBroker sb;
044: // ServiceBroker used to register thread service.
045: private ServiceBroker mysb;
046: private ThreadService proxy;
047: private ThreadStatusService statusProxy;
048:
049: public TrivialThreadServiceProvider() {
050: }
051:
052: public void setServiceBroker(ServiceBroker sb) {
053: this .sb = sb;
054: }
055:
056: public void load() {
057: super .load();
058: /* if (!sb.hasService(ThreadService.class)) */makeServices(sb);
059: }
060:
061: /**
062: * @see org.cougaar.util.GenericStateModelAdapter#unload()
063: */
064: public void unload() {
065: TreeNode.releaseTimer();
066:
067: TrivialThreadServiceProxy theProxy = (TrivialThreadServiceProxy) proxy;
068: theProxy.unload();
069: proxy = null;
070: statusProxy = null;
071:
072: // Release services.
073: if (mysb != null) {
074: mysb.revokeService(ThreadService.class, this );
075: mysb.revokeService(ThreadStatusService.class, this );
076: mysb = null;
077: }
078:
079: TrivialThreadPool.pool().stopAllThreads();
080:
081: super .unload();
082: }
083:
084: ThreadService makeThreadServiceProxy() {
085: return new TrivialThreadServiceProxy();
086: }
087:
088: ThreadStatusService makeThreadStatusService() {
089: return new ThreadStatusService() {
090: public int iterateOverStatus(ThreadStatusService.Body body) {
091: return TrivialThreadPool.pool()
092: .iterateOverRunningThreads(body);
093: }
094: };
095: }
096:
097: void makeServices(ServiceBroker the_sb) {
098: TrivialThreadPool.makePool();
099: proxy = makeThreadServiceProxy();
100: statusProxy = makeThreadStatusService();
101:
102: NodeControlService ncs = (NodeControlService) the_sb
103: .getService(this , NodeControlService.class, null);
104: if (ncs != null) {
105: mysb = ncs.getRootServiceBroker();
106: the_sb.releaseService(this , NodeControlService.class, ncs);
107: }
108:
109: if (mysb == null) {
110: mysb = the_sb;
111: }
112: mysb.addService(ThreadService.class, this );
113: mysb.addService(ThreadStatusService.class, this );
114:
115: }
116:
117: public Object getService(ServiceBroker sb, Object requestor,
118: Class serviceClass) {
119: if (serviceClass == ThreadService.class) {
120: return proxy;
121: } else if (serviceClass == ThreadStatusService.class) {
122: if (ThreadServiceProvider
123: .validateThreadStatusServiceClient(requestor))
124: return statusProxy;
125: else
126: return null;
127: } else {
128: return null;
129: }
130: }
131:
132: public void releaseService(ServiceBroker sb, Object requestor,
133: Class serviceClass, Object service) {
134: }
135:
136: }
|