001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (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 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.storage.sql;
021
022import java.sql.Connection;
023import java.sql.DriverManager;
024import java.sql.SQLException;
025import java.sql.Statement;
026import java.util.HashMap;
027import java.util.Map;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.runtime.api.Framework;
032
033/**
034 * @author Florent Guillaume
035 */
036public class DatabaseH2 extends DatabaseHelper {
037
038    public static final DatabaseHelper INSTANCE = new DatabaseH2();
039
040    private static final Log log = LogFactory.getLog(DatabaseH2.class);
041
042    /** This directory will be deleted and recreated. */
043    protected static final String DIRECTORY = "target";
044
045    protected static final String DEF_USER = "sa";
046
047    protected static final String DEF_PASSWORD = "";
048
049    protected static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-h2-contrib.xml";
050
051    protected static final String DRIVER = "org.h2.Driver";
052
053    protected static final String URL_FORMAT = "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1";
054
055    protected String url;
056
057    protected String user;
058
059    protected String password;
060
061    protected void setProperties() {
062        url = setProperty(URL_PROPERTY, String.format(URL_FORMAT, databaseName));
063
064        setProperty(DATABASE_PROPERTY, databaseName);
065        user = setProperty(USER_PROPERTY, DEF_USER);
066        password = setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
067        // for sql directory tests
068        setProperty(DRIVER_PROPERTY, DRIVER);
069    }
070
071    @Override
072    public void setUp() throws SQLException {
073        super.setUp();
074        try {
075            Class.forName(DRIVER);
076        } catch (ReflectiveOperationException e) {
077            throw new RuntimeException(e);
078        }
079        setProperties();
080        checkDatabaseLive();
081    }
082
083    protected void checkDatabaseLive() throws SQLException {
084        try (Connection connection = DriverManager.getConnection(url, Framework.getProperty(USER_PROPERTY, "sa"),
085                Framework.getProperty(PASSWORD_PROPERTY, null))) {
086            try (Statement st = connection.createStatement()) {
087                st.execute("SELECT 1");
088            }
089        }
090    }
091
092    protected String getId() {
093        return "nuxeo";
094    }
095
096    @Override
097    public void tearDown() throws SQLException {
098        if (owner == null) {
099            return;
100        }
101        try {
102            tearDownDatabase(url);
103        } finally {
104            super.tearDown();
105        }
106    }
107
108    protected void tearDownDatabase(String url) throws SQLException {
109        Connection connection = DriverManager.getConnection(url, user, password);
110        try {
111            Statement st = connection.createStatement();
112            try {
113                String sql = "SHUTDOWN";
114                log.trace(sql);
115                st.execute(sql);
116            } finally {
117                st.close();
118            }
119        } finally {
120            connection.close();
121        }
122    }
123
124    @Override
125    public String getDeploymentContrib() {
126        return CONTRIB_XML;
127    }
128
129    @Override
130    public RepositoryDescriptor getRepositoryDescriptor() {
131        RepositoryDescriptor descriptor = new RepositoryDescriptor();
132        descriptor.xaDataSourceName = "org.h2.jdbcx.JdbcDataSource";
133        Map<String, String> properties = new HashMap<String, String>();
134        properties.put("URL", url);
135        properties.put("User", Framework.getProperty(USER_PROPERTY));
136        properties.put("Password", Framework.getProperty(PASSWORD_PROPERTY));
137        descriptor.properties = properties;
138        return descriptor;
139    }
140
141    @Override
142    public boolean supportsClustering() {
143        return true;
144    }
145
146}