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:
016: package org.griphyn.cPlanner.namespace;
017:
018: import java.util.Iterator;
019: import java.util.Map;
020: import java.util.StringTokenizer;
021: import java.util.TreeMap;
022:
023: import org.griphyn.cPlanner.classes.Profile;
024: import org.griphyn.cPlanner.common.PegasusProperties;
025:
026: /**
027: * The environment namespace, that puts in the environment variables for the
028: * transformation that is being run, through Condor. At present on the occurence
029: * of a clash between the values of an environment variable the values are
030: * overwritten with the order of preference in decreasing order being users
031: * local properties, transformation catalog, pool file and the dax (vdl).
032: * Later on operations like append , prepend would also be supported.
033: *
034: * @author Karan Vahi
035: * @author Gaurang Mehta
036: * @version $Revision: 50 $
037: */
038:
039: public class ENV extends Namespace {
040:
041: /**
042: * The name of the namespace that this class implements.
043: */
044: public static final String NAMESPACE_NAME = Profile.ENV;
045:
046: /**
047: * The name of the environment variable that specifies the path to the
048: * proxy.
049: */
050: public static final String X509_USER_PROXY_KEY = "X509_USER_PROXY";
051:
052: /**
053: * The name of the environment variable that specifies the Gridstart PREJOB.
054: */
055: public static final String GRIDSTART_PREJOB = "GRIDSTART_PREJOB";
056:
057: /**
058: * The name of the implementing namespace. It should be one of the valid
059: * namespaces always.
060: *
061: * @see Namespace#isNamespaceValid(String)
062: */
063: protected String mNamespace;
064:
065: /**
066: * The default constructor.
067: * Note that the map is not allocated memory at this stage. It is done so
068: * in the overloaded construct function.
069: */
070: public ENV() {
071: mProfileMap = null;
072: mNamespace = NAMESPACE_NAME;
073: }
074:
075: /**
076: * The overloaded constructor.
077: *
078: * @param mp map (possibly empty).
079: */
080: public ENV(Map mp) {
081: mProfileMap = new TreeMap(mp);
082: mNamespace = NAMESPACE_NAME;
083: }
084:
085: /**
086: * Returns the name of the namespace associated with the profile implementations.
087: *
088: * @return the namespace name.
089: * @see #NAMESPACE_NAME
090: */
091: public String namespaceName() {
092: return mNamespace;
093: }
094:
095: /**
096: * Constructs a new element of the format (key=value). It first checks if
097: * the map has been initialised or not. If not then allocates memory first.
098: *
099: * @param key is the left-hand-side
100: * @param value is the right hand side
101: */
102: public void construct(String key, String value) {
103: if (mProfileMap == null)
104: mProfileMap = new TreeMap();
105: mProfileMap.put(key, value);
106: }
107:
108: /**
109: * This checks whether the key passed by the user is valid in the current
110: * namespace or not. At present, for this namespace all the keys are
111: * construed as valid as long as the value passed is not null.
112: *
113: * @param key (left hand side)
114: * @param value (right hand side)
115: *
116: * @return Namespace.VALID_KEY
117: * @return Namespace.NOT_PERMITTED_KEY
118: *
119: */
120: public int checkKey(String key, String value) {
121: if (key == null || value == null)
122: return Namespace.NOT_PERMITTED_KEY;
123: return Namespace.VALID_KEY;
124: }
125:
126: /**
127: * Converts the contents of the map into the string that can be put in the
128: * Condor file for printing.
129: */
130: public String toString() {
131: StringBuffer st = new StringBuffer();
132: String key = null;
133: String value = null;
134:
135: Iterator it = (mProfileMap == null) ? null : mProfileMap
136: .keySet().iterator();
137: if (it == null)
138: return null;
139:
140: st.append("environment = ");
141: while (it.hasNext()) {
142: key = (String) it.next();
143: value = (String) mProfileMap.get(key);
144: st.append(key).append("=").append(value).append(";");
145: }
146: st.append("\n");
147: return st.toString();
148:
149: }
150:
151: /**
152: * It puts in the namespace specific information specified in the properties
153: * file into the namespace. The name of the pool is also passed, as many of
154: * the properties specified in the properties file are on a per pool basis.
155: *
156: * @param properties the <code>PegasusProperties</code> object containing
157: * all the properties that the user specified at various
158: * places (like .chimerarc, properties file, command line).
159: * @param pool the pool name where the job is scheduled to run.
160: */
161: public void checkKeyInNS(PegasusProperties properties, String pool) {
162: //get from the properties for pool local
163: String prop = pool.equalsIgnoreCase("local") ?
164: //check if property in props file
165: properties.getLocalPoolEnvVar()
166: : null;
167:
168: if (prop != null) {
169: checkKeyInNS(prop);
170: }
171:
172: }
173:
174: /**
175: * It takes in key=value pairs separated by a ; and puts them into the
176: * namespace after checking if they are valid or not.
177: *
178: * @param envString the String containing the environment variables and
179: * their values separated by a semi colon.
180: */
181: public void checkKeyInNS(String envString) {
182: //sanity check
183: if (envString == null)
184: return;
185:
186: StringTokenizer st = new StringTokenizer(envString, ";");
187: String name;
188: String value;
189: String keyValPair;
190:
191: while (st.hasMoreTokens()) {
192: keyValPair = (String) st.nextToken(";");
193: if (keyValPair.trim().equalsIgnoreCase("null")) {
194: return;
195: }
196: StringTokenizer st1 = new StringTokenizer(keyValPair);
197:
198: name = st1.nextToken("=");
199: value = st1.nextToken();
200:
201: checkKeyInNS(name, value);
202: }
203:
204: }
205:
206: /**
207: * Merge the profiles in the namespace in a controlled manner.
208: * In case of intersection, the new profile value overrides, the existing
209: * profile value.
210: *
211: * @param profiles the <code>Namespace</code> object containing the profiles.
212: */
213: public void merge(Namespace profiles) {
214: //check if we are merging profiles of same type
215: if (!(profiles instanceof ENV)) {
216: //throw an error
217: throw new IllegalArgumentException(
218: "Profiles mismatch while merging");
219: }
220: String key;
221: for (Iterator it = profiles.getProfileKeyIterator(); it
222: .hasNext();) {
223: //construct directly. bypassing the checks!
224: key = (String) it.next();
225: this .construct(key, (String) profiles.get(key));
226: }
227: }
228:
229: /**
230: * Returns a copy of the current namespace object.
231: *
232: * @return the Cloned object
233: */
234: public Object clone() {
235: return (mProfileMap == null ? new ENV() : new ENV(
236: this.mProfileMap));
237: }
238:
239: }
|