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.classes;
017:
018: import org.griphyn.cPlanner.common.LogManager;
019: import org.griphyn.common.catalog.TransformationCatalogEntry;
020:
021: import java.util.ArrayList;
022: import java.util.HashMap;
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028:
029: /**
030: * This is a data class to store the TCMAP for a particular dag.
031: * This data class is populated and maintained in the TCMapper and is queried
032: * from the Interpool Engine and site selectors.
033: *
034: * The TCMAP is a hashmap which maps an lfn to a Map which contains keys as
035: * siteids and values as List of TransformationCatalogEntry objects
036: *
037: * TCMAP= lfn1 ---> MAP1
038: * lfn2 ---> MAP2
039: *
040: *
041: * MAP1 = site1 ---> List1
042: * site2 ---> List2
043: *
044: * List1 = TCE1
045: * TCE2
046: * TCEn
047: *
048: *
049: * @author Gaurang Mehta
050: * @version $Revision: 50 $
051: */
052: public class TCMap {
053:
054: /**
055: * The TCMap for a dag is stored in this HashMap.
056: */
057: private Map mTCMap;
058:
059: private LogManager mLogger;
060:
061: /**
062: * Default constructor. Initializes the tcmap to 10 lfns.
063: */
064: public TCMap() {
065: mLogger = LogManager.getInstance();
066: mTCMap = new HashMap(10);
067: }
068:
069: /**
070: * Returns a HashMap of sites as keys and a List of TransformationCatalogEntry object as values.
071: * @param fqlfn String The fully qualified logical transformation name for which you want the map.
072: * @return Map Returns <B>NULL</B> if the transformation does not exist in the map.
073: * @see org.griphyn.common.catalog.TransformationCatalogEntry
074: */
075: public Map getSiteMap(String fqlfn) {
076: return mTCMap.containsKey(fqlfn) ? (Map) mTCMap.get(fqlfn)
077: : null;
078: }
079:
080: /**
081: * This method allows to associate a site map with a particular logical transformation
082: * @param fqlfn String The transformation for which the sitemap is to be stored
083: * @param sitemap Map The sitemap that is to be stored. It is a hashmap with key
084: * as the siteid and value as a list of TranformationCatalogEntry objects
085: * @return boolean
086: * @see org.griphyn.common.catalog.TransformationCatalogEntry
087: */
088: public boolean setSiteMap(String fqlfn, Map sitemap) {
089: mTCMap.put(fqlfn, sitemap);
090: return true;
091: }
092:
093: /**
094: * Returns a List of siteid's that are valid for a particular lfn.
095: * @param fqlfn String
096: * @return List
097: */
098: public List getSiteList(String fqlfn) {
099: List results = null;
100: if (mTCMap.containsKey(fqlfn)) {
101: return new ArrayList(((Map) mTCMap.get(fqlfn)).keySet());
102: }
103: return results;
104: }
105:
106: /**
107: * Returns a list of siteid's that are valid for a particular lfn and
108: * among a list of input sites
109: * @param fqlfn The logical name of the transformation
110: * @param sites The list of siteids
111: * @return the list of siteids which are valid.
112: */
113: public List getSiteList(String fqlfn, List sites) {
114: List results = new ArrayList();
115: if (mTCMap.containsKey(fqlfn)) {
116: for (Iterator i = ((Map) mTCMap.get(fqlfn)).keySet()
117: .iterator(); i.hasNext();) {
118: String site = (String) i.next();
119: if (sites.contains(site)) {
120: results.add(site);
121: }
122: }
123: }
124: return results.isEmpty() ? null : results;
125: }
126:
127: /**
128: * This method returns a list of TransformationCatalogEntry objects
129: * for a given transformation and siteid
130: * @param fqlfn String The fully qualified logical name of the transformation
131: * @param siteid String The siteid for which the Entries are required
132: * @return List returns NULL if no entries exist.
133: */
134: public List getSiteTCEntries(String fqlfn, String siteid) {
135: Map sitemap = null;
136: List tcentries = null;
137: if (mTCMap.containsKey(fqlfn)) {
138: sitemap = (Map) mTCMap.get(fqlfn);
139: if (sitemap.containsKey(siteid)) {
140: tcentries = (List) sitemap.get(siteid);
141: } else {
142: mLogger.log("The TCMap does not contain the site \""
143: + siteid + "\" for the transformation \""
144: + fqlfn + "\"", LogManager.DEBUG_MESSAGE_LEVEL);
145: }
146: } else {
147: mLogger.log(
148: "The TCMap does not contain the transformation \""
149: + fqlfn + "\"",
150: LogManager.DEBUG_MESSAGE_LEVEL);
151: }
152: return tcentries;
153: }
154:
155: /**
156: * Retrieves all the entries matching a particular lfn for the sites
157: * passed.
158: *
159: * @param fqlfn the fully qualified logical name
160: * @param sites the list of siteID's for which the entries are required.
161: *
162: * @return a map indexed by site names. Each value is a collection of
163: * <code>TransformationCatalogEntry</code> objects. Returns null in case
164: * of no entry being found.
165: */
166: public Map getSitesTCEntries(String fqlfn, List sites) {
167: Map m = this .getSiteMap(fqlfn);
168: Set siteIDS = new HashSet(sites);
169: String site = null;
170: if (m == null) {
171: return null;
172: }
173:
174: Map result = new HashMap(siteIDS.size());
175: for (Iterator it = m.keySet().iterator(); it.hasNext();) {
176: site = (String) it.next();
177: if (siteIDS.contains(site)) {
178: result.put(site, m.get(site));
179: }
180: }
181:
182: //returning NULL only to maintain semantics
183: //for rest of mapper operations. Gaurang
184: //should change to return empty map
185: return result.isEmpty() ? null : result;
186: }
187:
188: /**
189: * This method allows to add a TransformationCatalogEntry object in the map
190: * to a particular transformation for a particular site
191: * @param fqlfn String The fully qualified logical transformation
192: * @param siteid String The site for which the TransformationCatalogEntry is valid
193: * @param entry TransformationCatalogEntry The Transformation CatalogEntry object to be added.
194: * @return boolean
195: */
196: public boolean setSiteTCEntries(String fqlfn, String siteid,
197: TransformationCatalogEntry entry) {
198: Map sitemap = null;
199: List tcentries = null;
200: if (mTCMap.containsKey(fqlfn)) {
201: sitemap = (Map) mTCMap.get(fqlfn);
202: } else {
203: sitemap = new HashMap(10);
204: setSiteMap(fqlfn, sitemap);
205: }
206: if (sitemap.containsKey(siteid)) {
207: tcentries = (List) sitemap.get(siteid);
208: } else {
209: tcentries = new ArrayList(10);
210: sitemap.put(siteid, tcentries);
211: }
212: tcentries.add(entry);
213: return true;
214: }
215:
216: /**
217: * Returns the textual description of the contents of the object
218: * @return String
219: */
220: public String toString() {
221: StringBuffer sb = new StringBuffer();
222: for (Iterator i = mTCMap.keySet().iterator(); i.hasNext();) {
223: sb.append(toString((String) i.next()));
224: }
225: return sb.toString();
226: }
227:
228: /**
229: * Returns a textual description of the object.
230: * @param lfn String
231: * @return the textual description.
232: */
233: public String toString(String lfn) {
234: StringBuffer sb = new StringBuffer();
235: sb.append("LFN = " + lfn + "\n");
236: sb.append("\tSite map\n");
237: Map sitemap = (Map) mTCMap.get(lfn);
238: for (Iterator j = sitemap.keySet().iterator(); j.hasNext();) {
239: String site = (String) j.next();
240: sb.append("\t\tSite=" + site + "\n");
241: List tc = (List) sitemap.get(site);
242: for (Iterator k = tc.iterator(); k.hasNext();) {
243: TransformationCatalogEntry tcentry = (TransformationCatalogEntry) k
244: .next();
245: sb.append("\t\t\tPfn="
246: + tcentry.getPhysicalTransformation() + "\n");
247: sb.append("\t\t\tPfn site=" + tcentry.getResourceId()
248: + "\n");
249:
250: }
251: }
252: return sb.toString();
253: }
254: }
|