view geo-backend/src/main/java/de/intevation/gnv/geobackend/sde/connectionpool/ArcSDEPoolableObjectFactory.java @ 1127:ebeb56428409

Added license headers and license file. geo-backend/trunk@1261 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:52:22 +0000
parents b757def3ff55
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.geobackend.sde.connectionpool;

import de.intevation.gnv.geobackend.base.connectionpool.exception.ConnectionException;

import de.intevation.gnv.geobackend.sde.datasources.ArcSDEConnection;

import java.sql.Connection;
import java.sql.SQLException;

import java.util.Properties;

import org.apache.commons.pool.PoolableObjectFactory;

import org.apache.log4j.Logger;

/**
 * ArcSDE specific Implementation of an PoolableObjectFactory.
 * This factory instantiate Objects of type ArcSDEConnection.
 *
 * @author <a href="mailto:tim.englich@intevation.de">Tim Englich</a>
 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
 */
public class ArcSDEPoolableObjectFactory implements PoolableObjectFactory {

    /**
     * the logger, used to log exceptions and additonaly information
     */
    private static Logger log = Logger.getLogger(
        ArcSDEPoolableObjectFactory.class);

    /**
     *  The 5 seconds are inspired by GeoTools's testServer() usage.
     */
    private int serverRoundtripInterval = 5;
    /**
     * The value of the  maximum Time a Connection is allowed to
     * be inactive without the validation of the Connection.
     */
    private long serverInactiveInterval = 5L*60L*1000L; // 5 minutes
    /**
     * The URL to the ArcSDE Server
     */
    private String server = null;
    /**
     * The Port the ArcSDE Server is connected to.
     */
    private String port = null;
    /**
     * The Name of the Database
     */
    private String database = null;
    /**
     * The Username for the Authentication
     */
    private String username = null;
    /**
     * The Credentials which belongs to the User
     */
    private String credentials = null;

    /**
     * Constructor of this Class
     * @param properties the Properties which includes the ConnectionParams to the Database
     */
    public ArcSDEPoolableObjectFactory(Properties properties) {

        log.debug("ArcSDEPoolableObjectFactory.Constructor");

        server      = properties.getProperty("server");
        port        = properties.getProperty("port");
        database    = properties.getProperty("database");
        username    = properties.getProperty("username");
        credentials = properties.getProperty("credentials");

        String serverRoundtripIntervalValue =
            properties.getProperty("serverRoundtripInterval");
        String serverInactiveIntervalValue =
            properties.getProperty("serverInactiveInterval");

        try {
            if (serverRoundtripIntervalValue != null) {
                serverRoundtripInterval =
                    Integer.parseInt(serverRoundtripIntervalValue);
            }
        }
        catch (NumberFormatException e) {
            log.error(e,e);
        }

        try {
            if (serverInactiveIntervalValue != null) {
                serverInactiveInterval = 1000L * // input in seconds!
                    Long.parseLong(serverInactiveIntervalValue);
            }
        }
        catch (NumberFormatException e) {
            log.error(e,e);
        }

        log.info("ArcSDEPoolableObjectFactory initialized");
        log.info("Server: "                   + server);
        log.info("Port: "                     + port);
        log.info("Database: "                 + database);
        log.info("User: "                     + username);
        log.info("Roundtrip check interval: " + serverRoundtripInterval);
        log.info("Inactive  check interval: " + serverInactiveInterval);
    }

    /**
     * @see org.apache.commons.pool.PoolableObjectFactory#activateObject(java.lang.Object)
     */
    public void activateObject(Object arg0) throws Exception {
        log.debug("ArcSDEPoolableObjectFactory.activateObject");
    }

    /**
     * @see org.apache.commons.pool.PoolableObjectFactory#destroyObject(java.lang.Object)
     */
    public void destroyObject(Object arg0) throws Exception {
        log.debug("ArcSDEPoolableObjectFactory.destroyObjectb");
        if (arg0 instanceof ArcSDEConnection) {
            ((ArcSDEConnection)arg0).close();
        }else{
            log.warn("Object cannot be handled");
        }
    }

    /**
     * @see org.apache.commons.pool.PoolableObjectFactory#makeObject()
     */
    public Object makeObject() throws Exception {
        log.debug("ArcSDEPoolableObjectFactory.makeObject");
        Connection con;
        try {
            con = new ArcSDEConnection(
                server,
                port,
                database,
                username,
                credentials,
                serverRoundtripInterval,
                serverInactiveInterval);
        }
        catch (ConnectionException e) {
            throw new ConnectionException(
                "Establishing a connection to database failed: " +
                    e.toString(), e);
        }
        return con;
    }

    /**
     * @see org.apache.commons.pool.PoolableObjectFactory#passivateObject(java.lang.Object)
     */
    public void passivateObject(Object arg0) throws Exception {

        boolean debug = log.isDebugEnabled();

        if (debug) {
            log.debug("ArcSDEPoolableObjectFactory.passivateObject");
        }

        if (arg0 instanceof ArcSDEConnection) {
            if (debug) {
                log.debug("    touching connection");
            }
            ((ArcSDEConnection)arg0).touch();
        }
    }

    /**
     * @see org.apache.commons.pool.PoolableObjectFactory#validateObject(java.lang.Object)
     */
    public boolean validateObject(Object arg0) {

        boolean debug = log.isDebugEnabled();

        if (debug) {
            log.debug("ArcSDEPoolableObjectFactory.validateObject");
        }

        if (!(arg0 instanceof ArcSDEConnection)) {
            return false;
        }

        try {
            ArcSDEConnection con = (ArcSDEConnection)arg0;

            boolean isValid =
                con.isActive() && con.isValid(serverRoundtripInterval);

            if (!isValid && debug) {
                log.debug("connection is invalid!");
            }

            return isValid;
        }
        catch (SQLException sqle) {
            log.error(sqle, sqle);
        }

        return false;
    }
}

http://dive4elements.wald.intevation.org