001/*
002 *  Copyright 2016 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.plugins.core.impl.checker;
018
019import java.sql.Connection;
020import java.sql.DriverManager;
021import java.util.List;
022
023import org.apache.avalon.framework.logger.AbstractLogEnabled;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026
027import org.ametys.core.datasource.ConnectionHelper;
028import org.ametys.core.datasource.dbtype.SQLDatabaseTypeExtensionPoint;
029import org.ametys.runtime.parameter.ParameterChecker;
030import org.ametys.runtime.parameter.ParameterCheckerTestFailureException;
031
032/**
033 * Checks that a sql connection can be established with the provided values
034 */
035public class SQLConnectionChecker extends AbstractLogEnabled implements ParameterChecker, Serviceable
036{
037    private ServiceManager _manager;
038
039    public void service(ServiceManager manager)
040    {
041        _manager = manager;
042    }
043    
044    /**
045     * Check the sql connection info
046     * @param url The sql url
047     * @param login The db login
048     * @param password The db password
049     * @param manager The service manager
050     * @throws ParameterCheckerTestFailureException If an error occurred
051     */
052    public static void check(String url, String login, String password, ServiceManager manager) throws ParameterCheckerTestFailureException
053    {
054        Connection connection = null;
055        try 
056        {
057            manager.lookup(SQLDatabaseTypeExtensionPoint.ROLE);
058            connection = DriverManager.getConnection(url, login, password);
059        }
060        catch (Exception e)
061        {
062            throw new ParameterCheckerTestFailureException(e.getMessage(), e);
063        }
064        finally
065        {
066            if (connection != null)
067            {
068                try
069                {
070                    ConnectionHelper.cleanup(connection);
071                }
072                catch (Exception e)
073                {
074                    throw new ParameterCheckerTestFailureException(e.getMessage(), e);
075                }
076            }
077        }
078    }
079    
080    @Override
081    public void check(List<String> values) throws ParameterCheckerTestFailureException
082    {
083        String url = values.get(0);
084        String login = values.get(1);
085        String password = values.get(2);
086        
087        check(url, login, password, _manager);
088    }
089}