org.kawanfw.sql.tomcat.TomcatStarterUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.kawanfw.sql.tomcat.TomcatStarterUtil.java

Source

/*
 * This file is part of AceQL. 
 * AceQL: Remote JDBC access over HTTP.                                     
 * Copyright (C) 2015,  KawanSoft SAS
 * (http://www.kawansoft.com). All rights reserved.                                
 *                                                                               
 * AceQL is free software; you can redistribute it and/or                 
 * modify it under the terms of the GNU Lesser General Public                    
 * License as published by the Free Software Foundation; either                  
 * version 2.1 of the License, or (at your option) any later version.            
 *                                                                               
 * AceQL 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             
 * Lesser General Public License for more details.                               
 *                                                                               
 * You should have received a copy of the GNU Lesser General Public              
 * License along with this library; if not, write to the Free Software           
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *
 * Any modifications to this file must keep this entire header
 * intact.
 */
package org.kawanfw.sql.tomcat;

import static org.kawanfw.file.servlet.ServerFileManager.COMMONS_CONFIGURATOR_CLASS_NAME;
import static org.kawanfw.file.servlet.ServerFileManager.FILE_CONFIGURATOR_CLASS_NAME;
import static org.kawanfw.sql.servlet.ServerSqlManager.SQL_CONFIGURATOR_CLASS_NAME;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.DatagramSocket;
import java.net.ServerSocket;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.apache.tomcat.jdbc.pool.PoolProperties;
import org.kawanfw.commons.server.util.embed.TomcatModeStore;
import org.kawanfw.sql.api.server.SqlConfigurationException;
import org.kawanfw.sql.servlet.sql.DbVendorManager;
import org.kawanfw.sql.tomcat.util.LinkedProperties;
import org.kawanfw.sql.util.SqlTag;

/**
 * @author Nicolas de Pomereu
 *
 *         Utility classes called at Tomcat startup
 */
public class TomcatStarterUtil {

    /**
     * protected constructor
     */
    protected TomcatStarterUtil() {

    }

