001: /**
002: * This file or a portion of this file is licensed under the terms of
003: * the Globus Toolkit Public License, found at $PEGASUS_HOME/GTPL or
004: * http://www.globus.org/toolkit/download/license.html.
005: * This notice must appear in redistributions of this file
006: * with or without modification.
007: *
008: * Redistributions of this Software, with or without modification, must reproduce
009: * the GTPL in:
010: * (1) the Software, or
011: * (2) the Documentation or
012: * some other similar material which is provided with the Software (if any).
013: *
014: * Copyright 1999-2004
015: * University of Chicago and The University of Southern California.
016: * All rights reserved.
017: */package org.griphyn.cPlanner.classes;
018:
019: import org.griphyn.common.classes.SysInfo;
020:
021: import org.griphyn.cPlanner.common.PegRandom;
022: import org.griphyn.cPlanner.common.Utility;
023: import org.griphyn.cPlanner.common.LogManager;
024:
025: import java.util.List;
026: import java.util.ArrayList;
027: import java.util.Iterator;
028:
029: /**
030: * This is a data class that is used to store information about a single
031: * remote site (pool).
032: *
033: * <p>
034: * The various types of information that can be associated with the the remote
035: * site are displayed in the following table.
036: *
037: * <p>
038: * <table border="1">
039: * <tr align="left"><th>Name</th><th>Description</th></tr>
040: * <tr align="left"><th>grid launch</th>
041: * <td>the path to kickstart on the remote site.</td>
042: * </tr>
043: * <tr align="left"><th>work directory</th>
044: * <td>the <code>WorkDir</code> object containing the information about the
045: * scratch space on the remote site.</td>
046: * </tr>
047: * <tr align="left"><th>grid ftp servers</th>
048: * <td>the list of <code>GridFTPServer</code> objects each containing information
049: * about one grid ftp server.</td>
050: * </tr>
051: * <tr align="left"><th>job managers</th>
052: * <td>the list of <code>JobManager</code> objects each containing information
053: * about one jobmanager.</td>
054: * </tr>
055: * <tr align="left"><th>profiles</th>
056: * <td>the list of <code>Profile</code> objects each containing one profile.</td>
057: * </tr>
058: * <tr align="left"><th>system info</th>
059: * <td>the <code>SysInfo</code> object containing the remote sites system
060: * information.</td>
061: * </tr>
062: * </table>
063: *
064: *
065: * @author Gaurang Mehta gmehta@isi.edu
066: * @author Karan Vahi vahi@isi.edu
067: *
068: * @version $Revision: 178 $
069: *
070: * @see GlobusVersion
071: * @see GridFTPServer
072: * @see GridFTPBandwidth
073: * @see JobManager
074: * @see LRC
075: * @see Profile
076: * @see SiteInfo
077: * @see org.griphyn.common.classes.SysInfo
078: * @see WorkDir
079: */
080: public class SiteInfo {
081:
082: /**
083: * Array storing the names of the attributes that are stored with the
084: * site.
085: */
086: public static final String SITEINFO[] = { "grid-ftp-server",
087: "jobmanager", "profile", "lrc", "workdir", "gridlaunch",
088: "sysinfo", "handle" };
089:
090: /**
091: * The constant to be passed to the accessor functions to get or set the
092: * list of <code>GridFTP</code> objects for the remote site.
093: */
094: public static final int GRIDFTP = 0;
095:
096: /**
097: * The constant to be passed to the accessor functions to get or set the
098: * list of <code>JobManager</code> objects for the remote site.
099: */
100: public static final int JOBMANAGER = 1;
101:
102: /**
103: * The constant to be passed to the accessor functions to get or set the
104: * list of <code>Profile</code> objects for the remote site.
105: */
106: public static final int PROFILE = 2;
107:
108: /**
109: * The constant to be passed to the accessor functions to get or set the list
110: * of <code>LRC</code> objects for the remote site.
111: */
112: public static final int LRC = 3;
113:
114: /**
115: * The constant to be passed to the accessor functions to get or set the
116: * List of <code>WorkDir</code> objects.
117: */
118: public static final int WORKDIR = 4;
119:
120: /**
121: * The constant to be passed to the accessor functions to get or set the
122: * path to kickstart.
123: */
124: public static final int GRIDLAUNCH = 5;
125:
126: /**
127: * The constant to be passed to the accessor functions to get or set the
128: * <code>SysInfo</code> site.
129: */
130: public static final int SYSINFO = 6;
131:
132: /**
133: * The name of the remote site. This is acts as the key by which to query
134: * a site catalog for information regarding a particular remote site.
135: */
136: public static final int HANDLE = 7;
137:
138: /**
139: * The path to the kickstart on the remote site.
140: */
141: private String mGridLaunch;
142:
143: /**
144: * The list of <code>LRC</code> objects that contain the information about
145: * the various LRCs associated with the remote site.
146: */
147: private List mLRCList;
148:
149: /**
150: * The list of <code>Profile</code> objects that contain the profile
151: * information associated with the remote site.
152: */
153: private List mProfileList;
154:
155: /**
156: * The list of <code>GridFTPServer</code> objects that contain the information
157: * about the gridftp servers on the remote site.
158: */
159: private List mGridFTPList;
160:
161: /**
162: * The list of <code>JobManager</code> objects that contain the information
163: * about the jobmanagers associated with the remote site.
164: */
165: private List mJobManagerList;
166:
167: /**
168: * Contains the information about the work directory on the remote site.
169: */
170: private WorkDir mWorkDir;
171:
172: /**
173: * The system information of the remote site.
174: */
175: private SysInfo mSysInfo;
176:
177: /**
178: * The handle to the site, usually name of the site.
179: */
180: private String mHandle;
181:
182: /**
183: * Default Constructor.
184: */
185: public SiteInfo() {
186: mHandle = null;
187: mLRCList = new ArrayList(3);
188: mProfileList = new ArrayList(3);
189: mGridFTPList = new ArrayList(3);
190: mJobManagerList = new ArrayList(5);
191: mSysInfo = new SysInfo();
192: mWorkDir = new WorkDir();
193: }
194:
195: /**
196: * Returns an <code>Object</code> containing the attribute value
197: * corresponding to the key specified.
198: *
199: * @param key the key.
200: *
201: * @return <code>Object</code> corresponding to the key value.
202: * @throws RuntimeException if illegal key defined.
203: *
204: *
205: * @see #HANDLE
206: * @see #GRIDFTP
207: * @see #GRIDLAUNCH
208: * @see #JOBMANAGER
209: * @see #LRC
210: * @see #PROFILE
211: * @see #SYSINFO
212: * @see #WORKDIR
213: */
214: public Object getInfo(int key) {
215:
216: switch (key) {
217: case 0:
218: return mGridFTPList;
219:
220: case 1:
221: return mJobManagerList;
222:
223: case 2:
224: return mProfileList;
225:
226: case 3:
227: return mLRCList;
228:
229: case 4:
230: return mWorkDir;
231:
232: case 5:
233: return mGridLaunch;
234:
235: case 6:
236: return mSysInfo;
237:
238: case 7:
239: return mHandle;
240:
241: default:
242: throw new RuntimeException(
243: " Wrong site key. Please use one of the predefined key types");
244: }
245:
246: }
247:
248: /**
249: * A helper method that returns the execution mount point.
250: *
251: * @return the execution mount point, else
252: * null if no mount point associated with the pool.
253: */
254: public String getExecMountPoint() {
255: Object workdir = getInfo(this .WORKDIR);
256:
257: return (workdir == null) ? null : ((WorkDir) workdir)
258: .getInfo(WorkDir.WORKDIR);
259: }
260:
261: /**
262: * A helper method that returns the path to gridlaunch on the site.
263: *
264: * @return the path to the kickstart.
265: */
266: public String getKickstartPath() {
267: Object path = getInfo(this .GRIDLAUNCH);
268:
269: return (path == null) ? null : ((String) path);
270: }
271:
272: /**
273: * A helper method that returns the url prefix for one of the gridftp server
274: * associated with the pool. If more than one gridftp servers is associated
275: * with the pool, then the function returns url prefix for the first
276: * gridftp server in the list, unless the parameter random is set to true.
277: *
278: * @param random boolean denoting whether to select a random gridftp server.
279: *
280: * @return the url prefix for the grid ftp server,
281: * else null if no gridftp server mentioned.
282: */
283: public String getURLPrefix(boolean random) {
284: String url = null;
285: GridFTPServer server = selectGridFTP(random);
286: url = server.getInfo(GridFTPServer.GRIDFTP_URL);
287: //on the safe side should prune also..
288: return Utility.pruneURLPrefix(url);
289: }
290:
291: /**
292: * It returns all the jobmanagers corresponding to a specified pool.
293: *
294: * @return list of <code>JobManager</code>, each referring to
295: * one jobmanager contact string. An empty list if no jobmanagers
296: * found.
297: */
298: public List getJobmanagers() {
299: Object obj;
300: return ((obj = getInfo(this .JOBMANAGER)) == null) ? new java.util.ArrayList(
301: 0)
302: : (List) obj;
303: }
304:
305: /**
306: * It returns all the jobmanagers corresponding to a specified pool and
307: * universe.
308: *
309: * @param universe the gvds universe with which it is associated.
310: *
311: * @return list of <code>JobManager</code>, each referring to
312: * one jobmanager contact string. An empty list if no jobmanagers
313: * found.
314: */
315: public List getJobmanagers(String universe) {
316: Object obj;
317: return ((obj = getInfo(this .JOBMANAGER)) == null) ? new java.util.ArrayList(
318: 0)
319: : this .getMatchingJMList((List) obj, universe);
320: }
321:
322: /**
323: * Sets an attribute associated with the remote site. It actually
324: * adds to the list where there is a list maintained like for grid ftp servers,
325: * jobmanagers, profiles, and LRCs.
326: *
327: * @param key the attribute key, which is one of the predefined keys.
328: * @param object the object containing the attribute value.
329: *
330: * @throws RuntimeException if the object passed for the key is not of
331: * valid type.
332: *
333: * @throws Exception if illegal key defined.
334: *
335: *
336: * @see #HANDLE
337: * @see #GRIDFTP
338: * @see #GRIDLAUNCH
339: * @see #JOBMANAGER
340: * @see #LRC
341: * @see #PROFILE
342: * @see #SYSINFO
343: * @see #WORKDIR
344: */
345: public void setInfo(int key, Object object) throws RuntimeException {
346:
347: //to denote if object is of valid type or not.
348: boolean valid = true;
349:
350: switch (key) {
351: case GRIDFTP:
352: if (object != null && object instanceof GridFTPServer)
353: mGridFTPList.add(object);
354: else
355: valid = false;
356: break;
357:
358: case JOBMANAGER:
359: if (object != null && object instanceof JobManager)
360: mJobManagerList.add(object);
361: else
362: valid = false;
363: break;
364:
365: case PROFILE:
366: if (object != null && object instanceof Profile)
367: mProfileList.add(object);
368: else
369: valid = false;
370: break;
371:
372: case LRC:
373: if (object != null && object instanceof LRC)
374: mLRCList.add(object);
375: else
376: valid = false;
377: break;
378:
379: case WORKDIR:
380: if (object != null && object instanceof WorkDir)
381: mWorkDir = (WorkDir) object;
382: else {
383: valid = false;
384: mWorkDir = null;
385: }
386:
387: break;
388:
389: case GRIDLAUNCH:
390: if (object != null && object instanceof String)
391: mGridLaunch = (String) object;
392: else {
393: valid = false;
394: mGridLaunch = null;
395: }
396: break;
397:
398: case SYSINFO:
399:
400: if (object != null && object instanceof String)
401: mSysInfo = new SysInfo((String) object);
402: else if (object != null && object instanceof SysInfo) {
403: mSysInfo = (SysInfo) object;
404: } else {
405: valid = false;
406: mSysInfo = null;
407: }
408:
409: break;
410:
411: case HANDLE:
412: if (object != null && object instanceof String)
413: mHandle = (String) object;
414: else {
415: valid = false;
416: mHandle = null;
417: }
418: break;
419:
420: default:
421: throw new RuntimeException(
422: " Wrong site key. Please use one of the predefined key types");
423: }
424:
425: //if object is not null , and valid == false
426: //throw exception
427: if (!valid && object != null) {
428: throw new RuntimeException("Invalid object passed for key "
429: + SITEINFO[key]);
430: }
431: }
432:
433: /**
434: * It removes a jobmanager from the pool. It calls the underlying equals
435: * method of the associated jobmanager object to remove it.
436: *
437: * @param universe the gvds universe with which it is associated.
438: * @param jobManagerContact the contact string to the jobmanager.
439: *
440: * @return true if was able to remove successfully
441: * else false.
442: */
443: public boolean removeJobmanager(String universe,
444: String jobManagerContact) {
445: if (mJobManagerList == null) {
446: return false;
447: }
448:
449: JobManager jm = new JobManager();
450: boolean val = false;
451:
452: try {
453: jm.setInfo(JobManager.UNIVERSE, universe);
454: jm.setInfo(JobManager.URL, jobManagerContact);
455: } catch (Exception e) {
456: //wonder why gaurang throws it
457: LogManager.getInstance().log(
458: "Exception while removing jobmanager:"
459: + e.getMessage(),
460: LogManager.ERROR_MESSAGE_LEVEL);
461: return false;
462: }
463: synchronized (mJobManagerList) {
464: val = mJobManagerList.remove(jm);
465: }
466: jm = null;
467: return val;
468: }
469:
470: /**
471: * Removes a grid ftp server from the soft state associated with the pool.
472: *
473: * @param urlPrefix the urlprefix associated with the server.
474: *
475: * @return boolean
476: */
477: public boolean removeGridFtp(String urlPrefix) {
478: if (mGridFTPList == null)
479: return false;
480:
481: GridFTPServer server = new GridFTPServer();
482: boolean val = false;
483:
484: try {
485: server.setInfo(GridFTPServer.GRIDFTP_URL, urlPrefix);
486: } catch (Exception e) {
487: //wonder why gaurang throws it
488: LogManager.getInstance().log(
489: "Exception while removing jobmanager:"
490: + e.getMessage(),
491: LogManager.ERROR_MESSAGE_LEVEL);
492: return false;
493:
494: }
495: synchronized (mGridFTPList) {
496: val = mGridFTPList.remove(server);
497: server = null;
498: }
499: return val;
500: }
501:
502: /**
503: * Returns a gridftp server from the list of gridftp servers associated with
504: * the site. If more than one candidate GridFTPServer is found , then the
505: * function returns the first matching <code>GridFTPServer</code>
506: * unless parameter random is set to true.
507: *
508: * @param random boolean denoting whether to select a random gridftp server.
509: *
510: * @return the selected <code>GridFTPServer</code> corresponding to the
511: * grid ftp server,
512: * else null if list is null.
513: *
514: * @see org.griphyn.cPlanner.classes.GridFTPServer
515: */
516: public GridFTPServer selectGridFTP(boolean random) {
517: List l = (List) this .getInfo(SiteInfo.GRIDFTP);
518: //sanity check
519: if (l == null || l.isEmpty())
520: return null;
521:
522: int sel = (random == true) ? PegRandom.getInteger(l.size() - 1)
523: : 0;
524:
525: return (GridFTPServer) (l.get(sel));
526: }
527:
528: /**
529: * Returns an LRC from the list of LRCs associated with the site.
530: * If more than one candidate LRC is found , then the function
531: * the first matching <code>LRC</code< unless parameter random is set to true.
532: *
533: * @param random boolean denoting whether to select a random gridftp server.
534: *
535: * @return the selected <code>LRC</code> corresponding to the selected LRC.
536: * else null if list is null.
537: *
538: * @see org.griphyn.cPlanner.classes.LRC
539: */
540: public LRC selectLRC(boolean random) {
541: List l = (List) this .getInfo(SiteInfo.LRC);
542: //sanity check
543: if (l == null || l.isEmpty())
544: return null;
545:
546: int sel = (random == true) ? PegRandom.getInteger(l.size() - 1)
547: : 0;
548:
549: return (LRC) (l.get(sel));
550: }
551:
552: /**
553: * Returns a selected jobmanager corresponding to a particular VDS
554: * universe.
555: * If more than one candidate jobmanager is found , then the function
556: * the first matching jobmanager unless parameter random is set to true.
557: *
558: * @param universe the VDS universe with which the jobmanager is associated.
559: * @param random boolean denoting whether to select a random gridftp server.
560: *
561: * @return the selected jobmanager,
562: * else null if list is null.
563: *
564: * @see org.griphyn.cPlanner.classes.JobManager
565: */
566: public JobManager selectJobManager(String universe, boolean random) {
567: List l = (List) this .getInfo(SiteInfo.JOBMANAGER);
568: //sanity check
569: if (l == null || l.isEmpty())
570: return null;
571:
572: //match on the universe
573: l = this .getMatchingJMList(l, universe);
574:
575: //do a sanity check again
576: if (l == null || l.isEmpty())
577: return null;
578:
579: int sel = (random == true) ? PegRandom.getInteger(l.size() - 1)
580: : 0;
581:
582: return (JobManager) (l.get(sel));
583: }
584:
585: /**
586: * Returns the textual description of the contents of <code>SiteInfo</code>
587: * object in the multiline format.
588: *
589: * @return the textual description in multiline format.
590: */
591: public String toMultiLine() {
592: String output = "site " + mHandle + "{\n";
593: if (mSysInfo != null) {
594: output += "sysinfo \"" + mSysInfo + "\"\n";
595: }
596: if (mGridLaunch != null) {
597: output += "gridlaunch \"" + mGridLaunch + "\"\n";
598: }
599: if (mWorkDir != null) {
600: output += mWorkDir.toMultiLine() + "\n";
601: }
602: if (!mGridFTPList.isEmpty()) {
603: for (Iterator i = mGridFTPList.iterator(); i.hasNext();) {
604: output += ((GridFTPServer) i.next()).toMultiLine()
605: + "\n";
606: }
607: }
608: if (!mJobManagerList.isEmpty()) {
609: for (Iterator i = mJobManagerList.iterator(); i.hasNext();) {
610: output += ((JobManager) i.next()).toMultiLine() + "\n";
611: }
612: }
613: if (!mLRCList.isEmpty()) {
614: for (Iterator i = mLRCList.iterator(); i.hasNext();) {
615: output += ((LRC) i.next()).toMultiLine() + "\n";
616: }
617: }
618: if (!mProfileList.isEmpty()) {
619: for (Iterator i = mProfileList.iterator(); i.hasNext();) {
620: output += ((Profile) i.next()).toMultiLine() + "\n";
621: }
622: }
623: output += "}\n";
624: // System.out.println(output);
625: return output;
626:
627: }
628:
629: /**
630: * Returns the textual description of the contents of <code>SiteInfo</code>
631: * object.
632: *
633: * @return the textual description.
634: */
635: public String toString() {
636: String output = "{\n";
637: if (mSysInfo != null) {
638: output += "sysinfo \"" + mSysInfo + "\"\n";
639: }
640: if (mGridLaunch != null) {
641: output += "gridlaunch \"" + mGridLaunch + "\"\n";
642: }
643: if (mWorkDir != null) {
644: output += mWorkDir.toString() + "\n";
645: }
646: if (!mGridFTPList.isEmpty()) {
647: for (Iterator i = mGridFTPList.iterator(); i.hasNext();) {
648: output += ((GridFTPServer) i.next()).toString() + "\n";
649: }
650: }
651: if (!mJobManagerList.isEmpty()) {
652: for (Iterator i = mJobManagerList.iterator(); i.hasNext();) {
653: output += ((JobManager) i.next()).toString() + "\n";
654: }
655: }
656: if (!mLRCList.isEmpty()) {
657: for (Iterator i = mLRCList.iterator(); i.hasNext();) {
658: output += ((LRC) i.next()).toString() + "\n";
659: }
660: }
661: if (!mProfileList.isEmpty()) {
662: for (Iterator i = mProfileList.iterator(); i.hasNext();) {
663: output += ((Profile) i.next()).toString() + "\n";
664: }
665: }
666: output += "}\n";
667: // System.out.println(output);
668: return output;
669: }
670:
671: /**
672: * Returns the XML description of the contents of <code>SiteInfo</code>
673: * object.
674: *
675: * @return the xml description.
676: */
677: public String toXML() {
678: String output = "";
679: if (mGridLaunch != null) {
680: output += " gridlaunch=\"" + mGridLaunch + "\"";
681: }
682: if (mSysInfo != null) {
683: output += " sysinfo=\"" + mSysInfo + "\"";
684: }
685: output += ">\n";
686: if (!mProfileList.isEmpty()) {
687: for (Iterator i = mProfileList.iterator(); i.hasNext();) {
688: output += " " + ((Profile) i.next()).toXML() + "\n";
689: }
690: }
691: if (!mLRCList.isEmpty()) {
692: for (Iterator i = mLRCList.iterator(); i.hasNext();) {
693: output += " " + ((LRC) i.next()).toXML() + "\n";
694: }
695: }
696: if (!mGridFTPList.isEmpty()) {
697: for (Iterator i = mGridFTPList.iterator(); i.hasNext();) {
698: output += " " + ((GridFTPServer) i.next()).toXML()
699: + "\n";
700: }
701: }
702: if (!mJobManagerList.isEmpty()) {
703: for (Iterator i = mJobManagerList.iterator(); i.hasNext();) {
704: output += " " + ((JobManager) i.next()).toXML()
705: + "\n";
706: }
707: }
708:
709: if (mWorkDir != null) {
710: output += " " + mWorkDir.toXML() + "\n";
711: }
712:
713: output += " </site>\n";
714: return output;
715: }
716:
717: /**
718: * Returns a list containing only those jobmanager entries that match a
719: * particular universe.
720: *
721: * @param superList the list containing all the entries of type <code>
722: * JobManager</code>.
723: * @param universe the universe against which you want to match the
724: * entries.
725: *
726: * @return List which is a subset of the elements in the superList
727: */
728: private List getMatchingJMList(List super List, String universe) {
729: ArrayList subList = new ArrayList(0);
730:
731: for (Iterator i = super List.iterator(); i.hasNext();) {
732: JobManager jbinfo = (JobManager) i.next();
733:
734: if (jbinfo.getInfo(JobManager.UNIVERSE).equalsIgnoreCase(
735: universe)) {
736:
737: subList.add(jbinfo);
738: }
739: }
740:
741: return subList;
742:
743: }
744:
745: }
|