001: /*
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found in file GTPL, or at
004: * http://www.globus.org/toolkit/download/license.html. This notice must
005: * appear in redistributions of this file, with or without modification.
006: *
007: * Redistributions of this Software, with or without modification, must
008: * reproduce the GTPL in: (1) the Software, or (2) the Documentation or
009: * some other similar material which is provided with the Software (if
010: * any).
011: *
012: * Copyright 1999-2004 University of Chicago and The University of
013: * Southern California. All rights reserved.
014: */
015: package org.griphyn.vdl.invocation;
016:
017: import java.util.*;
018: import java.io.Writer;
019: import java.io.IOException;
020:
021: /**
022: * This class maintains the application that was run, and the
023: * arguments to the commandline that were actually passed on to
024: * the application.
025: *
026: * @author Jens-S. Vöckler
027: * @author Yong Zhao
028: * @version $Revision: 50 $
029: * @see Job
030: */
031: public class Environment extends Invocation {
032: /**
033: * Mappings of keys to values
034: */
035: private Map m_environment;
036:
037: /**
038: * Default c'tor: Construct a hollow shell and allow further
039: * information to be added later.
040: */
041: public Environment() {
042: m_environment = new HashMap();
043: }
044:
045: /**
046: * Adds an environment entry, effectively a key value pair, to the
047: * current environment settings.
048: *
049: * @param entry is the environment entry to add
050: * @return the old entry including <code>null</code>.
051: * @see #addEntry( String, String )
052: */
053: public String addEntry(EnvEntry entry) {
054: String key = entry.getKey();
055: if (key != null) {
056: String value = entry.getValue();
057: if (value == null)
058: value = new String();
059: return (String) m_environment.put(entry.getKey(), entry
060: .getValue());
061: } else {
062: return null; // evil!
063: }
064: }
065:
066: /**
067: * Adds an environment entry, effectively a key value pair, to the
068: * current environment settings.
069: *
070: * @param key is the identifier for the environment setting.
071: * @param value is the value associated with the key.
072: * @return the old entry including <code>null</code>.
073: * @see #addEntry( EnvEntry )
074: */
075: public String addEntry(String key, String value) {
076: if (key != null) {
077: if (value == null)
078: value = new String();
079: return (String) m_environment.put(key, value);
080: } else {
081: return null;
082: }
083: }
084:
085: /**
086: * Retrieves the value for a given key
087: *
088: * @param key is the identifier in the map to retrieve the key for
089: * @return the value for the given, which may include <code>null</code>.
090: */
091: public String get(String key) {
092: return (String) m_environment.get(key);
093: }
094:
095: /**
096: * Creates a sorted iterator
097: *
098: * @return an iterator over sorted keys
099: */
100: public Iterator iterator() {
101: Set result = new TreeSet(m_environment.keySet());
102: return result.iterator();
103: }
104:
105: /**
106: * Converts the active state into something meant for human consumption.
107: * The method will be called when recursively traversing the instance
108: * tree.
109: *
110: * @param stream is a stream opened and ready for writing. This can also
111: * be a string stream for efficient output.
112: */
113: public void toString(Writer stream) throws IOException {
114: throw new IOException(
115: "method not implemented, please contact vds-support@griphyn.org");
116: }
117:
118: /**
119: * Dump the state of the current element as XML output. This function
120: * traverses all sibling classes as necessary, and converts the data
121: * into pretty-printed XML output. The stream interface should be able
122: * to handle large output efficiently.
123: *
124: * @param stream is a stream opened and ready for writing. This can also
125: * be a string stream for efficient output.
126: * @param indent is a <code>String</code> of spaces used for pretty
127: * printing. The initial amount of spaces should be an empty string.
128: * The parameter is used internally for the recursive traversal.
129: * If a <code>null</code> value is specified, no indentation nor
130: * linefeeds will be generated.
131: * @param namespace is the XML schema namespace prefix. If neither
132: * empty nor null, each element will be prefixed with this prefix,
133: * and the root element will map the XML namespace.
134: * @exception IOException if something fishy happens to the stream.
135: */
136: public void toXML(Writer stream, String indent, String namespace)
137: throws IOException {
138: String newline = System.getProperty("line.separator", "\r\n");
139: String tag = (namespace != null && namespace.length() > 0) ? namespace
140: + ":environment"
141: : "environment";
142:
143: // open tag
144: if (indent != null && indent.length() > 0)
145: stream.write(indent);
146: stream.write('<');
147: stream.write(tag);
148: if (m_environment.size() == 0) {
149: // no content
150: stream.write("/>");
151: if (indent != null)
152: stream.write(newline);
153: } else {
154: // yes, content
155: String newindent = (indent == null) ? null : indent + " ";
156: String envtag = (namespace != null && namespace.length() > 0) ? namespace
157: + ":env"
158: : "env";
159: stream.write('>');
160: if (indent != null)
161: stream.write(newline);
162:
163: for (Iterator i = this .iterator(); i.hasNext();) {
164: String key = (String) i.next();
165: String value = this .get(key);
166:
167: if (newindent != null && newindent.length() > 0)
168: stream.write(newindent);
169: stream.write('<');
170: stream.write(envtag);
171: writeAttribute(stream, " key=\"", key);
172: stream.write('>');
173:
174: if (value != null)
175: stream.write(quote(value, false));
176:
177: stream.write("</");
178: stream.write(envtag);
179: stream.write('>');
180: if (indent != null)
181: stream.write(newline);
182: }
183:
184: stream.write("</");
185: stream.write(tag);
186: stream.write('>');
187: }
188: if (indent != null)
189: stream.write(newline);
190: }
191: }
|