Project

General

Profile

« Previous | Next » 

Revision 4959

Added by daigle over 15 years ago

Added generic scheduler and workflow scheduling functionality.

View differences:

src/edu/ucsb/nceas/metacat/workflow/WorkflowEngineClient.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements system utility methods 
4
 *  Copyright: 2008 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-23 13:56:56 -0800 (Mon, 23 Mar 2009) $'
10
 * '$Revision: 4854 $'
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.util.Hashtable;
30

  
31
import org.apache.log4j.Logger;
32

  
33
public class WorkflowEngineClient implements WorkflowClientInterface {
34

  
35
	private static Logger logMetacat = Logger.getLogger(WorkflowEngineClient.class);
36
	
37
	public void execute(String workflowName, Hashtable<String, String> additionalParams) {
38
		// do execution stuff here
39
	}
40
	
41
	public String getStatus(String workflowId) {
42
		String statusXML = "";
43
		
44
		// do status stuff here
45
		
46
		return statusXML;
47
	}
48

  
49
 }
50 0

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

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

  
30
import java.util.Hashtable;
31

  
32
/**
33
 * This interface will handle the access control for different documents:
34
 * the package-orented, single eml2 document and other single file
35
 */
36
public interface WorkflowClientInterface
37
{
38

  
39
	public void execute(String workflowName, Hashtable<String, String> additionalParams);
40
	
41
	public String getStatus(String workflowId); 
42
 
43
}
44 0

  
src/edu/ucsb/nceas/metacat/util/ErrorUtil.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements administrative methods 
4
 *  Copyright: 2008 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: 2008-08-22 16:26:10 -0700 (Fri, 22 Aug 2008) $'
