001/*
002 * (C) Copyright 2017 Nuxeo (http://nuxeo.com/) and others.
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 * Contributors:
017 *     dmetzler
018 */
019package org.nuxeo.launcher.config.backingservices;
020
021import java.io.FileNotFoundException;
022import java.io.IOException;
023import java.sql.SQLException;
024import java.util.Arrays;
025import java.util.List;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.common.codec.CryptoProperties;
030import org.nuxeo.launcher.commons.DatabaseDriverException;
031import org.nuxeo.launcher.config.ConfigurationException;
032import org.nuxeo.launcher.config.ConfigurationGenerator;
033
034/**
035 * @since 9.2
036 */
037public class DBCheck implements BackingChecker {
038
039    private static final Log log = LogFactory.getLog(DBCheck.class);
040
041    public static final List<String> DB_EXCLUDE_CHECK_LIST = Arrays.asList("default", "none");
042
043    @Override
044    public boolean accepts(ConfigurationGenerator cg) {
045        return !DB_EXCLUDE_CHECK_LIST.contains(
046                cg.getUserConfig().getProperty(ConfigurationGenerator.PARAM_TEMPLATE_DBTYPE));
047
048    }
049
050    @Override
051    public void check(ConfigurationGenerator cg) throws ConfigurationException {
052        try {
053            checkDatabaseConnection(cg);
054        } catch (IOException e) {
055            throw new ConfigurationException(e);
056        } catch (DatabaseDriverException e) {
057            log.debug(e, e);
058            log.error(e.getMessage());
059            throw new ConfigurationException("Could not find database driver: " + e.getMessage());
060        } catch (SQLException e) {
061            log.debug(e, e);
062            log.error(e.getMessage());
063            throw new ConfigurationException("Failed to connect on database: " + e.getMessage());
064        }
065    }
066
067    /**
068     * Check driver availability and database connection
069     */
070    public void checkDatabaseConnection(ConfigurationGenerator cg)
071            throws FileNotFoundException, IOException, DatabaseDriverException, SQLException {
072        CryptoProperties config = cg.getUserConfig();
073        String databaseTemplate = config.getProperty(ConfigurationGenerator.PARAM_TEMPLATE_DBNAME);
074        String dbName = config.getProperty(ConfigurationGenerator.PARAM_DB_NAME);
075        String dbUser = config.getProperty(ConfigurationGenerator.PARAM_DB_USER);
076        String dbPassword = config.getProperty(ConfigurationGenerator.PARAM_DB_PWD);
077        String dbHost = config.getProperty(ConfigurationGenerator.PARAM_DB_HOST);
078        String dbPort = config.getProperty(ConfigurationGenerator.PARAM_DB_PORT);
079
080        cg.checkDatabaseConnection(databaseTemplate, dbName, dbUser, dbPassword, dbHost, dbPort);
081    }
082
083}