01: /*
02: * <copyright>
03: *
04: * Copyright 1997-2007 BBNT Solutions, LLC
05: * under sponsorship of the Defense Advanced Research Projects
06: * Agency (DARPA).
07: *
08: * You can redistribute this software and/or modify it under the
09: * terms of the Cougaar Open Source License as published on the
10: * Cougaar Open Source Website (www.cougaar.org).
11: *
12: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
13: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
14: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
15: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
16: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
17: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
18: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23: *
24: * </copyright>
25: */
26:
27: package org.cougaar.core.node;
28:
29: import java.util.Collection;
30: import java.util.Iterator;
31: import org.cougaar.bootstrap.SystemProperties;
32: import org.cougaar.core.component.ComponentSupport;
33:
34: /**
35: * This component simply copies its parameters into Java's System Properties.
36: * <p>
37: * Note that only "-D" system properties are supported. There's no way to
38: * set "-X" JVM properties at runtime. Also, some "-Ds" may have already
39: * been used at this point, such as the Cougaar bootstrapper's
40: * "-Dorg.cougaar.install.path".
41: * <p>
42: * Example usage:<pre>
43: * <component class='org.cougaar.core.node.SetProperties'>
44: * <argument>-Dx=y</argument>
45: * <argument>-Dfoo=bar</argument>
46: * </component>
47: * </pre>
48: */
49: public class SetProperties extends ComponentSupport {
50:
51: public void setParameter(Object o) {
52: // remember if there are any pending $s
53: boolean any_dollars = false;
54:
55: for (Iterator iter = ((Collection) o).iterator(); iter
56: .hasNext();) {
57: String s = (String) iter.next();
58:
59: // expand "-Dx=$COUGAAR_INSTALL_PATH"
60: boolean windows = false; // use unix style regardless of OS
61: s = SystemProperties.resolveEnv(s, windows);
62: if (s.indexOf('$') >= 0) {
63: any_dollars = true;
64: }
65:
66: // parse
67: String key;
68: String value;
69: int sep = s.indexOf('=');
70: if (sep < 0) {
71: key = s;
72: value = "";
73: } else {
74: key = s.substring(0, sep).trim();
75: value = s.substring(sep + 1).trim();
76: }
77: if (key.startsWith("unset(") && key.endsWith(")")) {
78: key = key.substring(6, key.length() - 1);
79: value = null;
80: }
81: if (!key.startsWith("-D")) {
82: throw new IllegalArgumentException(
83: "Expecting a \"-D\", not " + s);
84: }
85:
86: // set
87: SystemProperties.setProperty(key, value);
88: }
89:
90: if (any_dollars) {
91: // resolve "-Dx=\\${org.cougaar.install.path}/blah" property references.
92: SystemProperties.expandProperties();
93: }
94: }
95: }
|