001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: jianwu $'
006 * '$Date: 2013-02-28 00:07:47 +0000 (Thu, 28 Feb 2013) $' 
007 * '$Revision: 31500 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.job;
031
032import java.util.Hashtable;
033
034/**
035 * JobManagerFactory singleton class to create JobManager objects. This class
036 * provides a factory to store JobManager objects. The reference to a jobmanager
037 * is with "<name>-user@host", where <name> is one of the supported jobmanagers,
038 * e.g. Condor, PBS or LoadLeveler. This class should be used instead of the
039 * JobManager class directly
040 * 
041 * <p>
042 * 
043 * @author Norbert Podhorszki
044 */
045
046public class JobManagerFactory {
047
048        /* Singleton object */
049        public final static JobManagerFactory instance = new JobManagerFactory();
050
051        /* Private variables */
052        private static Hashtable jmgrTable = new Hashtable();
053
054        private JobManagerFactory() {
055                // jmgrTable = new Hashtable();
056        }
057
058        /**
059         * return an existing job manager
060         * 
061         * @return the existing jobmanager or null
062         */
063        public synchronized static JobManager get(String jmgrID) {
064                return (JobManager) jmgrTable.get(jmgrID);
065        }
066
067        /**
068         * return an existing job manager
069         * 
070         * @return the existing jobmanager or null
071         */
072        public synchronized static JobManager get(String supportname, String target) {
073                return get(createKey(supportname, target));
074        }
075
076        /** return an existing job manager OR create one now */
077        public synchronized static JobManager get(String supportname,
078                        String target, String binPath) throws JobException {
079                JobManager jmgr;
080                // System.out.println("org.kepler.job.JobManagerFactory.getJobManager(): supportclass = "
081                // + supportname +
082                // " target = " + target + " binPath = " + binPath );
083                jmgr = (JobManager) jmgrTable.get(createKey(supportname, target));
084                if (jmgr == null) {
085                        // System.out.println("Job Manager will now be created");
086                        jmgr = new JobManager();
087                        jmgr.selectJobManager(supportname, target, binPath);
088                        jmgrTable.put(createKey(supportname, target), jmgr);
089                }
090                return jmgr;
091        }
092
093        protected static String createKey(String supportname, String target) {
094                return (supportname + "-" + target);
095        }
096        
097        public synchronized static void removeJmgrFromTable(String supportname, String target) {
098                jmgrTable.remove(createKey(supportname,target));
099        }
100
101}