001: /*
002: * <copyright>
003: *
004: * Copyright 2001-2007 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.util.Collection;
030: import java.util.Enumeration;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.Map;
034: import java.util.Properties;
035: import org.cougaar.bootstrap.SystemProperties;
036: import org.cougaar.core.component.Component;
037: import org.cougaar.core.component.ServiceBroker;
038: import org.cougaar.util.GenericStateModelAdapter;
039:
040: /**
041: * This component can be used to set {@link SystemProperties} values
042: * and initialize the properties map.
043: * <p>
044: * For an example, see {@link NodeApplet}'s "createProperties()" method.
045: * <p>
046: * Each parameter must be one of:<ul>
047: * <li>A Properties object</li>
048: * <li>A Map of String names to String values</li>
049: * <li>A Collection of String name=value pairs</li>
050: * <li>A single String name=value pair</li>
051: * </ul>
052: * <p>
053: * Four special properties are filtered out and processed before all
054: * others, and in the following order:<ol>
055: * <li>"override_props": If true, call
056: * {@link SystemProperties#overrideProperties} with an
057: * empty Properties table.</li>
058: * <li>"put_system_props": If true, copy all System properties
059: * into the SystemProperties table.</li>
060: * <li>"put_applet_props": If true, copy all System properties
061: * that Applets are allowed to access into the SystemProperties
062: * table.</li>
063: * <li>"finalize_props": If true, call
064: * {@link SystemProperties.finalizeProperties}.</li>
065: * </ol>
066: * <p>
067: * Property values that specify "$" variables are expanded in place, using the
068: * SystemProperties "resolveEnv" and "expandProperties" methods.
069: */
070: public class SetPropertiesComponent extends GenericStateModelAdapter
071: implements Component {
072:
073: // applets are following to get the following properties
074: private static final String[] APPLET_PROPS = new String[] {
075: "file.separator", "java.class.version", "java.vendor",
076: "java.vendor.url", "java.version", "line.separator",
077: "os.arch", "os.name", "path.separator", };
078:
079: private Object param;
080:
081: public void setServiceBroker(ServiceBroker sb) {
082: // ignore
083: }
084:
085: public void setParameter(Object o) {
086: param = o;
087: }
088:
089: public void load() {
090: super .load();
091:
092: // parse parameters
093: Map m = parse(param);
094: param = null;
095:
096: // check for special control options
097: boolean override_props = "true".equals(m
098: .remove("override_props"));
099: boolean put_system_props = "true".equals(m
100: .remove("put_system_props"));
101: boolean put_applet_props = "true".equals(m
102: .remove("put_applet_props"));
103: boolean finalize_props = "true".equals(m
104: .remove("finalize_props"));
105:
106: if (override_props) {
107: SystemProperties.overrideProperties(new Properties());
108: }
109: if (put_system_props) {
110: Properties props = System.getProperties();
111: for (Enumeration en = props.propertyNames(); en
112: .hasMoreElements();) {
113: String key = (String) en.nextElement();
114: String value = props.getProperty(key);
115: SystemProperties.setProperty(key, value);
116: }
117: }
118: if (put_applet_props) {
119: for (int i = 0; i < APPLET_PROPS.length; i++) {
120: String key = APPLET_PROPS[i];
121: String value = System.getProperty(key);
122: if (value != null) {
123: SystemProperties.setProperty(key, value);
124: }
125: }
126: }
127: if (finalize_props) {
128: SystemProperties.finalizeProperties();
129: }
130:
131: // remember if there are any pending $s
132: boolean any_dollars = false;
133:
134: for (Iterator iter = m.entrySet().iterator(); iter.hasNext();) {
135: Map.Entry me = (Map.Entry) iter.next();
136:
137: String key = (String) me.getKey();
138: String value = (String) me.getValue();
139:
140: if (key.startsWith("-D")) {
141: key = key.substring(2);
142: }
143: if (value == null) {
144: value = "";
145: }
146:
147: // expand "-Dx=$COUGAAR_INSTALL_PATH"
148: boolean windows = false; // use unix style regardless of OS
149: value = SystemProperties.resolveEnv(value, windows);
150: if (value.indexOf('$') >= 0) {
151: any_dollars = true;
152: }
153:
154: if (key.startsWith("unset(") && key.endsWith(")")) {
155: key = key.substring(6, key.length() - 1);
156: value = null;
157: }
158:
159: // set
160: SystemProperties.setProperty(key, value);
161: }
162:
163: if (any_dollars) {
164: // resolve "-Dx=\\${org.cougaar.install.path}/blah" property references.
165: SystemProperties.expandProperties();
166: }
167: }
168:
169: private Map parse(Object o) {
170: Map ret = new HashMap();
171: parse(param, ret);
172: return ret;
173: }
174:
175: // recursive!
176: private void parse(Object o, Map toMap) {
177: if (o instanceof String) {
178: // base case
179: String s = (String) o;
180: int sep = s.indexOf('=');
181: if (sep < 0) {
182: toMap.put(s, "");
183: } else {
184: String key = s.substring(0, sep).trim();
185: String value = s.substring(sep + 1).trim();
186: toMap.put(key, value);
187: }
188: } else if (o instanceof Collection) {
189: // typical case, name=value pairs
190: Collection c = (Collection) o;
191: for (Iterator iter = c.iterator(); iter.hasNext();) {
192: Object oi = iter.next();
193: // recurse!
194: parse(oi, toMap);
195: }
196: } else if (o instanceof Map) {
197: toMap.putAll((Map) o);
198: } else if (o instanceof Properties) {
199: Properties props = (Properties) o;
200: for (Enumeration en = props.propertyNames(); en
201: .hasMoreElements();) {
202: String key = (String) en.nextElement();
203: String value = props.getProperty(key);
204: toMap.put(key, (value == null ? "" : value));
205: }
206: } else {
207: throw new RuntimeException("Invalid parameter type: "
208: + (o == null ? "null" : o.getClass().getName()));
209: }
210: }
211:
212: }
|