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.community.init;
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.DBInitializerService;
033: import org.cougaar.core.node.NodeControlService;
034: import org.cougaar.core.service.LoggingService;
035: import org.cougaar.util.GenericStateModelAdapter;
036:
037: /**
038: * A component which creates and advertises the appropriate
039: * CommunityInitializerService ServiceProvider.
040: * It can initialize from the CSMART database, using the <code>DBInitializerService</code>,
041: * or from XML files, depending on where components were intialized from.
042: * <p>
043: * @see org.cougaar.community.init.FileCommunityInitializerServiceProvider
044: * @see org.cougaar.community.init.DBCommunityInitializerServiceProvider
045: **/
046: public final class CommunityInitializerServiceComponent extends
047: GenericStateModelAdapter implements Component {
048: private static final String INITIALIZER_PROP = "org.cougaar.core.node.InitializationComponent";
049:
050: private ServiceBroker sb;
051:
052: private DBInitializerService dbInit;
053: private ServiceProvider theSP;
054: private LoggingService log;
055:
056: // ignore "setServiceBroker", we want the node-level service broker
057:
058: public void setNodeControlService(NodeControlService ncs) {
059: if (ncs == null) {
060: // Revocation
061: } else {
062: this .sb = ncs.getRootServiceBroker();
063: }
064: }
065:
066: /*
067: // DBInitializerService isn't available in the node agent
068: public void setDBInitializerService(DBInitializerService dbInit) {
069: this.dbInit = dbInit;
070: }
071: */
072:
073: public void load() {
074: super .load();
075:
076: log = (LoggingService) sb.getService(this ,
077: LoggingService.class, null);
078: if (log == null) {
079: log = LoggingService.NULL;
080: }
081:
082: dbInit = (DBInitializerService) sb.getService(this ,
083: DBInitializerService.class, null);
084:
085: // Do not provide this service if there is already one there.
086: // This allows someone to provide their own component to provide
087: // the community initializer service in their configuration
088: if (sb.hasService(CommunityInitializerService.class)) {
089: // already have CommunityInitializer service!
090: //
091: // leave the existing service in place
092: if (log.isInfoEnabled()) {
093: log
094: .info("Not loading the default community initializer service");
095: }
096: if (log != LoggingService.NULL) {
097: sb.releaseService(this , LoggingService.class, log);
098: log = null;
099: }
100: return;
101: }
102:
103: theSP = chooseSP();
104: if (theSP != null)
105: sb.addService(CommunityInitializerService.class, theSP);
106:
107: if (log != LoggingService.NULL) {
108: sb.releaseService(this , LoggingService.class, log);
109: log = null;
110: }
111: }
112:
113: public void unload() {
114: if (theSP != null) {
115: sb.revokeService(CommunityInitializerService.class, theSP);
116: theSP = null;
117: }
118: super .unload();
119: }
120:
121: private ServiceProvider chooseSP() {
122: try {
123: ServiceProvider sp;
124: String prop = System.getProperty(INITIALIZER_PROP);
125: if (prop != null && prop.indexOf("DB") != -1
126: && dbInit != null) {
127: sp = new DBCommunityInitializerServiceProvider(dbInit);
128: if (log.isInfoEnabled())
129: log.info("Using CSMART DB CommunityInitializer");
130: } else {
131: // Note that these files are XML
132: sp = new FileCommunityInitializerServiceProvider();
133: if (log.isInfoEnabled())
134: log.info("Using File (XML) CommunityInitializer");
135: }
136: return sp;
137: } catch (Exception e) {
138: log.error("Exception creating CommunityInitializerService",
139: e);
140: return null;
141: }
142: }
143: }
|