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.node;
028:
029: import org.cougaar.bootstrap.SystemProperties;
030: import org.cougaar.core.component.Component;
031: import org.cougaar.core.component.ServiceBroker;
032: import org.cougaar.core.component.ServiceProvider;
033: import org.cougaar.core.component.ServiceRevokedListener;
034: import org.cougaar.core.service.LoggingService;
035: import org.cougaar.util.GenericStateModelAdapter;
036:
037: /**
038: * This component advertises the {@link DBInitializerService} and
039: * CSMART-database {@link ComponentInitializerService}.
040: *
041: * @property org.cougaar.node.name org.cougaar.experiment.id
042: * CSMART database experiment identifier
043: */
044: public class DBComponentInitializerServiceComponent extends
045: GenericStateModelAdapter implements Component {
046: private static final String EXPTID_PROP = "org.cougaar.experiment.id";
047:
048: private ServiceBroker sb;
049: private ServiceProvider theInitSP;
050: private ServiceProvider theDBSP;
051:
052: public void setServiceBroker(ServiceBroker sb) {
053: // this is the *node* service broker! The NodeControlService
054: // is not available until the node-agent is created...
055: this .sb = sb;
056: }
057:
058: public void load() {
059: super .load();
060:
061: LoggingService log = (LoggingService) sb.getService(this ,
062: LoggingService.class, null);
063: if (log == null) {
064: log = LoggingService.NULL;
065: }
066:
067: // Do not provide this service if there is already one there.
068: // This allows someone to provide their own component to provide
069: // the asset initializer service in their configuration
070: DBInitializerService dbInit;
071: String experimentId = SystemProperties.getProperty(EXPTID_PROP);
072: if (sb.hasService(DBInitializerService.class)) {
073: // already have DBInitializer service!
074: //
075: // leave the existing service in place
076: if (log.isInfoEnabled()) {
077: log.info("Not loading the DBInitializer service"
078: + ", it already exists");
079: }
080: dbInit = (DBInitializerService) sb.getService(this ,
081: DBInitializerService.class, null);
082: } else if (experimentId == null) {
083: if (log.isInfoEnabled()) {
084: log.info("Not loading the DBInitializer service"
085: + ", missing system property -D" + EXPTID_PROP);
086: }
087: dbInit = null;
088: } else {
089: if (log.isInfoEnabled()) {
090: log.info("Creating a new DBInitializer service"
091: + ", using system property -D" + EXPTID_PROP
092: + "=" + experimentId);
093: }
094: try {
095: dbInit = new DBInitializerServiceImpl(experimentId);
096: } catch (Exception e) {
097: throw new RuntimeException(
098: "Unable to load Database Initializer.");
099: }
100: theInitSP = new DBInitializerServiceProvider(dbInit);
101: sb.addService(DBInitializerService.class, theInitSP);
102: }
103:
104: if (sb.hasService(ComponentInitializerService.class)) {
105: // already have a ComponentInitializer?
106: // Leave the existing one in place
107: if (log.isInfoEnabled()) {
108: log
109: .info("Not loading the DB ComponentInitializer service"
110: + ", it already exists");
111: }
112: } else if (dbInit == null) {
113: if (log.isInfoEnabled()) {
114: log
115: .info("Not loading the DB ComponentInitializer service"
116: + ", missing the DBInitializer service");
117: }
118: } else {
119: if (log.isDebugEnabled())
120: log
121: .debug("Creating a new DB ComponentInitializer service"
122: + " based on the DBInitializer service");
123: theDBSP = new DBComponentInitializerServiceProvider(dbInit);
124: sb.addService(ComponentInitializerService.class, theDBSP);
125: }
126:
127: if (log != LoggingService.NULL) {
128: sb.releaseService(this , LoggingService.class, log);
129: log = null;
130: }
131: }
132:
133: public void unload() {
134: if (theInitSP != null) {
135: sb.revokeService(DBInitializerService.class, theInitSP);
136: theInitSP = null;
137: }
138: if (theDBSP != null) {
139: sb
140: .revokeService(ComponentInitializerService.class,
141: theDBSP);
142: theDBSP = null;
143: }
144: super .unload();
145: }
146:
147: private static class DBInitializerServiceProvider implements
148: ServiceProvider {
149:
150: private final DBInitializerService dbInit;
151:
152: public DBInitializerServiceProvider(DBInitializerService dbInit) {
153: this .dbInit = dbInit;
154: }
155:
156: public Object getService(ServiceBroker sb, Object requestor,
157: Class serviceClass) {
158: if (serviceClass != DBInitializerService.class) {
159: throw new IllegalArgumentException(getClass()
160: + " does not furnish " + serviceClass);
161: }
162: return dbInit;
163: }
164:
165: public void releaseService(ServiceBroker sb, Object requestor,
166: Class serviceClass, Object service) {
167: }
168: }
169: }
|