Package com.acelet.s.scheduler

Source Code of com.acelet.s.scheduler.DirectTaskDatabaseConnection

/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */

package com.acelet.s.scheduler;

import java.io.*;
import java.sql.*;
import java.util.*;

import com.acelet.lib.*;
import com.acelet.s.*;
import com.acelet.s.database.DefaultDatabase;
import com.acelet.s.task.AccessTaskDatabase;

public class DirectTaskDatabaseConnection implements Runnable {
  public static String databaseDriverName = DefaultDatabase.databaseDriverName;
  public static String databaseUrl = DefaultDatabase.databaseUrl;
  public static String databaseUserName = DefaultDatabase.databaseUserName;
  public static String databasePassword = DefaultDatabase.databasePassword;
  public static int queryTimeout = DefaultDatabase.databaseQueryTimeout;
  public static int entitlement;

  protected static Exception exception;
  protected static Connection connection;

  static {
    try {
      readFromProperties();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public DirectTaskDatabaseConnection() {
  }

  public static Connection getConnection() throws Exception {
    if (connection == null) {
      connection = getNewConnection();
      entitlement = new AccessTaskDatabase(connection).getEntitlement(databaseUserName);
      ConnectionUtil.checkConnection(connection);
    }
    return connection;
  }

  public Exception getException() {
    return exception;
  }

  public static Connection getNewConnection() throws Exception {
    exception = null;

    try {
      Class.forName(databaseDriverName);
      DriverManager.setLoginTimeout(queryTimeout);
      Connection conn =
        DriverManager.getConnection(databaseUrl, databaseUserName, databasePassword);
      conn.setAutoCommit(true);

      String databaseInfo = "";
      try {
       
      } catch (Exception ex) {
      }
      System.out.println("SuperScheduler: database > " + databaseInfo + ": " + databaseDriverName);

      if (databaseDriverName.equals("org.hsql.jdbcDriver")) {
        SuperProperties.blobGetMethod = SuperProperties.BLOB_GET_BYTES;
        SuperProperties.blobSetMethod = SuperProperties.BLOB_SET_BYTES;
      }
      return conn;
    } catch (Exception ex) {
      throw new Exception(Phrase.get("ER_CONNECT_DATABASE") + ": " +
        Phrase.get("TX_DIRECT_TASK_DATABASE"), ex);
    } finally {
    }
  }

  public static Connection peekConnection() {
    return connection;
  }

  public static void readFromProperties() throws Exception {
    SchedulerProperties schedulerProperties = new SchedulerProperties();
    databaseDriverName =
      schedulerProperties.get(SchedulerProperties.TASK_DATABASE_DRIVER_NAME, databaseDriverName);
    databaseUrl =
      schedulerProperties.get(SchedulerProperties.TASK_DATABASE_URL, databaseUrl);
    databaseUserName =
      schedulerProperties.get(SchedulerProperties.TASK_DATABASE_USER_NAME, databaseUserName);
    databasePassword =
      schedulerProperties.get(SchedulerProperties.TASK_DATABASE_PASSWORD, databasePassword);
    queryTimeout =
      schedulerProperties.getInt(SchedulerProperties.TASK_DATABASE_QUERY_TIMEOUT, queryTimeout);
  }

  public void run() {
    try {
      connection = null;
      connection = getConnection();
    } catch (Exception ex) {
      exception = ex;
      return;
    }
    if (connection == null)
      exception = new Exception(Phrase.get("ER_CONNECT_DATABASE"));
  }

  public static Connection testConnection() {
    try {
      return getConnection();
    } catch (Exception ex) {
      return null;
    }
  }

  public static void writeToProperties(boolean rememberPassword) throws Exception {
    SchedulerProperties schedulerProperties = new SchedulerProperties();
    schedulerProperties.put(SchedulerProperties.TASK_DATABASE_DRIVER_NAME, databaseDriverName);
    schedulerProperties.put(SchedulerProperties.TASK_DATABASE_URL, databaseUrl);

    if (rememberPassword) {
      schedulerProperties.put(SchedulerProperties.TASK_DATABASE_USER_NAME, databaseUserName);
      schedulerProperties.put(SchedulerProperties.TASK_DATABASE_PASSWORD, databasePassword);
    } else {
      schedulerProperties.put(SchedulerProperties.TASK_DATABASE_USER_NAME, "");
      schedulerProperties.put(SchedulerProperties.TASK_DATABASE_PASSWORD, "");
    }
  }
}
TOP

Related Classes of com.acelet.s.scheduler.DirectTaskDatabaseConnection

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.