001/*
002 * Copyright (c) 2004-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: chandrika $'
006 * '$Date: 2011-01-04 02:26:26 +0000 (Tue, 04 Jan 2011) $' 
007 * '$Revision: 26608 $'
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.text.SimpleDateFormat;
033import java.util.Date;
034import java.util.Hashtable;
035
036/**
037 * This class provides a factory to store Job objects. The reference to a job is
038 * with a String id. This class should be used in Kepler actors instead of the
039 * Job class directly, so that job id's can be sent along.
040 * 
041 * <p>
042 * 
043 * @author Norbert Podhorszki
044 */
045
046public class JobFactory {
047
048        /* Singleton object */
049        public final static JobFactory instance = new JobFactory();
050
051        /* Private variables */
052        private static Hashtable jobTable = new Hashtable();
053        private static int jobIDCounter = 0;
054
055        private JobFactory() {
056                // jobTable = new Hashtable();
057        }
058
059        /**
060         * Create a new job object.
061         * 
062         * @return the unique ID for the job. Use method getJob() to get the Job
063         *         object itself.
064         */
065        public static synchronized String create() {
066                Job job;
067                String jobID = createUniqueKey(jobIDCounter);
068                job = new Job(jobID);
069                jobTable.put(jobID, job);
070                jobIDCounter++;
071                return jobID;
072        }
073
074        /**
075         * Return an existing job.
076         * 
077         * @return the existing job or null.
078         */
079        public static Job get(String jobID) {
080                return (Job) jobTable.get(jobID);
081        }
082
083        /**
084         * Remove an existing job.
085         * 
086         * @return true if such job existed, false otherwise
087         */
088        public static boolean remove(String jobID) {
089                if (jobTable.remove(jobID) == null)
090                        return false;
091                else
092                        return true;
093        }
094
095        protected synchronized static String createUniqueKey(int jobIDCounter) {
096                /** return ( new String("job"+jobIDCounter) ); */
097                return (new String(user + "_" + formatter.format(new Date()) + "_"
098                                + jobIDCounter));
099        }
100
101        private static String user = System.getProperty("user.name", "none");
102        private static SimpleDateFormat formatter = new SimpleDateFormat(
103                        "MMMdd_HHmmssz");
104        /* Note that SimpleDateFormat objects must be externally synchronised */
105
106}
107