10
 * '$Revision: 4299 $'
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.util;
28

  
29
import java.io.IOException;
30
import java.io.PrintWriter;
31
import java.util.Hashtable;
32

  
33
import javax.servlet.http.HttpServletResponse;
34

  
35
import org.apache.log4j.Logger;
36

  
37
public class ErrorUtil {
38
	
39
	private static Logger logMetacat = Logger.getLogger(RequestUtil.class);
40
	
41
	// 101XXX - general errors
42
	public static Long GENERAL_UTILITY_ERROR = new Long(101001);	
43
	public static Long METACAT_UTILITY_ERROR = new Long(101002);
44
	// 104XXX - property errors
45
	public static Long PROPERTY_NOT_FOUND = new Long(104001);	
46
	// 105XXX - permission errors
47
	public static Long NO_READ_PERMISSION = new Long(105001);
48
	
49
	// errorCodes is a lookup table for generic messages for each code.  It
50
	// is better to use the sendError() versions that accept an explicit 
51
	// error message;
52
	static Hashtable<Long, String> errorCodes = new Hashtable<Long, String>();
53
	static {
54
		errorCodes.put(GENERAL_UTILITY_ERROR, "General utility error");
55
		errorCodes.put(METACAT_UTILITY_ERROR, "Metacat utility error");
56
		errorCodes.put(PROPERTY_NOT_FOUND, "Property not found");
57
		errorCodes.put(NO_READ_PERMISSION, "Read permission denied for user");
58
	}
59
	
60
	/**
61
	 * private constructor - all methods are static so there is no
62
     * no need to instantiate.
63
	 */
64
	private ErrorUtil() {}
65
	
66
	public static void sendError(HttpServletResponse response, Long errorCode, String errorMessage) 
67
		throws ErrorSendingErrorException {
68
		
69
		PrintWriter out = null;
70
		try {
71
			out = response.getWriter();			
72
			response.setContentType("text/xml");
73
		
74
			out.println("<?xml version=\"1.0\"?>");
75
			out.println("<error>");
76
			out.println("<code>" + errorCode + "</code>");
77
			out.println("<message>" + errorMessage + "</message>");
78
			out.println("</error>");
79
		
80
		} catch (IOException ioe) {
81
			throw new ErrorSendingErrorException("I/O error when returning error: " 
82
					+ errorCode + " : " + errorMessage);
83
		} finally {
84
			if (out != null) {
85
				out.close();
86
			}
87
		}
88
	}
89
	
90
	public static void sendError(HttpServletResponse response, Long errorCode) 
91
		throws ErrorSendingErrorException {
92
		
93
		PrintWriter out = null;
94
		try {
95
			out = response.getWriter();	
96
			response.setContentType("text/xml");
97
		
98
			out.println("<?xml version=\"1.0\"?>");
99
			out.println("<error>");
100
			out.println("<code>" + errorCode + "</code>");
101
			out.println("<message>" + errorCodes.get(errorCode) + "</message>");
102
			out.println("</error>");
103
		
104
		} catch (IOException ioe) {
105
			throw new ErrorSendingErrorException("I/O error when returning error: " 
106
					+ errorCode);
107
		} finally {
108
			if (out != null) {
109
				out.close();
110
			}
111
		} out.close();
112
	}
113
	
114
	public static void sendError(HttpServletResponse response, PrintWriter out,
115
			Long errorCode, String errorMessage) throws ErrorSendingErrorException {
116

  
117
		response.setContentType("text/xml");
118

  
119
		out.println("<?xml version=\"1.0\"?>");
120
		out.println("<error>");
121
		out.println("<code>" + errorCode + "</code>");
122
		out.println("<message>" + errorMessage + "</message>");
123
		out.println("</error>");
124
	}
125

  
126
	public static void sendError(HttpServletResponse response, PrintWriter out,
127
			Long errorCode) throws ErrorSendingErrorException {
128

  
129
		response.setContentType("text/xml");
130

  
131
		out.println("<?xml version=\"1.0\"?>");
132
		out.println("<error>");
133
		out.println("<code>" + errorCode + "</code>");
134
		out.println("<message>" + errorCodes.get(errorCode) + "</message>");
135
		out.println("</error>");
136
	}
137
}
138 0

  
src/edu/ucsb/nceas/metacat/scheduler/ScheduledJobInterface.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that runs a single scheduled report task 
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-23 13:56:56 -0800 (Mon, 23 Mar 2009) $'
10
 * '$Revision: 4854 $'
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.scheduler;
28

  
29
import org.quartz.InterruptableJob;
30

  
31
public interface ScheduledJobInterface extends InterruptableJob {
32
	
33
	public static String STATE_SCHEDULED = "scheduled";
34
	public static String STATE_EXECUTING = "executing";
35
	public static String STATE_UNSHCEDULED = "unscheduled";	
36
	public static String STATE_DELETED = "deleted";
37
	
38
}
39 0

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

  
28
package edu.ucsb.nceas.metacat.scheduler;
29

  
30
import java.sql.PreparedStatement;
31
import java.sql.SQLException;
32
import java.util.Enumeration;
33
import java.util.Hashtable;
34

  
35
import org.apache.log4j.Logger;
36

  
37
import edu.ucsb.nceas.shared.AccessException;
38
import edu.ucsb.nceas.shared.BaseAccess;
39

  
40
public class ScheduledJobParamsAccess extends BaseAccess {
41
	
42
	private Logger logMetacat = Logger.getLogger(ScheduledJobParamsAccess.class);
43
	
44
	public ScheduledJobParamsAccess() throws AccessException {
45
		super("ScheduledJobParamsAccess");
46
	}
47
	
48
	protected void createJobParams(Long jobId, Hashtable<String, String> jobParams) throws AccessException {
49
		Enumeration<String> paramKeys = jobParams.keys();
50
		
51
		while(paramKeys.hasMoreElements()) {
52
			String paramKey = paramKeys.nextElement();
53
			
54
			ScheduledJobParamsDAO jobParamsDAO = new ScheduledJobParamsDAO();
55
			jobParamsDAO.setStatus(ScheduledJobInterface.STATE_SCHEDULED);
56
			jobParamsDAO.setJobId(jobId);
57
			jobParamsDAO.setKey(paramKey);
58
			jobParamsDAO.setValue(jobParams.get(paramKey));
59
			
60
			try {
61
				String sql = 
62
					"INSERT INTO scheduled_job_params (date_created, date_updated, status, job_id, key, value) " 
63
					+ "VALUES(now(), now(), ?, ?, ?, ?)";		
64
				PreparedStatement pstmt = conn.prepareStatement(sql);
65
			
66
				pstmt.setString(1, jobParamsDAO.getStatus());
67
				pstmt.setLong(2, jobParamsDAO.getJobId());
68
				pstmt.setString(3, jobParamsDAO.getKey());
69
				pstmt.setString(4, jobParamsDAO.getValue());
70
				
71
				logMetacat.info("SQL createJobParams - " + sql);
72
				logMetacat.info("SQL params:  [" + jobParamsDAO.getStatus() + ","
73
						+ jobParamsDAO.getJobId() + ","
74
						+ jobParamsDAO.getKey() + ","
75
						+ jobParamsDAO.getValue().toString() + "]");
76
				pstmt.execute();
77
				
78
			} catch (SQLException sqle) {
79
				// Just throw the exception.  The ScheduledJobAccess class should handle cleanup.
80
				throw new AccessException("ScheduledJobParamsAccess.createJobParams - SQL error when creating scheduled job parameter : "    
81
					 + sqle.getMessage());
82
			}
83
		}
84
	}
85
	
86
	protected void deleteJobParams(Long jobId) throws AccessException {
87
		try {
88
			String sql = "UPDATE scheduled_job_params SET status = ? WHERE jobId = ?";		
89
			pstmt.setString(1, ScheduledJobInterface.STATE_DELETED);
90
			pstmt.setLong(1, jobId);
91
			
92
			logMetacat.info("SQL deleteJobParams - " + sql);
93

  
94
			pstmt.execute();
95
		} catch (SQLException sqle) {
96
			throw new AccessException("ScheduledJobParamsAccess.deleteJobParams - SQL error " 
97
					+ "when deleting scheduled job params for job" + jobId  + " : "  + sqle.getMessage());
98
		}	
99
		
100
	}	
101
}
src/edu/ucsb/nceas/metacat/scheduler/ScheduledJobParamsDAO.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that holds the data from the scheduled_task 
4
 *             table in the database. 
5
 *  Copyright: 2009 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Michael Daigle
8
 * 
9
 *   '$Author: daigle $'
10
 *     '$Date: 2009-03-23 13:56:56 -0800 (Mon, 23 Mar 2009) $'
11
 * '$Revision: 4854 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

  
28
package edu.ucsb.nceas.metacat.scheduler;
29

  
30
import edu.ucsb.nceas.shared.BaseDAO;
31

  
32
public class ScheduledJobParamsDAO extends BaseDAO {
33

  
34
	private Long _jobId;
35
	private String _key;
36
	private String _value;
37
	
38
	public Long getJobId() {
39
		return _jobId;
40
	}
41
	
42
	public void setJobId(Long jobId) {
43
		_jobId = jobId;
44
	}
45
	
46
	public String getKey() {
47
		return _key;
48
	}
49
	
50
	public void setKey(String key) {
51
		_key = key;
52
	}
53
	
54
	public String getValue() {
55
		return _value;
56
	}
57
	
58
	public void setValue(String value) {
59
		_value = value;
60
	}
61
}
src/edu/ucsb/nceas/shared/AccessException.java
31 31
 * Exception thrown when an error occurs in a configuration administrative
