/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */
package com.acelet.s.scheduler;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
import com.acelet.lib.Common;
import com.acelet.lib.Externals;
import com.acelet.lib.Kit;
import com.acelet.lib.LogAgent;
import com.acelet.lib.LoggingConstants;
import com.acelet.lib.NotSupportedException;
import com.acelet.lib.Phrase;
import com.acelet.lib.SendEmail;
import com.acelet.lib.SuperProperties;
import com.acelet.s.MailServerData;
import com.acelet.s.task.CandidateTask;
import com.acelet.s.task.DoerTalker;
import com.acelet.s.task.Holiday;
import com.acelet.s.task.HolidayRule;
import com.acelet.s.task.Task;
import com.acelet.s.task.TaskProcess;
import com.acelet.s.task.WorkingTask;
/**
* Copyright Acelet Corp. 2001 - 2006. All rights reserved
* <p>
* The <code>Delegate</code> is the inner API to SuperScheduler, which should not
* exposed to outside.
* <br>
* @see com.acelet.s.job.AbstractTaskConstants
* @see com.acelet.s.task.Task
* @see com.acelet.s.task.TaskConstants
* @see com.acelet.s.task.WorkingTask
*/
public class Delegate {
public static LogAgent logAgent;
static boolean initialized = false;
protected Delegate() throws Exception {
}
public static int changeTaskStatus(String taskName, int newStatus) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).changeTaskStatus(taskName, newStatus);
}
}
public static int deleteCandidateTask(long id) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).deleteCandidateTask(id);
}
}
public static int getNumberOfTasks() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).getNumberOfTasks();
}
}
public static int deleteOldWorkingTasks(long workingTaskRetireTime) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).deleteOldWorkingTasks(workingTaskRetireTime);
}
}
public static String getRefreshTime(String doerTalkerName) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).getRefreshTime(doerTalkerName);
}
}
public static void init() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
logAgent = new TaskProcess(connection).setLogAgent();
}
init2();
}
/**
* <code>init</code> init this Delegate. It must be called before Delegate can be used.
* <br>
*
* @param connection a database connection.
* @param logAgentName a LogAgent object to use for logging. It can be null if you do not
* want log.
* @exception Exception.
*/
public static void init(String logAgentName) throws Exception {
logAgent = TaskProcess.setLogAgent(logAgentName, Scheduling.logAgentTimeout);
init2();
}
public static void init2() throws Exception {
String isolationLevel = setIsolationLevel();
System.out.println("SuperScheduler: transactionIsolation=" + isolationLevel);
String existingSchema = selectSchemaVersion();
if (existingSchema.equals("?") ||
existingSchema.compareTo(TaskProcess.DATABASE_SCHEMA_VERSION) < 0) {
String error = Phrase.get("ER_DATABASE_SCHEMA_VERSION") + ": " + existingSchema + "\n" +
Phrase.get("TX_REQUIRED") + ": " + TaskProcess.DATABASE_SCHEMA_VERSION;
throw new Exception(error);
}
Scheduling.readPreference();
initialized = true;
}
/**
* <code>insertWorkingTask</code> insert the WorkingTask
* <br>
* @param workingTask the WorkingTask to be inserted.
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @see com.acelet.s.task.WorkingTask
*/
public static int insertWorkingTask(WorkingTask workingTask) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).insertWorkingTask(workingTask);
}
}
public static boolean isInitialized() {
return initialized;
}
public static void resetTaskProcess() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
TaskProcess.reset(connection);
}
}
public static Vector selectAllCandidateTasks() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectAllCandidateTasks();
}
}
public static Vector selectAllHolidays() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectAllHolidays();
}
}
public static Vector selectAllTasks() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectAllTasks();
}
}
public static Vector selectAllWorkingTasks(long from, long to, int rows) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectAllWorkingTasks(from, to, rows);
}
}
public static Properties selectSchedulerPreference() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectSchedulerPreference();
}
}
public static String selectSchemaVersion() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectSchemaVersion();
}
}
public static Properties selectSettings() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectSettings();
}
}
public static Task selectTask(long id) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectTask(id);
}
}
public static Task selectTask(String name) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).selectTask(name);
}
}
static void sendAlarmEmail(String alarmEmail, String subject, String msg) throws Exception {
msg += "\n" + Phrase.get("TX_HOST") + ": " + Common.hostname;
if (alarmEmail == null || alarmEmail.trim().length() == 0) {
System.out.println(Phrase.get("ER_EMAIL") + ": Subject=" + subject + "\n " + msg);
return;
}
SendEmail sendEmail = MailServerData.getSendEmailObject();
sendEmail.send(alarmEmail, sendEmail.getPostmasterEmailAddress(),
subject, "SuperScheduler: " + msg);
}
public static void sendAlarmEmailForExceedDuration(String alarmEmail, String taskName,
String msg) throws Exception {
String subject = "SuperScheduler: " + Task.TASK_EXCEEDED_DURATION + ": " + taskName;
sendAlarmEmail(alarmEmail, subject, msg);
}
public static void sendAlarmEmailForKillingOfExceedDuration(String alarmEmail, String taskName,
String msg) throws Exception {
String subject = "SuperScheduler: " + Task.TASK_KILLED + ": " + taskName;
sendAlarmEmail(alarmEmail, subject, msg);
}
public static void sendAlarmEmailForTaskError(String alarmEmail, String taskName, String msg)
throws Exception {
String subject = "SuperScheduler: " + Task.TASK_ERROR + ": " + taskName;
sendAlarmEmail(alarmEmail, subject, msg);
}
public static void sendAlarmEmailForTaskMissed(String alarmEmail, String taskName, String msg)
throws Exception {
String subject = "SuperScheduler: " + Task.TASK_MISSED + ": " + taskName;
sendAlarmEmail(alarmEmail, subject, msg);
}
public static void sendAlarmMessageForPatrol(String alarmEmail, String subject, String msg)
throws Exception {
String message = "SuperPatrol Alarm" + LoggingConstants.COMMON_DELIMITER + subject +
LoggingConstants.COMMON_DELIMITER + msg;
sendAlarmEmail(alarmEmail, subject, message);
}
public static void sendAlertEmail(String subject, String msg) throws Exception {
SendEmail sendEmail = MailServerData.getSendEmailObject();
sendEmail.sendAlert(subject, "SuperScheduler: " + msg);
}
public static String setIsolationLevel() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
Properties settingProperties = selectSettings();
String isolationLevel = settingProperties.getProperty("transactionIsolation");
try {
if (isolationLevel.equals("TRANSACTION_READ_COMMITTED"))
connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
else if (isolationLevel.equals("TRANSACTION_REPEATABLE_READ"))
connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
else if (isolationLevel.equals("TRANSACTION_SERIALIZABLE"))
connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
} catch (Exception e) {
e.printStackTrace();
}
int isolation = connection.getTransactionIsolation();
if (isolation == Connection.TRANSACTION_READ_COMMITTED)
isolationLevel = "TRANSACTION_READ_COMMITTED";
else if (isolation == Connection.TRANSACTION_REPEATABLE_READ)
isolationLevel = "TRANSACTION_REPEATABLE_READ";
else if (isolation == Connection.TRANSACTION_SERIALIZABLE)
isolationLevel = "TRANSACTION_SERIALIZABLE";
else
isolationLevel = isolation + "";
return isolationLevel;
}
public static int updateTask(Task task, long newModifiedAt) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).updateTask(task, newModifiedAt);
}
}
public static int updateTaskRuntimeInfo(Task task) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).updateTaskRuntimeInfo(task);
}
}
public static int updateWorkingTask(WorkingTask workingTask) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).updateWorkingTask(workingTask);
}
}
public static int updateWorkingTaskForExceedingDuration(WorkingTask workingTask)
throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new TaskProcess(connection).updateWorkingTaskForExceedingDuration(workingTask);
}
}
public static void writeLogMessageToDatabaseForScheduler(String text) throws Exception {
if (logAgent != null) {
logAgent.log(text);
}
}
}