    /**
     * If the user has created a driverClassName property in the properties
     * file: we create a Tomcat JDBC Pool from the properties
     * 
     * @param properties
     *            properties extracted from the properties file
     * @param index
     *            the servlet index: "" for first servlet, then ".2", ".3", etc.
     * @throws SqlConfigurationException
     */
    public static void createAndStoreDataSource(Properties properties, String index)
            throws SqlConfigurationException {

        String driverClassName = properties.getProperty("driverClassName" + index);
        String serverSqlManagerServletName = properties.getProperty("serverSqlManagerServletName" + index);
        serverSqlManagerServletName = serverSqlManagerServletName.trim();

        if (driverClassName == null || driverClassName.isEmpty()) {
            System.out.println(SqlTag.SQL_PRODUCT_START + " WARNING: driverClassName" + index
                    + " property not found for servlet " + serverSqlManagerServletName + "! ");
            System.out.println(SqlTag.SQL_PRODUCT_START
                    + "          Connection management must be defined in CommonsConfigurator.getConnection()");
            return;
        }

        if (index == null) {
            throw new IllegalArgumentException("index can not be null");
        }

        if (!index.equals("") && !index.startsWith(".")) {
            throw new IllegalArgumentException("index must be empty or start with \".\"");
        }

        String url = properties.getProperty("url" + index);

        if ((url == null) || url.isEmpty()) {
            throw new SqlConfigurationException(
                    "the url" + index + " property is not set in properties file for driverClassName "
                            + driverClassName + ". " + SqlTag.PLEASE_CORRECT);
        }

        String username = properties.getProperty("username" + index);
        if ((username == null) || username.isEmpty()) {
            throw new SqlConfigurationException(
                    "the username" + index + " property is not set in properties file for driverClassName "
                            + driverClassName + ". " + SqlTag.PLEASE_CORRECT);
        }

        String password = properties.getProperty("password" + index);
        if ((password == null) || password.isEmpty()) {
            throw new SqlConfigurationException(
                    "the password" + index + " property is not set in properties file for driverClassName "
                            + driverClassName + ". " + SqlTag.PLEASE_CORRECT);
        }

        System.out.println(SqlTag.SQL_PRODUCT_START + " Setting Tomcat JDBC Pool attributes for "
                + serverSqlManagerServletName + " Servlet:");

        // OK! create and test the DataSource
        PoolPropertiesCreator poolPropertiesCreator = new PoolPropertiesCreator(properties, index);
        PoolProperties poolProperties = null;

        try {
            poolProperties = poolPropertiesCreator.create();
        } catch (Exception e) {
            throw new SqlConfigurationException(e.getMessage());
        }

        DataSource dataSource = new DataSource();
        dataSource.setPoolProperties(poolProperties);

        Connection connection = null;

        try {
            try {
                System.out.println(SqlTag.SQL_PRODUCT_START + " Testing DataSource.getConnection() for "
                        + serverSqlManagerServletName + " Servlet:");
                connection = dataSource.getConnection();

                // Connection connection2 = dataSource.getConnection();
                // System.out.println("dataSource.getActive()): " +
                // dataSource.getActive());
                // connection2.close();

                if (connection == null) {
                    throw new SqlConfigurationException(
                            "Connection is null. Please verify all the values in properties file.");
                }

                System.out.println(SqlTag.SQL_PRODUCT_START + "  -> Connection OK!");

            } catch (SQLException e) {
                throw new SqlConfigurationException(e.getMessage() + " " + e.getCause());
            }
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        TomcatModeStore.setDataSource(serverSqlManagerServletName, dataSource);

    }

    /**
     * Returns the index from the servlet name
     * 
     * @param properties
     *            Properties extracted from the server-sql.properties files
     * @param serverSqlManagerServletName
     *            The Server SQL servlet name that is defined in a property of
     *            properties
     * @return the index corresponding to the Server SQL servlet name
     * @throws IllegalArgumentException
     */
    public static String getIndexFromServletName(Properties properties, String serverSqlManagerServletName)
            throws IllegalArgumentException {

        if (properties == null) {
            throw new IllegalArgumentException("properties can not be null");
        }

        if (serverSqlManagerServletName == null) {
            throw new IllegalArgumentException("serverSqlManagerServletName can not be null");
        }

        serverSqlManagerServletName = serverSqlManagerServletName.trim();

        for (Enumeration<?> e = properties.propertyNames(); e.hasMoreElements();) {

            String propertyName = (String) e.nextElement();
            String propertyValue = properties.getProperty(propertyName);

            propertyValue = propertyValue.trim();

            if (propertyValue.equals(serverSqlManagerServletName)) {
                String index = null;
                propertyName = propertyName.trim();
                if (!propertyName.contains(".")) {
                    index = ""; // First Servlet, no index
                } else {
                    index = "." + StringUtils.substringAfterLast(propertyName, ".");
                }

                return index;
            }

        }

        throw new SqlConfigurationException(
                "Server SQL Manager servlet name not found in properties file: " + serverSqlManagerServletName);
    }

    /**
     * Returns the Properties extracted from a file.
     * 
     * @param file
     *            the file containinf the properties
     * @return the Properties extracted from the file
     * 
     * @throws IOException
     * @throws SqlConfigurationException
     */
    public static Properties getProperties(File file) throws IOException, SqlConfigurationException {

        if (file == null) {
            throw new IllegalArgumentException("file can not be null!");
        }

        if (!file.exists()) {
            throw new SqlConfigurationException("properties file not found: " + file);
        }

        // Get the properties with order of position in file:
        Set<String> linkedProperties = LinkedProperties.getLinkedPropertiesName(file);

        InputStream in = null;

        // Create the ordered properties
        Properties properties;
        try {
            in = new FileInputStream(file);
            properties = new LinkedProperties(linkedProperties);
            properties.load(in);
        } finally {
            IOUtils.closeQuietly(in);
        }
        return properties;
    }

    /**
     * Set the servlet parameters store with the values extracted from the
     * Properties.
     * 
     * @param properties
     *            Properties extractec from the server-sql.properties files
     * @param index
     *            the servlet index: "" for first servlet, then ".2", ".3", etc.
     * 
     * @throws IllegalArgumentException
     */
    public static void setInitParametersInStore(Properties properties, String index)
            throws IllegalArgumentException {

        if (properties == null) {
            throw new IllegalArgumentException("properties can not be null");
        }

        if (index == null) {
            throw new IllegalArgumentException("index can not be null");
        }

        if (!index.equals("") && !index.startsWith(".")) {
            throw new IllegalArgumentException("index must be empty or start with \".\"");
        }

        String serverSqlManagerServletName = properties.getProperty("serverSqlManagerServletName" + index);

        if (serverSqlManagerServletName == null || serverSqlManagerServletName.isEmpty()) {
            throw new SqlConfigurationException(
                    "serverSqlManagerServletName property is null. " + SqlTag.PLEASE_CORRECT);
        }

        serverSqlManagerServletName = serverSqlManagerServletName.trim();

        // Set the configurators to use
        String commonsConfiguratorClassName = TomcatStarterUtil
                .trimSafe(properties.getProperty(COMMONS_CONFIGURATOR_CLASS_NAME + index));
        String sqlConfiguratorClassName = TomcatStarterUtil
                .trimSafe(properties.getProperty(SQL_CONFIGURATOR_CLASS_NAME + index));
        String fileConfiguratorClassName = TomcatStarterUtil
                .trimSafe(properties.getProperty(FILE_CONFIGURATOR_CLASS_NAME + index));

        ServletParametersStore.setInitParameter(serverSqlManagerServletName,
                new InitParamNameValuePair(COMMONS_CONFIGURATOR_CLASS_NAME, commonsConfiguratorClassName));
        ServletParametersStore.setInitParameter(serverSqlManagerServletName,
                new InitParamNameValuePair(SQL_CONFIGURATOR_CLASS_NAME, sqlConfiguratorClassName));
        ServletParametersStore.setInitParameter(serverSqlManagerServletName,
                new InitParamNameValuePair(FILE_CONFIGURATOR_CLASS_NAME, fileConfiguratorClassName));

    }

    /**
     * Safely trim a String
     * 
     * @param s
     *            the String to trim
     * @return
     */
    public static String trimSafe(String s) {
        if (s != null) {
            s = s.trim();
        }

        return s;
    }

    /**
     * Checks to see if a specific port is available.
     * 
     * @param port
     *            the port to check for availability
     */
    public static boolean available(int port) {

        ServerSocket ss = null;
        DatagramSocket ds = null;
        try {
            ss = new ServerSocket(port);
            ss.setReuseAddress(true);
            ds = new DatagramSocket(port);
            ds.setReuseAddress(true);
            return true;
        } catch (IOException e) {
            // e.printStackTrace();
        } finally {
            if (ds != null) {
                ds.close();
            }

            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    /* should not be thrown */
                }
            }
        }

        return false;
    }

}