32 32
 * class
33 33
 */
34
public class AccessException extends Exception {
34
public class AccessException extends BaseException {
35 35
	
36 36
	private static final long serialVersionUID = -8436697355629175917L;
37 37

  
......
43 43
	public AccessException(String message) {
44 44
		super(message);
45 45
	}
46
	
47
	public AccessException(String message, BaseException deeperException) {
48
		super(message, deeperException);
49
	}
46 50
}
src/edu/ucsb/nceas/shared/BaseException.java
1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: An Exception thrown when an error occurs because an 
4
 *             AccessionNumber was invalid or used incorrectly
5
 *  Copyright: 2008 Regents of the University of California and the
6
 *             National Center for Ecological Analysis and Synthesis
7
 *    Authors: Michael Daigle
8
 *
9
 *   '$Author: daigle $'
10
 *     '$Date: 2008-07-06 21:25:34 -0700 (Sun, 06 Jul 2008) $'
11
 * '$Revision: 4080 $'
12
 *
13
 * This program is free software; you can redistribute it and/or modify
14
 * it under the terms of the GNU General Public License as published by
15
 * the Free Software Foundation; either version 2 of the License, or
16
 * (at your option) any later version.
17
 *
18
 * This program is distributed in the hope that it will be useful,
19
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
 * GNU General Public License for more details.
22
 *
23
 * You should have received a copy of the GNU General Public License
24
 * along with this program; if not, write to the Free Software
25
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
 */
27

  
28
package edu.ucsb.nceas.shared;
29

  
30
/**
31
 * Exception thrown when an error occurs in a configuration administrative
32
 * class
33
 */
34
public class BaseException extends Exception {
35

  
36

  
37
	private static final long serialVersionUID = 1331212806605660813L;
38
	
39
	private static String _coreMessage;
40
	
41
	/**
42
	 * Create a new AccessException.
43
	 *
44
	 * @param message The error or warning message.
45
	 */
46
	public BaseException(String message) {
47
		super(message);
48
		_coreMessage = message;
49
	}
50
	
51
	/**
52
	 * Create a new BaseException.
53
	 *
54
	 * @param message The error or warning message.
55
	 */
56
	public BaseException(String message, BaseException deeperException) {
57
		super(message + " --> " + deeperException.getMessage());
58
		_coreMessage = deeperException.getCoreMessage();
59
	}
60
	
61
	public String getCoreMessage() {
62
		return _coreMessage;
63
	}
64
	
65
	public void setCoreMessage(String coreMessage) {
66
		_coreMessage = coreMessage;
67
	}
68
	
69

  
70

  
71
}
0 72

  
src/edu/ucsb/nceas/shared/BaseAccess.java
27 27

  
28 28
package edu.ucsb.nceas.shared;
29 29

  
30
import java.sql.PreparedStatement;
31 30
import java.sql.ResultSet;
32 31
import java.sql.SQLException;
33 32

  
34 33
import edu.ucsb.nceas.metacat.DBConnection;
35 34
import edu.ucsb.nceas.metacat.DBConnectionPool;
36 35

  
37
public class BaseAccess {
36
public abstract class BaseAccess {
38 37
	
39 38
	protected DBConnection conn = null;
40 39
    protected int serialNumber = -1;
41
    protected PreparedStatement pstmt = null;
42 40
    protected ResultSet rs = null;
43 41
	
44 42
    protected BaseAccess(String methodName) throws AccessException {
......
53 51
    	}
54 52
    }
55 53
    
56
    protected ResultSet select(PreparedStatement pstmt) throws AccessException {
57
    	try {
58
    		pstmt.execute();
59
        	rs = pstmt.getResultSet();
60
    	} catch (SQLException sqle) {
61
    		throw new AccessException("Could not perform select: " + pstmt.toString()
62
    				+ sqle.getMessage());
63
    	}
64
    	
65
    	return rs;
66
    }
54
    protected abstract BaseDAO populateDAO(ResultSet resultSet) throws SQLException ;
67 55
}
src/edu/ucsb/nceas/metacat/ArchiveHandler.java
36 36

  
37 37
import edu.ucsb.nceas.metacat.service.PropertyService;
38 38
import edu.ucsb.nceas.metacat.util.ErrorSendingErrorException;
39
import edu.ucsb.nceas.metacat.util.ErrorUtil;
40 39
import edu.ucsb.nceas.metacat.util.MetacatUtilException;
41 40
import edu.ucsb.nceas.metacat.util.ResponseUtil;
42 41
import edu.ucsb.nceas.utilities.FileUtil;
......
108 107
            	if (!DocumentImpl.hasReadPermission(user, groups, docid)) {
109 108
            		String errorString = "User " + user + " does not have permission"
110 109
    					+ " to read the document with the docid " + docid;
111
            		ErrorUtil.sendError(response, ErrorUtil.NO_READ_PERMISSION, errorString);
110
            		ResponseUtil.sendErrorXML(response, ResponseUtil.NO_READ_PERMISSION, errorString);
112 111
            		return;
113 112
            	}
114 113
            } catch (McdbException mcdbe) {
......
151 150
        } catch (UtilException ue) {
152 151
        	String errorString = "Utility error reading archive entry for docid: " 
153 152
    			+ docid + " : " + ue.getMessage();
154
        	ErrorUtil.sendError(response, ErrorUtil.GENERAL_UTILITY_ERROR, errorString);
153
        	ResponseUtil.sendErrorXML(response, ResponseUtil.GENERAL_UTILITY_ERROR, errorString);
155 154
        	throw new ErrorHandledException(null);
156 155
        } catch (PropertyNotFoundException pnfe) {
157 156
        	String errorString = "Property error reading archive entry for docid: " 
158 157
        		+ docid + " : " + pnfe.getMessage();
159
        	ErrorUtil.sendError(response, ErrorUtil.PROPERTY_NOT_FOUND, errorString);
158
        	ResponseUtil.sendErrorXML(response, ResponseUtil.PROPERTY_NOT_FOUND, errorString);
160 159
        	throw new ErrorHandledException(null);
161 160
        } catch (MetacatUtilException mue) {
162 161
        	String errorString = "Metacat utility error reading archive entry for docid: " 
163 162
        		+ docid + " : " + mue.getMessage();
164
        	ErrorUtil.sendError(response, ErrorUtil.METACAT_UTILITY_ERROR, errorString);
163
        	ResponseUtil.sendErrorXML(response, ResponseUtil.METACAT_UTILITY_ERROR, errorString);
165 164
        	throw new ErrorHandledException(null);
166 165
        } 
