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.sql.Connection;
030: import java.sql.ResultSet;
031: import java.sql.Statement;
032: import java.util.ArrayList;
033: import java.util.List;
034: import java.util.Map;
035: import java.util.ArrayList;
036:
037: import org.cougaar.core.component.ComponentDescription;
038: import org.cougaar.core.component.ServiceBroker;
039: import org.cougaar.core.component.ServiceProvider;
040: import org.cougaar.util.log.Logger;
041: import org.cougaar.util.log.Logging;
042:
043: /**
044: * {@link ServiceProvider} for a {@link ComponentInitializerService}
045: * backed by the {@link DBInitializerService}.
046: */
047: class DBComponentInitializerServiceProvider implements ServiceProvider {
048:
049: private final DBInitializerService dbInit;
050: private final Logger logger;
051:
052: public DBComponentInitializerServiceProvider(
053: DBInitializerService dbInit) {
054: this .dbInit = dbInit;
055: this .logger = Logging.getLogger(getClass());
056: }
057:
058: public Object getService(ServiceBroker sb, Object requestor,
059: Class serviceClass) {
060: if (serviceClass != ComponentInitializerService.class) {
061: throw new IllegalArgumentException(getClass()
062: + " does not furnish " + serviceClass);
063: }
064: return new ComponentInitializerServiceImpl();
065: }
066:
067: public void releaseService(ServiceBroker sb, Object requestor,
068: Class serviceClass, Object service) {
069: }
070:
071: private class ComponentInitializerServiceImpl implements
072: ComponentInitializerService {
073: // Remember, this returns only items strictly _below_ the given
074: // insertion point, listed as children of the given component.
075: public ComponentDescription[] getComponentDescriptions(
076: String parentName, String containerInsertionPoint)
077: throws InitializerException {
078: if (logger.isDebugEnabled()) {
079: logger.debug("In getComponentDescriptions");
080: }
081: if (parentName == null)
082: throw new IllegalArgumentException(
083: "parentName cannot be null");
084: // append a dot to containerInsertionPoint if not already there
085: if (!containerInsertionPoint.endsWith("."))
086: containerInsertionPoint += ".";
087: Map substitutions = dbInit.createSubstitutions();
088: substitutions.put(":parent_name:", parentName);
089: substitutions.put(":container_insertion_point:",
090: containerInsertionPoint);
091: if (logger.isInfoEnabled()) {
092: logger.info("Looking for direct sub-components of "
093: + parentName + " just below insertion point "
094: + containerInsertionPoint);
095: }
096:
097: try {
098: Connection conn = dbInit.getConnection();
099: try {
100: Statement stmt = conn.createStatement();
101: String query = dbInit.getQuery("queryComponents",
102: substitutions);
103:
104: /*
105: if (logger.isDebugEnabled()) {
106: logger.debug("getComponentDescriptions doing query " + query);
107: }
108: */
109:
110: ResultSet rs = dbInit.executeQuery(stmt, query);
111: List componentDescriptions = new ArrayList();
112: while (rs.next()) {
113: String componentName = dbInit.getNonNullString(
114: rs, 1, query);
115: String componentClass = dbInit
116: .getNonNullString(rs, 2, query);
117: String componentId = dbInit.getNonNullString(
118: rs, 3, query);
119: String insertionPoint = dbInit
120: .getNonNullString(rs, 4, query);
121: String priority = dbInit.getNonNullString(rs,
122: 5, query);
123: Statement stmt2 = conn.createStatement();
124: substitutions
125: .put(":component_id:", componentId);
126: String query2 = dbInit.getQuery(
127: "queryComponentParams", substitutions);
128:
129: ResultSet rs2 = dbInit.executeQuery(stmt2,
130: query2);
131: ArrayList vParams = null;
132: while (rs2.next()) {
133: String param = dbInit.getNonNullString(rs2,
134: 1, query2);
135: if (!param.startsWith("PROP$")) { // CSMART private arg
136: if (vParams == null)
137: vParams = new ArrayList(1); // lazy create
138: vParams.add(param);
139: }
140: }
141:
142: ComponentDescription desc = new ComponentDescription(
143: componentName, insertionPoint,
144: componentClass, null, // codebase
145: vParams, null, // certificate
146: null, // lease
147: null, // policy
148: ComponentDescription
149: .parsePriority(priority));
150: componentDescriptions.add(desc);
151: rs2.close();
152: stmt2.close();
153: } // end of loop over result set
154:
155: int len = componentDescriptions.size();
156: if (logger.isDebugEnabled()) {
157: logger.debug("... returning " + len
158: + " CDescriptions");
159: }
160: ComponentDescription[] result = new ComponentDescription[len];
161: result = (ComponentDescription[]) componentDescriptions
162: .toArray(result);
163:
164: // Print out each component description
165: if (logger.isDetailEnabled()) {
166: for (int i = 0; i < result.length; i++) {
167: StringBuffer buf = new StringBuffer();
168: buf.append(result[i].getInsertionPoint());
169: if (result[i].getPriority() != ComponentDescription.PRIORITY_STANDARD) {
170: buf.append("(");
171: buf.append(ComponentDescription
172: .priorityToString(result[i]
173: .getPriority()));
174: buf.append(") ");
175:
176: }
177: buf.append("=");
178: buf.append(result[i].getClassname());
179: ArrayList params = (ArrayList) result[i]
180: .getParameter();
181: int n = params.size();
182: if (n > 0) {
183: for (int j = 0; j < n; j++) {
184: if (j == 0)
185: buf.append("(");
186: else
187: buf.append(", ");
188: buf.append(params.get(j));
189: }
190: buf.append(")");
191: }
192: logger.detail(buf.toString());
193: }
194: } // end of if(detail)
195:
196: return result;
197: } finally {
198: conn.close();
199: }
200: } catch (Exception e) {
201: throw new InitializerException(
202: "getComponentDescriptions(" + parentName + ", "
203: + containerInsertionPoint + ")", e);
204: }
205: }
206:
207: public boolean includesDefaultComponents() {
208: return false;
209: }
210: }
211: }
|