001: /*
002: * <copyright>
003: *
004: * Copyright 2001-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: package org.cougaar.tools.csmart.recipe;
027:
028: import org.cougaar.core.agent.Agent;
029: import org.cougaar.tools.csmart.core.db.DBUtils;
030: import org.cougaar.tools.csmart.core.property.ConfigurableComponent;
031: import org.cougaar.tools.csmart.core.property.ConfigurableComponentProperty;
032: import org.cougaar.tools.csmart.core.property.range.StringRange;
033: import org.cougaar.tools.csmart.ui.viewer.CSMART;
034: import org.cougaar.util.DBProperties;
035: import org.cougaar.util.log.Logger;
036:
037: import java.sql.Connection;
038: import java.sql.ResultSet;
039: import java.sql.Statement;
040: import java.util.HashMap;
041: import java.util.HashSet;
042: import java.util.Map;
043: import java.util.Set;
044:
045: /**
046: * Extends ConfigurableComponentProperty in order to override the
047: * getAllowedValues method with values obtained from the set of
048: * available queries. Also overrides the setValue to insure a good
049: * value.
050: **/
051: public class AgentQueryProperty extends ConfigurableComponentProperty {
052:
053: private static Set availableQueries = null;
054:
055: /**
056: * Creates a new <code>AgentQueryProperty</code> instance.
057: *
058: * @param c <code>ConfigurableComponent</code> the Property belongs to
059: * @param name Name of the <code>ConfigurableComponentProperty</code>
060: * @param value Value for the Property
061: */
062: public AgentQueryProperty(ConfigurableComponent c, String name,
063: Object value) {
064: super (c, name, value);
065: setPropertyClass(StringRange.class);
066: }
067:
068: /**
069: * Gets all AllowedValues for this ComponentProperty
070: *
071: * @return null
072: */
073: public Set getAllowedValues() {
074: return null;
075: }
076:
077: /**
078: * Sets the value of this Property
079: *
080: * @param newValue The new property value
081: */
082: public void setValue(Object newValue) {
083: super .setValue(newValue);
084: }
085:
086: private static Set getAvailableQueries() {
087: String query = null;
088: Logger log = CSMART
089: .createLogger("org.cougaar.tools.csmart.recipe.AgentQueryProperty");
090:
091: if (availableQueries == null) {
092: availableQueries = new HashSet();
093: Map substitutions = new HashMap();
094:
095: substitutions
096: .put(":insertion_point", Agent.INSERTION_POINT);
097:
098: try {
099: Connection conn = DBUtils.getConnection();
100: try {
101: Statement stmt = conn.createStatement();
102: query = DBUtils.getQuery("queryAllAgentNames",
103: substitutions);
104: ResultSet rs = stmt.executeQuery(query);
105: while (rs.next()) {
106: availableQueries.add(new StringRange(rs
107: .getString(1)));
108: }
109: stmt.close();
110: } finally {
111: conn.close();
112: }
113: } catch (Exception e) {
114: if (log.isErrorEnabled()) {
115: log.error("Query: " + query, e);
116: }
117: throw new RuntimeException("Error" + e);
118: }
119: }
120: return availableQueries;
121: }
122: }
|