167 166
	}
src/edu/ucsb/nceas/metacat/MetaCatServlet.java
90 90
import edu.ucsb.nceas.metacat.util.MetacatUtil;
91 91
import edu.ucsb.nceas.metacat.util.MetacatUtilException;
92 92
import edu.ucsb.nceas.metacat.util.RequestUtil;
93
import edu.ucsb.nceas.metacat.util.ResponseUtil;
93 94
import edu.ucsb.nceas.metacat.util.SystemUtil;
94 95
import edu.ucsb.nceas.metacat.util.SessionData;
96
import edu.ucsb.nceas.metacat.workflow.WorkflowScheduler;
97
import edu.ucsb.nceas.shared.BaseException;
95 98
import edu.ucsb.nceas.utilities.FileUtil;
96 99
import edu.ucsb.nceas.utilities.GeneralPropertyException;
97 100
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
......
154 157
	private static final long serialVersionUID = 1L;
155 158
	private Timer timer = null;
156 159
    private static boolean sitemapScheduled;
160
    private static boolean firstHalfInitialized = false;
157 161
    private static boolean fullyInitialized = false;
158 162
    
159 163
    // Constants -- these should be final in a servlet
......
175 179
    public void init(ServletConfig config) throws ServletException {
176 180
    	Logger logMetacat = Logger.getLogger(MetaCatServlet.class);
177 181
    	try {
182
    		if(firstHalfInitialized) {
183
    			return;
184
    		}
185
    		
178 186
            super.init(config);
179 187
            
180 188
            ServletContext context = config.getServletContext();
......
200 208
            	return;
201 209
            }  
202 210
            
211
            firstHalfInitialized = true;
212
            
203 213
            initSecondHalf(context);
204 214
            
205 215
    	} catch (ServiceException se) {
......
865 875
					// MetacatAdminServlet
866 876
					ServiceService.refreshService("XMLSchemaService");
867 877
					return;
868
				} else if (action.equals("schedule")) {
869
					SchedulerService.getInstance().scheduleHandler(params, request, response, 
870
							userName, groupNames);
871
					return;
878
				} else if (action.equals("scheduleWorkflow")) {
879
					try {
880
						String resultString = 
881
							WorkflowScheduler.getInstance().scheduleJob(params, request, response, 
882
								userName, groupNames);
883
						ResponseUtil.sendSuccessXML(response, resultString);
884
						return;
885
					} catch (BaseException be) {
886
						ResponseUtil.sendErrorXML(response, ResponseUtil.SCHEDULE_WORKFLOW_ERROR, be);
887
						return;						
888
					}
889
				} else if (action.equals("getScheduledWorkflow")) {
890
					try {
891
						String resultString = 
892
							WorkflowScheduler.getInstance().getJobs(params, request, response, 
893
								userName, groupNames);
894
						ResponseUtil.sendSuccessXML(response, resultString);
895
						return;
896
					} catch (BaseException be) {
897
						ResponseUtil.sendErrorXML(response, ResponseUtil.SCHEDULE_WORKFLOW_ERROR, be);
898
						return;						
899
					}
872 900
				} else {
873 901
					PrintWriter out = response.getWriter();
874 902
					out.println("<?xml version=\"1.0\"?>");
875 903
					out.println("<error>");
876
					out
877
							.println("Error: action not registered.  Please report this error.");
904
					out.println("Error: action not registered.  Please report this error.");
878 905
					out.println("</error>");
879 906
					out.close();
880 907
				}
......
909 936
		} catch (ErrorHandledException ehe) {
910 937
			// Nothing to do here.  We assume if we get here, the error has been 
911 938
			// written to ouput.  Continue on and let it display.
912
		}
939
		} 
913 940
	}
914 941
    
915 942
	
src/edu/ucsb/nceas/metacat/service/ServiceException.java
27 27

  
28 28
package edu.ucsb.nceas.metacat.service;
29 29

  
30
import edu.ucsb.nceas.shared.BaseException;
31

  
30 32
/**
31 33
 * Exception thrown when an error occurs in a configuration administrative
32 34
 * class
33 35
 */
