Package limpidlog.log

Source Code of limpidlog.log.DatabaseLog

//Copyright 2006-2007 Acelet Corporation. All rights reserved.

package limpidlog.log;

import java.net.*;
import java.sql.*;
import java.util.*;

import limpidlog.lib.Constants;
import limpidlog.lib.Options;

/**
* @author Wei Jiang
*/
public class DatabaseLog implements Constants, LogProxy {
  private static String hostname = "?";

  private static String databaseDriverName = "org.hsql.jdbcDriver";
  private static String databaseUrl = "jdbc:HypersonicSQL:hsql://localhost:23536";
  private static String databaseUserName = "acelet";
  private static String databasePassword = "acelet";
  private static int queryTimeout = 15;
  private static int messageMaxSize = 1024;
  private static Connection connection;

  private static PreparedStatement insertStatement;

  static {
    try {
      hostname = InetAddress.getLocalHost().getHostName();
    } catch (Throwable ex) {
      if (Options.debug)
        ex.printStackTrace();
    }
  }

  public DatabaseLog() throws Exception {
    Options options = new Options();
    databaseDriverName = options.databaseDriverName;
    databaseUrl = options.databaseUrl;
    databaseUserName = options.databaseUserName;
    databasePassword = options.databasePassword;
    queryTimeout = options.databaseQueryTimeout;
    messageMaxSize = options.messageMaxSize;

    connect();
  }

  protected void closeConnection() {
    if (connection == null)
      return;

    synchronized (connection) {
      if (insertStatement != null)
        try {
          insertStatement.close();
        } catch (Exception e1) {
          if (Options.debug)
            e1.printStackTrace();
        }
     
      if (connection != null)
        try {
          connection.close();
        } catch (Exception e2) {
          if (Options.debug)
            e2.printStackTrace();
        }
      connection = null;
    }
  }

  protected void connect() throws Exception {
    closeConnection();

    Class.forName(databaseDriverName);
    DriverManager.setLoginTimeout(queryTimeout);
    connection = DriverManager.getConnection(databaseUrl, databaseUserName, databasePassword);
    connection.setAutoCommit(true);

    String insertSql = "INSERT INTO logMessage " +
      " (timemark, hostname, threadName, whereabouts, why, msg) " +
      " VALUES (?,?,?,?,?,?)";
    insertStatement = connection.prepareStatement(insertSql);
    insertStatement.setQueryTimeout(queryTimeout);

    String threadName = Thread.currentThread().getName();
    log(threadName, "", Options.LIMPIDLOG_VERSION, dataVersion + "\t" + version);
    log(threadName, "", Options.LIMPIDLOG_OPTIONS, new Options().getBytecoderOptions());
  }

  protected void handleDatabaseError(SQLException ex) {
    closeConnection();
  }

  public void log(String threadName, String whereabouts, String why, Object message) {
    String msg = (message==null)? "<NULL>": message.toString();
    log(threadName, whereabouts, why, msg);
  }

  public void log(String threadName, String whereabouts, String why, String message) {
    if (connection == null)
      return;

    synchronized (connection) {
      long timemark = System.currentTimeMillis();
      message = (message==null)? NULL: message;
      if (message.length() > messageMaxSize)
        message = message.substring(0, messageMaxSize);

      try {
        int nn = 0;
        insertStatement.clearParameters();
        insertStatement.setLong(++nn, timemark);
        insertStatement.setString(++nn, hostname);
        insertStatement.setString(++nn, threadName);
        insertStatement.setString(++nn, whereabouts);
        insertStatement.setString(++nn, why);
        insertStatement.setString(++nn, message);
        insertStatement.executeUpdate();
      } catch (SQLException ex) {
        ex.printStackTrace();
        handleDatabaseError(ex);
      }
    }
  }
}

TOP

Related Classes of limpidlog.log.DatabaseLog

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.