001/*
002 *  Copyright 2018 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 */
016package org.ametys.plugins.core.impl.datasource;
017
018import java.sql.DriverManager;
019import java.sql.SQLException;
020import java.util.Map;
021
022import org.ametys.core.datasource.AbstractDataSourceManager.DataSourceDefinition;
023import org.ametys.core.datasource.SQLDataSourceManager;
024
025/**
026 * Specifically handle Derby connections, allowing to actually shut the database down;
027 */
028public class DerbySQLDatabaseType extends StaticSQLDatabaseType
029{
030    private static final String __DERBY_SHUTDOWN_SUFFIX = ";shutdown=true";
031    
032    /**
033     * Shut down a derby database.<br>
034     * This method is static because often called during dispose() process.
035     * @param definition the corresponding {@link DataSourceDefinition}.
036     */
037    public static void shutDown(DataSourceDefinition definition)
038    {
039        Map<String, String> parameters = definition.getParameters();
040        String url = parameters.get(SQLDataSourceManager.PARAM_DATABASE_URL);
041        int i = url.indexOf(';');
042        url = i == -1 ? url + __DERBY_SHUTDOWN_SUFFIX : url.substring(0, i) + __DERBY_SHUTDOWN_SUFFIX;
043        
044        try
045        {
046            DriverManager.getConnection(url).close();
047        }
048        catch (SQLException e)
049        {
050            // Derby "normal" shutdown exception
051        }
052    }
053}