All Downloads are FREE. Search and download functionalities are using the official Maven repository.

weka.gui.sql.event.ConnectionEvent Maven / Gradle / Ivy

Go to download

The Waikato Environment for Knowledge Analysis (WEKA), a machine learning workbench. This version represents the developer version, the "bleeding edge" of development, you could say. New functionality gets added to this version.

There is a newer version: 3.9.6
Show newest version
/*
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see .
 */

/*
 * ConnectionEvent.java
 * Copyright (C) 2005-2012 University of Waikato, Hamilton, New Zealand
 *
 */

package weka.gui.sql.event;

import java.util.EventObject;

import weka.gui.sql.DbUtils;

/**
 * An event that is generated when a connection is established or dropped.
 *
 * @see         ConnectionListener
 * @author      FracPete (fracpete at waikato dot ac dot nz)
 * @version     $Revision: 8034 $
 */
public class ConnectionEvent
  extends EventObject {

  /** for serialization */
  private static final long serialVersionUID = 5420308930427835037L;
  
  /** it was a connect try */
  public final static int CONNECT = 0;

  /** it was a disconnect */
  public final static int DISCONNECT = 1;
  
  /** the type of event, CONNECT or DISCONNECT */
  protected int m_Type;
  
  /** the databaseutils instance reponsible for the connection */
  protected DbUtils m_DbUtils;

  /** a possible exception that occurred if not successful */
  protected Exception m_Exception;
  
  /**
   * constructs the event
   * @param source        the source that generated this event
   * @param type          whether CONNECT or DISCONNECT happened
   * @param utils         the DatabaseUtils isntance responsible for the
   *                      connection
   */
  public ConnectionEvent(Object source, int type, DbUtils utils) {
    this(source, type, utils, null);
  }
  
  /**
   * constructs the event
   * @param source        the source that generated this event
   * @param type          whether CONNECT or DISCONNECT happened
   * @param utils         the DatabaseUtils isntance responsible for the
   *                      connection
   * @param ex            a possible exception, if not successful
   */
  public ConnectionEvent(Object source, int type, DbUtils utils, Exception ex) {
    super(source);
    
    m_Type      = type;
    m_DbUtils   = utils;
    m_Exception = ex;
  }
  
  /**
   * returns the type of this event, CONNECT or DISCONNECT
   * @return          the type of this event
   * @see             #CONNECT
   * @see             #DISCONNECT
   */
  public int getType() {
    return m_Type;
  }
  
  /**
   * whether an exception happened and is stored
   * @return          whether an exception happened
   */
  public boolean failed() {
    return (getException() != null);
  }
  
  /**
   * returns whether the connection is still open.
   * @return        whether the connection is still open
   */
  public boolean isConnected() {
    return m_DbUtils.isConnected();
  }

  /**
   * returns the stored exception, if any (can be NULL)
   */
  public Exception getException() {
    return m_Exception;
  }
  
  /**
   * returns the DbUtils instance that is responsible for the
   * connect/disconnect.
   * @return        the responsible DbUtils instance
   */
  public DbUtils getDbUtils() {
    return m_DbUtils;
  }

  /**
   * returns the event in a string representation
   * @return        the event in a string representation
   */
  public String toString() {
    String        result;

    result  = super.toString();
    result  = result.substring(0, result.length() - 1);  // remove "]"
    result +=   ",url=" + m_DbUtils.getDatabaseURL() 
              + ",user=" + m_DbUtils.getUsername()
              + ",password=" + m_DbUtils.getPassword().replaceAll(".", "*")
              + ",connected=" + isConnected() 
              + ",exception=" + getException()
              + "]";

    return result;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy