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 java.io.InputStream;
030: import java.util.List;
031: import java.util.ArrayList;
032:
033: import org.cougaar.core.component.ComponentDescription;
034: import org.cougaar.core.component.ServiceBroker;
035: import org.cougaar.core.component.ServiceProvider;
036: import org.cougaar.util.ConfigFinder;
037:
038: /**
039: * {@link ServiceProvider} for the {@link
040: * FileComponentInitializerServiceComponent} that uses the {@link
041: * INIParser}.
042: */
043: public class FileComponentInitializerServiceProvider implements
044: ServiceProvider {
045:
046: public Object getService(ServiceBroker sb, Object requestor,
047: Class serviceClass) {
048: if (serviceClass != ComponentInitializerService.class) {
049: throw new IllegalArgumentException(getClass()
050: + " does not furnish " + serviceClass);
051: }
052: return new ComponentInitializerServiceImpl();
053: }
054:
055: public void releaseService(ServiceBroker sb, Object requestor,
056: Class serviceClass, Object service) {
057: }
058:
059: private class ComponentInitializerServiceImpl implements
060: ComponentInitializerService {
061: /**
062: * Get the descriptions of components with the named parent having
063: * an insertion point below the given container insertion point.
064: */
065: public ComponentDescription[] getComponentDescriptions(
066: String parentName, String containerInsertionPoint)
067: throws InitializerException {
068: try {
069: // parse the file (we could cache this!)
070: String filename = parentName;
071: if (!parentName.endsWith(".ini")) {
072: filename = parentName + ".ini";
073: }
074: InputStream in = ConfigFinder.getInstance().open(
075: filename);
076: ComponentDescription[] allDescs;
077: try {
078: allDescs = INIParser.parse(in);
079: } finally {
080: in.close();
081: }
082:
083: // extract the components at the specified insertion point
084: List descs = new ArrayList();
085: String cpr = containerInsertionPoint + ".";
086: int cprl = cpr.length();
087: for (int i = 0, n = allDescs.length; i < n; i++) {
088: ComponentDescription cd = allDescs[i];
089: String ip = cd.getInsertionPoint();
090: if (ip.startsWith(cpr)
091: && ip.indexOf(".", cprl + 1) < 0) {
092: descs.add(cd);
093: }
094: }
095:
096: // return as array
097: return (ComponentDescription[]) descs
098: .toArray(new ComponentDescription[descs.size()]);
099: } catch (Exception e) {
100: throw new InitializerException(
101: "getComponentDescriptions(" + parentName + ", "
102: + containerInsertionPoint + ")", e);
103: }
104: }
105:
106: public boolean includesDefaultComponents() {
107: return false;
108: }
109: }
110: }
|