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.planning.ldm;
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.Node;
034: import org.cougaar.core.node.NodeControlService;
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.planning.service.AssetInitializerService;
037: import org.cougaar.util.ConfigFinder;
038: import org.cougaar.util.GenericStateModelAdapter;
039: import org.cougaar.util.Parameters;
040:
041: import java.io.File;
042: import java.io.IOException;
043: import java.io.InputStream;
044:
045: /**
046: * A component which creates and advertises the appropriate
047: * AssetInitializerService ServiceProvider.
048: * <p/>
049: * The rule is that we use the CSMART DB if components were intialized from there.
050: * Otherwise, if the components coming from XML,
051: * we use the non-CSMART DB. Otherwise we try to initialize from INI-style files.
052: * <p/>
053: *
054: * @see FileAssetInitializerServiceProvider
055: * @see DBAssetInitializerServiceProvider
056: * @see NonCSMARTDBInitializerServiceImpl
057: */
058: public final class AssetInitializerServiceComponent extends
059: GenericStateModelAdapter implements Component {
060:
061: // Used below to confirm good cougaar.rc file for use with XML files
062: private static final String DATABASE = "org.cougaar.refconfig.database";
063: private static final String USER = "org.cougaar.refconfig.user";
064: private static final String PASSWORD = "org.cougaar.refconfig.password";
065:
066: private ServiceBroker sb;
067:
068: private DBInitializerService dbInit = null;
069: private ServiceProvider theSP;
070: private LoggingService log;
071:
072: // ignore "setServiceBroker", we want the node-level service broker
073:
074: public void setNodeControlService(NodeControlService ncs) {
075: if (ncs == null) {
076: // Revocation
077: } else {
078: this .sb = ncs.getRootServiceBroker();
079: }
080: }
081:
082: public void load() {
083: super .load();
084:
085: log = (LoggingService) sb.getService(this ,
086: LoggingService.class, null);
087: if (log == null) {
088: log = LoggingService.NULL;
089: }
090:
091: // Do not provide this service if there is already one there.
092: // This allows someone to provide their own component to provide
093: // the asset initializer service in their configuration
094: if (sb.hasService(AssetInitializerService.class)) {
095: // already have AssetInitializer service!
096: //
097: // leave the existing service in place
098: if (log.isInfoEnabled()) {
099: log
100: .info("Not loading the default asset initializer service");
101: }
102: if (log != LoggingService.NULL) {
103: sb.releaseService(this , LoggingService.class, log);
104: log = null;
105: }
106: return;
107: }
108:
109: theSP = chooseSP();
110: if (theSP != null)
111: sb.addService(AssetInitializerService.class, theSP);
112:
113: if (log != LoggingService.NULL) {
114: sb.releaseService(this , LoggingService.class, log);
115: log = null;
116: }
117: }
118:
119: public void unload() {
120: if (theSP != null) {
121: sb.revokeService(AssetInitializerService.class, theSP);
122: theSP = null;
123: }
124:
125: super .unload();
126: }
127:
128: // If the DB property was supplied and we have a DBInializerService,
129: // we initializer Organization/Entity assets from the configuration.database
130: // Otherwise if the XML property was supplied, we check for a valid
131: // cougaar.rc file for the refconfig.database. If we have one,
132: // we try to initialize assets from that database.
133: // If we have no such cougaar.rc file, or if some other
134: // parameter was supplied, we initialize assets strictly
135: // from files.
136: private ServiceProvider chooseSP() {
137: try {
138: ServiceProvider sp = null;
139: String prop = System.getProperty(Node.INITIALIZER_PROP);
140:
141: // If user specified to load from the database
142: if (prop != null && prop.indexOf("DB") != -1) {
143: // Init from CSMART DB
144: dbInit = (DBInitializerService) sb.getService(this ,
145: DBInitializerService.class, null);
146: if (dbInit != null) {
147: if (log.isInfoEnabled())
148: log.info("Will init OrgAssets from CSMART DB");
149: }
150: } else if (prop != null && prop.indexOf("XML") != -1) {
151: // Else if user specified to load from XML
152: // First check to see if user set up a DB for use with XML files
153: if (rcFileExists() && isValidRCFile()) {
154: // Initing config from XML. Assets will come from non-CSMART DB
155: // Create a new DBInitializerService
156: dbInit = new NonCSMARTDBInitializerServiceImpl();
157: if (dbInit != null) {
158: if (log.isInfoEnabled()) {
159: log
160: .info("Will init OrgAssets from NON CSMART DB!");
161: }
162: }
163: }
164: }
165:
166: // If we got a good DB set up, then use that.
167: if (dbInit != null) {
168: sp = new DBAssetInitializerServiceProvider(dbInit);
169: }
170:
171: // Handle this separately in case the above fails somehow
172: if (sp == null) {
173: // If user specified INI files, or set up no database, ie
174: // if didn't get a good DBInitializerService, then
175: // use files. This may be INI or XML.
176: sp = new FileAssetInitializerServiceProvider();
177: if (log.isInfoEnabled())
178: log
179: .info("Not using a database, initializing solely from Files.");
180: }
181:
182: return sp;
183: } catch (Exception e) {
184: log.error(
185: "Exception while creating AssetInitializerService",
186: e);
187: return null;
188: }
189: }
190:
191: // Helper function to check for a cougaar.rc file without parsing it
192: private boolean rcFileExists() {
193: boolean found = false;
194: try {
195: File f = new File(System.getProperty("user.home")
196: + File.separator + ".cougaarrc");
197: if (!f.exists()) {
198: InputStream in = ConfigFinder.getInstance().open(
199: "cougaar.rc");
200: if (in != null) {
201: found = true;
202: }
203: } else {
204: found = true;
205: }
206: } catch (IOException e) {
207: //We really do want to ignore this!
208: }
209:
210: if (!found) {
211: // util.Parameters will tell people this already
212: if (log.isInfoEnabled())
213: log.info("No Cougaar rc file found.");
214: }
215:
216: return found;
217: }
218:
219: // Use Parameters utilities parse of the cougaar.rc file
220: // to check for key parameters needed when running from XML files
221: private boolean isValidRCFile() {
222: boolean valid = false;
223:
224: valid = (Parameters.findParameter(DATABASE) == null) ? false
225: : true;
226: valid &= (Parameters.findParameter(USER) == null) ? false
227: : true;
228: valid &= (Parameters.findParameter(PASSWORD) == null) ? false
229: : true;
230: return valid;
231: }
232:
233: }
|