34
public class ServiceException extends Exception {
36
public class ServiceException extends BaseException {
35 37
	
36 38
	private static final long serialVersionUID = -2982801310798091071L;
37 39

  
......
43 45
	public ServiceException(String message) {
44 46
		super(message);
45 47
	}
48
	
49
	public ServiceException(String message, BaseException deeperException) {
50
		super(message, deeperException);
51
	}
46 52
}
src/edu/ucsb/nceas/metacat/workflow/WorkflowJob.java
25 25
 */
26 26
package edu.ucsb.nceas.metacat.workflow;
27 27

  
28
import java.util.Calendar;
29

  
30
import org.apache.log4j.Logger;
31

  
32
import org.quartz.InterruptableJob;
28 33
import org.quartz.JobExecutionContext;
29 34
import org.quartz.JobExecutionException;
30 35

  
31
import edu.ucsb.nceas.metacat.scheduler.ScheduledJobInterface;
36
import edu.ucsb.nceas.utilities.FileUtil;
37
import edu.ucsb.nceas.utilities.UtilException;
32 38

  
33
public class WorkflowJob implements ScheduledJobInterface {
39
public class WorkflowJob implements InterruptableJob {
34 40
	
41
	private static Logger logMetacat = Logger.getLogger(WorkflowJob.class);
42
	
35 43
	public void execute(JobExecutionContext context) throws JobExecutionException {
44
		try {
45
			FileUtil.createFile("/tmp/worflowJob.txt");
46
			FileUtil.writeFile("/tmp/worflowJob.txt", Calendar.getInstance().getTime().toString());			
47
		} catch (UtilException ue) {
48
			String errorstr = "Util Exception writing to file" + ue.getMessage();
49
			logMetacat.error(errorstr);
50
			throw new JobExecutionException(errorstr);
51
		}
52
	
36 53
	}
37 54
	
38 55
	public void interrupt() {
src/edu/ucsb/nceas/metacat/workflow/WorkflowScheduler.java
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
}
0 154

  
src/edu/ucsb/nceas/metacat/util/ResponseUtil.java
31 31
import java.io.IOException;
32 32
import java.io.InputStream;
33 33
import java.io.OutputStream;
34
import java.io.PrintWriter;
35
import java.util.Hashtable;
34 36

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

  
38 40
import org.apache.log4j.Logger;
39 41

  
42
import edu.ucsb.nceas.shared.BaseException;
40 43
import edu.ucsb.nceas.utilities.FileUtil;
41 44

  
42 45
public class ResponseUtil {
43 46
	
47
	// 101XXX - general errors
48
	public static Long GENERAL_UTILITY_ERROR = new Long(101001);	
49
	public static Long METACAT_UTILITY_ERROR = new Long(101002);
50
	// 104XXX - property errors
51
	public static Long PROPERTY_NOT_FOUND = new Long(104001);	
52
	// 105XXX - permission errors
53
	public static Long NO_READ_PERMISSION = new Long(105001);
54
	// 130XXX - scheduler errors
55
	public static Long SCHEDULE_WORKFLOW_ERROR = new Long(130001);
56
	
57
	// errorCodes is a lookup table for generic messages for each code.  It
58
	// is better to use the sendError() versions that accept an explicit 
59
	// error message;
60
	static Hashtable<Long, String> errorCodes = new Hashtable<Long, String>();
61
	static {
62
		errorCodes.put(GENERAL_UTILITY_ERROR, "General utility error");
63
		errorCodes.put(METACAT_UTILITY_ERROR, "Metacat utility error");
64
		errorCodes.put(PROPERTY_NOT_FOUND, "Property not found");
65
		errorCodes.put(NO_READ_PERMISSION, "Read permission denied for user");
66
		errorCodes.put(SCHEDULE_WORKFLOW_ERROR, "Schedule workflow error");
67
	}
68
	
44 69
	private static Logger logMetacat = Logger.getLogger(ResponseUtil.class);
45 70
	
46 71
	private static int DEFAULT_BUFFER_SIZE = 4 * 1024; // 4K buffer
......
103 128
					+ "  to output");
104 129
		}
105 130
	}
131
	
132
	public static void sendErrorXML(HttpServletResponse response, Long errorCode,
133
			BaseException be) throws ErrorSendingErrorException {
134

  
135
		logMetacat.error(errorCodes.get(errorCode) + " : " + be.getMessage());
136

  
137
		PrintWriter out = null;
138
		try {
139
			out = response.getWriter();
140
			response.setContentType("text/xml");
141

  
142
			out.println("<?xml version=\"1.0\"?>");
143
			out.println("<error>");
144
			out.println("<code>" + errorCode + "</code>");
145
			out.println("<defaultMessage>" + errorCodes.get(errorCode) + "</defaultMessage>");
146
			out.println("<coreMessage>" + be.getCoreMessage() + "</coreMessage>");
147
			out.println("<chainedMessage>" + be.getMessage() + "</chainedMessage>");
148
			out.println("</error>");
149

  
150
		} catch (IOException ioe) {
151
			throw new ErrorSendingErrorException("I/O error when returning error: "
152
					+ errorCode + " : " + ioe.getMessage());
153
		} finally {
154
			if (out != null) {
155
				out.close();
156
			}
157
		}
158
	}
159
	
160
	public static void sendErrorXML(HttpServletResponse response, Long errorCode,
161
			String message) throws ErrorSendingErrorException {
162

  
163
		logMetacat.error(errorCodes.get(errorCode) + " : " + message);
164

  
165
		PrintWriter out = null;
166
		try {
167
			out = response.getWriter();
168
			response.setContentType("text/xml");
169

  
170
			out.println("<?xml version=\"1.0\"?>");
171
			out.println("<error>");
172
			out.println("<code>" + errorCode + "</code>");
173
			out.println("<defaultMessage>" + errorCodes.get(errorCode) + "</defaultMessage>");
174
			out.println("<coreMessage>" + message + "</coreMessage>");
175
			out.println("<chainedMessage></chainedMessage>");
176
			out.println("</error>");
177

  
178
		} catch (IOException ioe) {
179
			throw new ErrorSendingErrorException("I/O error when returning error: "
180
					+ errorCode + " : " + ioe.getMessage());
181
		} finally {
182
			if (out != null) {
183
				out.close();
184
			}
185
		}
186
	}
187
	
188
	public static void sendSuccessXML(HttpServletResponse response, String message) 
189
		throws ErrorSendingErrorException {
190

  
191
		PrintWriter out = null;
192
		try {
193
			out = response.getWriter();
194
			response.setContentType("text/xml");
195

  
196
			out.println("<?xml version=\"1.0\"?>");
197
			out.println("<success>" + message + "</success>");
198
		} catch (IOException ioe) {
199
			throw new ErrorSendingErrorException("I/O error when returning success XML: "
200
					+ message + " : " + ioe.getMessage());
201
		} finally {
202
			if (out != null) {
203
				out.close();
204
			}
205
		}
206
	}
106 207
}
src/edu/ucsb/nceas/metacat/scheduler/ScheduledJobDAO.java
28 28
package edu.ucsb.nceas.metacat.scheduler;
29 29

  
30 30
import java.sql.Timestamp;
31
import java.util.HashMap;
31 32

  
32 33
import edu.ucsb.nceas.shared.BaseDAO;
33 34

  
......
45 46
	private String _groupName;
