/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */
package com.acelet.s.watchdog;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.*;
import javax.naming.*;
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.chore.CandidateChore;
import com.acelet.s.chore.Chore;
import com.acelet.s.chore.ChoreProcess;
import com.acelet.s.chore.WatchdogDoerTalker;
import com.acelet.s.chore.WorkingChore;
import com.acelet.s.scheduler.DirectTaskDatabaseConnection;
/**
* The <code>Delegate</code> is a utility class for SuperWatchdog.
* <br>
*/
public class Delegate {
static LogAgent logAgent;
static boolean initialized = false;
static boolean isSuperLoggingAvailable = false;
protected Delegate() throws Exception {
}
public static int deleteCandidateChore(long id) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).deleteCandidateChore(id);
}
}
public static int deleteOldWorkingChores(long workingChoreRetireTime) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new
ChoreProcess(connection).deleteOldWorkingChores(workingChoreRetireTime);
}
}
public static int deleteChore(long id) throws Exception {
Connection connection = null;
try {
connection = DirectTaskDatabaseConnection.getNewConnection();
return new ChoreProcess(connection).deleteChore(id);
} finally {
if (connection != null)
connection.close();
}
}
public static int deleteWorkingChore(long id) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).deleteWorkingChore(id);
}
}
public static int getNumberOfChores() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).getNumberOfChores();
}
}
public static String getRefreshTime(String doerTalkerName) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).getRefreshTime(doerTalkerName);
}
}
public static void init() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
logAgent = new ChoreProcess(connection).setLogAgent();
}
init2();
}
public static void init(String logAgentName) throws Exception {
logAgent = ChoreProcess.setLogAgent(logAgentName, Watchdogging.logAgentTimeout);
init2();
}
public static void init2() throws Exception {
String isolationLevel = com.acelet.s.scheduler.Delegate.setIsolationLevel();
System.out.println("SuperWatchdog: transactionIsolation=" + isolationLevel);
String existingSchema = selectSchemaVersion();
if (existingSchema.equals("?") ||
existingSchema.compareTo(ChoreProcess.DATABASE_SCHEMA_VERSION) < 0) {
String error = Phrase.get("ER_DATABASE_SCHEMA_VERSION") + ": " + existingSchema + "\n" +
Phrase.get("TX_REQUIRED") + ": " + ChoreProcess.DATABASE_SCHEMA_VERSION;
throw new Exception(error);
}
Watchdogging.readPreference();
initialized = true;
}
public static boolean isInitialized() {
return initialized;
}
/**
* <code>insertWorkingChore</code> insert the WorkingChore
* <br>
* @param workingChore the WorkingChore to be inserted.
* @return the number of rows changed. It is 1, if successful, 0 otherwise.
* @see com.acelet.s.chore.WorkingChore
*/
public static int insertWorkingChore(WorkingChore workingChore) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).insertWorkingChore(workingChore);
}
}
public static void resetChoreProcess() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
ChoreProcess.reset(connection);
}
}
public static Vector selectAllCandidateChores() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).selectAllCandidateChores();
}
}
public static Vector selectAllChores() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).selectAllChores();
}
}
public static void sendAlertEmail(String subject, String msg) throws Exception {
SendEmail sendEmail = MailServerData.getSendEmailObject();
sendEmail.sendAlert(subject, "SuperWatchdog: " + msg);
}
public static Vector selectAllWorkingChores(long from, long to, int rows) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).selectAllWorkingChores(from, to, rows);
}
}
public static Chore selectChore(long id) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).selectChore(id);
}
}
public static Chore selectChore(String name) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).selectChore(name);
}
}
public static String selectSchemaVersion() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).selectSchemaVersion();
}
}
public static Properties selectSettings() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).selectSettings();
}
}
public static Properties selectWatchdogPreference() throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).selectWatchdogPreference();
}
}
protected 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, "SuperWatchdog: " + msg);
}
public static void sendAlarmEmailForChoreError(String alarmEmail, String choreName, String msg)
throws Exception {
String subject = "SuperWatchdog: " + Chore.TASK_ERROR + ": " + choreName;
sendAlarmEmail(alarmEmail, subject, msg);
}
public static void sendAlarmEmailForExceedDuration(String alarmEmail, String choreName,
String msg) throws Exception {
String subject = "SuperWatchdog: " + Chore.TASK_EXCEEDED_DURATION + ": " + choreName;
sendAlarmEmail(alarmEmail, subject, msg);
}
public static void sendAlarmEmailForKillingOfExceedDuration(String alarmEmail, String choreName,
String msg) throws Exception {
String subject = "SuperWatchdog: " + Chore.TASK_KILLED + ": " + choreName;
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 int updateChore(Chore chore, long newModifiedAt) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).updateChore(chore, newModifiedAt);
}
}
public static int updateChoreRuntimeInfo(Chore chore) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).updateChoreRuntimeInfo(chore);
}
}
public static int updateWorkingChore(WorkingChore workingChore) throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).updateWorkingChore(workingChore);
}
}
public static int updateWorkingChoreForExceedingDuration(WorkingChore workingChore)
throws Exception {
Connection connection = DirectTaskDatabaseConnection.getConnection();
synchronized (connection) {
return new ChoreProcess(connection).
updateWorkingChoreForExceedingDuration(workingChore);
}
}
public static void writeLogMessageToDatabaseForWatchdog(String text) throws Exception {
if (logAgent != null) {
logAgent.log(text);
}
}
}