Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that handles scheduling tasks 
4
 *  Copyright: 2009 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 * 
8
 *   '$Author: daigle $'
9
 *     '$Date: 2009-03-25 13:41:15 -0800 (Wed, 25 Mar 2009) $'
10
 * '$Revision: 4861 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.workflow;
28

    
29
import java.text.DateFormat;
30
import java.text.ParseException;
31
import java.util.Calendar;
32
import java.util.Date;
33
import java.util.Enumeration;
34
import java.util.Hashtable;
35
import java.util.HashMap;
36

    
37
import javax.servlet.http.HttpServletRequest;
38
import javax.servlet.http.HttpServletResponse;
39

    
40
import org.apache.log4j.Logger;
41

    
42
import edu.ucsb.nceas.metacat.scheduler.BaseScheduler;
43
import edu.ucsb.nceas.metacat.scheduler.SchedulerService;
44
import edu.ucsb.nceas.metacat.scheduler.MetacatSchedulerException;
45
import edu.ucsb.nceas.metacat.service.ServiceException;
46

    
47
public class WorkflowScheduler extends BaseScheduler {
48
	
49
	private static WorkflowScheduler workflowScheduler = null;
50
	
51
	private static Logger logMetacat = Logger.getLogger(WorkflowScheduler.class);
52
	
53
	private static String SCHEDULED_JOB_GROUP = "workflow";
54
	private static String SCHEDULED_JOB_CLASS = "edu.ucsb.nceas.metacat.workflow.WorkflowJob";
55

    
56
	/**
57
	 * private constructor since this is a singleton
58
	 */
59
	private WorkflowScheduler()  {}
60
	
61
	/**
62
	 * Get the single instance of SchedulerService.
63
	 * 
64
	 * @return the single instance of SchedulerService
65
	 */
66
	public static WorkflowScheduler getInstance() {
67
		if (workflowScheduler == null) {
68
			workflowScheduler = new WorkflowScheduler();
69
		}
70
		return workflowScheduler;
71
	}
72
	
73
	public String scheduleJob(Hashtable<String, String[]> params,
74
			HttpServletRequest request, HttpServletResponse response, String username,
75
			String[] groups) throws MetacatSchedulerException {
76
		
77
		String delays[] = params.get("delay");
78
		String startTimes[] = params.get("starttime");
79
		HashMap<String, String> jobParams = new HashMap<String, String>();
80
		Calendar startCal = null;
81

    
82
		try {
83
			SchedulerService schedulerService = SchedulerService.getInstance();
84

    
85
			if (delays != null && delays.length > 0) {
86
				startCal = schedulerService.getStartDateFromDelay(delays[0]);
87
			} else if (startTimes != null && startTimes.length > 0) {
88
				Date startDate = DateFormat.getInstance().parse(startTimes[0]);
89
				startCal = Calendar.getInstance();
90
				startCal.setTime(startDate);
91
			} else {
92
				// if delay and starttime were not provided, set date to now.
93
				startCal = Calendar.getInstance();
94
			}
95

    
96
			String intervals[] = params.get("interval");
97
			if (intervals == null || intervals.length == 0) {
98
				throw new MetacatSchedulerException(
99
						"SchedulerService.scheduleHandler - Interval field must be populated "
100
								+ "in scheduler parameters when scheduling job.");
101
			}
102
			String interval = intervals[0];
103

    
104
			Enumeration<String> paramNames = params.keys();
105
			while (paramNames.hasMoreElements()) {
106
				String paramName = paramNames.nextElement();
107
				if (paramName.startsWith("jobparam_")) {
108
					jobParams.put(paramName.substring(9), params.get(paramName)[0]);
109
				}
110
			}
111

    
112
			String jobName = SCHEDULED_JOB_GROUP
113
					+ Calendar.getInstance().getTimeInMillis();
114

    
115
			return schedulerService.scheduleJob(jobName, startCal, interval,
116
					SCHEDULED_JOB_CLASS, SCHEDULED_JOB_GROUP, jobParams);
117
		} catch (ParseException pe) {
118
			throw new MetacatSchedulerException("WorkflowScheduler.scheduleHandler - Could not " + "schedule job  : " 
119
					+ pe.getMessage());
120
		} catch (ServiceException se) {
121
			throw new MetacatSchedulerException("WorkflowScheduler.scheduleHandler - Service issue scheduling job", se);
122
		}
123
	}
124
	
125
	public String unScheduleJob(Hashtable<String, String[]> params,
126
            HttpServletRequest request, HttpServletResponse response,
127
            String username, String[] groups) throws MetacatSchedulerException {
128
		return "";
129
	}
130
	
131
	public String deleteJob(Hashtable<String, String[]> params,
132
            HttpServletRequest request, HttpServletResponse response,
133
            String username, String[] groups) throws MetacatSchedulerException {
134
		return "";
135
	}
136
	
137
	public String getJobs(Hashtable<String, String[]> params,
138
            HttpServletRequest request, HttpServletResponse response,
139
            String username, String[] groups) throws MetacatSchedulerException {
140
		
141
		String groupName = null;
142
		String groupNames[] = params.get("groupName");
143
		if (groupNames != null && groupNames.length != 0) {
144
			groupName = groupNames[0];
145
		}
146
		
147
		try {
148
			return SchedulerService.getInstance().getJobsInfoXML(groupName);
149
		} catch (ServiceException se) {
150
			throw new MetacatSchedulerException("WorkflowScheduler.getJobs - Service issue getting jobs", se);
151
		}		
152
	}	
153
}
(2-2/2)