46 47
	private String _className;
47 48
	private Timestamp _startTime;
48
	private int _interval;
49
	private int _intervalValue;
50
	private String _intervalUnit;
51
	private HashMap<String, ScheduledJobParamDAO> _jobParams = new HashMap<String, ScheduledJobParamDAO>();
49 52
	
50 53
	public String getName() {
51 54
		return _name;
......
87 90
		_startTime = startTime;
88 91
	}
89 92
	
90
	public int getInterval() {
91
		return _interval;
93
	public int getIntervalValue() {
94
		return _intervalValue;
92 95
	}
93 96
	
94
	public void setInterval(int interval) {
95
		_interval = interval;
97
	public void setIntervalValue(int intervalValue) {
98
		_intervalValue = intervalValue;
96 99
	}
100
	
101
	public String getIntervalUnit() {
102
		return _intervalUnit;
103
	}
104
	
105
	public void setIntervalUnit(String intervalUnit) {
106
		_intervalUnit = intervalUnit;
107
	}
108
	
109
	public ScheduledJobParamDAO getJobParam(String key) {
110
		return _jobParams.get(key);
111
	}
112
	
113
	public HashMap<String, ScheduledJobParamDAO> getAllJobParams() {
114
		return _jobParams;
115
	}
116
	
117
	public void addJobParam(ScheduledJobParamDAO jobParamDAO) {
118
		_jobParams.put(jobParamDAO.getKey(), jobParamDAO);
119
	}
97 120
}
src/edu/ucsb/nceas/metacat/scheduler/SchedulerService.java
26 26

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

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

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

  
41 34
import org.apache.log4j.Logger;
42 35

  
43 36
import org.quartz.Job;
37
import org.quartz.JobDataMap;
44 38
import org.quartz.JobDetail;
45 39
import org.quartz.Scheduler;
46 40
import org.quartz.SchedulerException;
......
51 45
import edu.ucsb.nceas.metacat.service.BaseService;
52 46
import edu.ucsb.nceas.metacat.service.ServiceException;
53 47
import edu.ucsb.nceas.shared.AccessException;
48
import edu.ucsb.nceas.utilities.StatusUtil;
54 49

  
55 50
public class SchedulerService extends BaseService {
56 51
	
......
59 54
	private static Logger logMetacat = Logger.getLogger(SchedulerService.class);
60 55
	
61 56
	private static Scheduler sched = null;
62
	
63
	static Hashtable<String, String> jobClassNames = new Hashtable<String, String>();
64
	static {
65
		jobClassNames.put("workflow", "edu.ucsb.nceas.metacat.workflow.WorkflowJob");
66
	}
67 57

  
68 58
	/**
69 59
	 * private constructor since this is a singleton
70 60
	 */
71
	private SchedulerService() throws ServiceException {}
61
	private SchedulerService() throws ServiceException {
62
		start();
63
	}
72 64
	
73 65
	/**
74 66
	 * Get the single instance of SchedulerService.
......
116 108
		return new Vector<String>();
117 109
	}
118 110
	
119
	public String scheduleHandler(Hashtable<String, String[]> params,
120
            HttpServletRequest request, HttpServletResponse response,
121
            String username, String[] groups) throws ServiceException {
111
	public String scheduleJob(String jobName, Calendar startCal, String interval,
112
			String jobClassName, String jobGroup, HashMap<String, String> jobParams) throws ServiceException {
122 113
        
123
		String returnString = "";
124
        String[] categories = params.get("category");
125
        if (categories == null || categories.length == 0) {
126
        	throw new ServiceException("SchedulerService.scheduleHandler - Category field " 
127
        			+ "must be populated in scheduler parameters");
128
        }
129
        
130
        String category = categories[0];
131
        
132
        if (category.equals("schedule")) {
133
    		Calendar startCal = null;
134
            Hashtable<String, String> jobParams = new Hashtable<String, String>();
135
        	
136
    		String jobTypes[] = params.get("type");
137
            if (jobTypes == null || jobTypes.length == 0) {
138
            	throw new ServiceException("SchedulerService.scheduleJob - Type field must be populated " 
139
            			+ "in scheduler parameters when scheduling job.");
140
            }
141
    		
142
            String delays[] = params.get("delay");
143
    		String startTimes[] = params.get("starttime");       
144
            try {
145
    			if (delays != null && delays.length > 0) {
146
    				startCal = getStartDateFromDelay(delays[0]);
147
    			} else if (startTimes != null && startTimes.length > 0) {
148
    				Date startDate = DateFormat.getInstance().parse(startTimes[0]);
149
    				startCal = Calendar.getInstance();
150
    				startCal.setTime(startDate);
151
    			} else {
152
    				throw new ServiceException("SchedulerService.scheduleJob -  Either delay or starttime  " 
153
    						+ "field must be populated in scheduler parameters when scheduling job.");
154
    			}
155
    		} catch (ParseException pe) {
156
    			throw new ServiceException("SchedulerService.scheduleJob - Could not parse start time " 
157
    					+ startTimes[0] + " : " + pe.getMessage());
158
    		}
159
            
160
            String intervals[] = params.get("interval"); 
161
            if (intervals == null || intervals.length == 0) {
162
            	throw new ServiceException("SchedulerService.scheduleJob - Interval field must be populated " 
163
            			+ "in scheduler parameters when scheduling job.");
164
            } 
165
            String interval = intervals[0];
166
            
167
            Enumeration<String> paramNames = params.keys();
168
            while (paramNames.hasMoreElements()) {
169
            	String paramName = paramNames.nextElement();
170
            	if (paramName.startsWith("jobparam_")) {
171
            		jobParams.put(paramName.substring(8), params.get(paramName)[0]);
172
            	}
173
            }
174
                   
175
            String jobType = jobTypes[0];
176
            String jobName = jobType + Calendar.getInstance().getTimeInMillis();
177
        	
178
        	returnString = scheduleJob(jobName, startCal, interval, jobType, jobParams);
179
        } else if (category.equals("unschedule")) {
180
        	String jobNames[] = params.get("jobName");
181
        	if (jobNames == null || jobNames.length == 0) {
182
        		throw new ServiceException("SchedulerService.scheduleJob - Job ID field must be populated " 
183
            			+ "in scheduler parameters when unscheduling job.");
184
        	}
185
        	        	
186
        	unscheduleJob(jobNames[0]);
187
        } else if (category.equals("delete")) {
188
//        	deleteJob(params);
189
        }
190
        
191
        return returnString;
192
	}
193
	
194
	public String scheduleJob(String jobName, Calendar startCal, String interval,
195
			String jobType, Hashtable<String, String> jobParams) throws ServiceException {
196

  
197 114
        Class<Job> jobClass = null;
198
       
199
        String className = jobClassNames.get(jobType);
200
        if (className == null) {
201
        	throw new ServiceException("SchedulerService.scheduleJob - Type: " + jobType 
202
        			+ " is not registered as a valid job type");
203
        }
204
        
205
        logMetacat.info("Scheduling job -- name: " + jobName + ", class: " + className 
206
        		+ ", start time: " + startCal.toString() + ", interval: " + interval);
207
       	 
208 115
        try {        	
209
        	jobClass = (Class<Job>)Class.forName(jobType + "Job");     	
116
        	jobClass = (Class<Job>)Class.forName(jobClassName);     	
210 117
        } catch (ClassNotFoundException cnfe) {
211 118
        	throw new ServiceException("SchedulerService.scheduleJob - Could not find class with name: " 
212
        			+ jobType + "Job");
213
        }    
119
        			+ jobClassName + " : " + cnfe.getMessage());
120
        } 
214 121
        
215
		char intervalUnit = interval.trim().charAt(interval.length());
122
        logMetacat.info("Scheduling job -- name: " + jobName + ", class: " + jobClassName 
123
        		+ ", start time: " + startCal.toString() + ", interval: " + interval);  
124
        
125
		char intervalUnit = interval.trim().charAt(interval.length() - 1);
216 126
		String intervalStrValue = interval.trim().substring(0, interval.length() - 1);
217 127
		int intervalValue;
218 128
		try {
219 129
			intervalValue = Integer.parseInt(intervalStrValue);
220 130
		} catch (NumberFormatException nfe) {
221 131
			throw new ServiceException("SchedulerService.scheduleJob - Could not parse interval value " 
222
					+ "into an integer: " + intervalStrValue);
132
					+ "into an integer: " + intervalStrValue + " : " + nfe.getMessage());
223 133
		}
224 134
		
225
		switch (intervalUnit) {
226
		case 's':
227
		case 'S':
228
			scheduleSecondlyJob(jobName, jobClass, startCal, intervalValue, jobType);
229
			break;
230
		case 'm':
231
		case 'M':
232
			scheduleMinutelyJob(jobName, jobClass, startCal, intervalValue, jobType);
233
			break;
234
		case 'h':
235
		case 'H':
236
			scheduleHourlyJob(jobName, jobClass, startCal, intervalValue, jobType);
237
			break;
238
		case 'd':
239
		case 'D':
240
			scheduleDailyJob(jobName, jobClass, startCal, intervalValue, jobType);
241
			break;
242
		default:
243
			throw new ServiceException("SchedulerService.scheduleJob - Could not interpret interval unit: " 
244
					+ intervalUnit + ". Unit must be s, m, h or d");	
245
		}	
135
		startJob(jobName, startCal, intervalValue, intervalUnit, jobClass, jobGroup, jobParams);
246 136
		
247 137
		try {
248 138
			ScheduledJobAccess jobAccess = new ScheduledJobAccess();
249
			jobAccess.createJob(jobName, jobClass, startCal, intervalValue, jobParams);
139
			jobAccess.createJob(jobName, jobName, jobGroup, jobClass, startCal, intervalValue, Character.toString(intervalUnit), jobParams);
250 140
		} catch (AccessException ae) {
251 141
			try {
252 142
				deleteJob(jobName);
253
			} catch (ServiceException se) {
143
			} catch (Exception e) {
254 144
				// Not much we can do here but log this
255 145
				logMetacat.error("An access exception was thrown when writing job: " + jobName 
256 146
						+ "to the db, and another exception was thrown when trying to remove the " 
257
						+ "job from the scheduler.  The db and scheduler may be out of sync");
147
						+ "job from the scheduler.  The db and scheduler may be out of sync: " + e.getMessage());
258 148
			}
259
			throw new ServiceException("SchedulerService.scheduleJob - Error accessing db: " 
260
					+ ae.getMessage());
149
			throw new ServiceException("SchedulerService.scheduleJob - Error accessing db: ", ae);
261 150
		}
262 151
		
263 152
		return jobName;
......
270 159

  
271 160
			sched.unscheduleJob(jobDAO.getName(), jobDAO.getGroupName());
272 161

  
273
			jobDAO.setStatus(ScheduledJobInterface.STATE_UNSHCEDULED);
162
			jobDAO.setStatus(StatusUtil.UNSHCEDULED);
274 163
			jobAccess.updateJobStatus(jobDAO);
275 164
		} catch (SchedulerException se) {
276 165
			throw new ServiceException("SchedulerService.unscheduleJob - Could not create "
277 166
							+ "scheduled job because of service issue: " + se.getMessage());
278 167
		} catch (AccessException ae) {
279 168
			throw new ServiceException("SchedulerService.unscheduleJob - Could not create "
280
							+ "scheduled job because of db access issue: " + ae.getMessage());
169
							+ "scheduled job because of db access issue: ", ae);
281 170
		}
282 171
	}
283 172
	
284 173
	public void deleteJob(String jobName) throws ServiceException {
285 174
		String groupName = "";
286
		
175

  
287 176
		try {
288 177
			ScheduledJobAccess jobAccess = new ScheduledJobAccess();
289 178
			ScheduledJobDAO jobDAO = jobAccess.getJobByName(jobName);
290 179
			groupName = jobDAO.getGroupName();
291
			
180

  
292 181
			sched.deleteJob(jobName, groupName);
293 182
		} catch (SchedulerException se) {
294
			throw new ServiceException("SchedulerService.deleteJob - Could not delete job: " 
295
					+ jobName + " for group: " + groupName + " : " + se.getMessage());
183
			throw new ServiceException("SchedulerService.deleteJob - Could not delete job: " + jobName
184
							+ " for group: " + groupName + " : " + se.getMessage());
296 185
		} catch (AccessException ae) {
297 186
			throw new ServiceException("SchedulerService.deleteJob - Could not delete "
298
					+ "scheduled job because of db access issue: " + ae.getMessage());
299
}
187
					+ "scheduled job because of db access issue: ", ae);
188
		}
300 189
	}
301 190
	
302 191
	public Vector<String> getJobInfo(String groupName) throws ServiceException {
......
319 208
		return returnVector;
320 209
	}
321 210
	
322
	private void scheduleSecondlyJob(String name, Class<Job> jobClass, Calendar startTime, int interval, String jobGroup) throws ServiceException {
323
		JobDetail jobDetail = new JobDetail(name, jobGroup, jobClass);
211
	public String getJobInfoXML(Long jobId) throws ServiceException {
212
		String jobInfoXML = "";
213
		
214
		try {
215
			ScheduledJobAccess jobAccess = new ScheduledJobAccess();
216
			ScheduledJobDAO scheduledJobDAO = jobAccess.getJob(jobId);
217
			
218
			jobInfoXML += 
219
				"<scheduledJobs>" + jobToXML(scheduledJobDAO) + "</scheduledJobs>";
220
			
221
		} catch (AccessException ae) {
222
			throw new ServiceException("SchedulerService.getJobInfoXML - Could not get job info for job: " 
223
					+ jobId, ae);
224
		}
225
		
226
		return jobInfoXML;
227
	}
228
	
229
	public String getJobsInfoXML(String groupName) throws ServiceException {
230
		String jobInfoXML = "";
231
		
232
		try {
233
			ScheduledJobAccess jobAccess = new ScheduledJobAccess();
234
			HashMap<Long, ScheduledJobDAO> JobDAOMap = jobAccess.getAllJobs(groupName);
235
			
236
			jobInfoXML += "<scheduledJobs>";
237
			for (Long jobDAOId : JobDAOMap.keySet()) {
238
				jobInfoXML +=  jobToXML(JobDAOMap.get(jobDAOId)); 
239
			}				
240
			jobInfoXML += "</scheduledJobs>";
241
			
242
		} catch (AccessException ae) {
243
			throw new ServiceException("SchedulerService.getJobInfoXML - Could not get jobs info for group: " 
244
					+ groupName, ae);
245
		}
246
		
247
		return jobInfoXML;
248
	}
249
	
250
	private String jobToXML(ScheduledJobDAO scheduledJobDAO) {
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff