001/*
002 * (C) Copyright 2006-2013 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.ResultSet;
025import java.sql.SQLException;
026import java.sql.Statement;
027import java.util.ArrayList;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.nuxeo.runtime.api.Framework;
035
036public class DatabaseDB2 extends DatabaseHelper {
037
038    private static final Log log = LogFactory.getLog(DatabaseDB2.class);
039
040    public static DatabaseHelper INSTANCE = new DatabaseDB2();
041
042    private static final String DEF_SERVER = "localhost";
043
044    private static final String DEF_PORT = "3700";
045
046    // 8 chars max
047    private static final String DEFAULT_DATABASE_NAME = "nuxeotst";
048
049    private static final String DEF_USER = "db2inst1";
050
051    private static final String DEF_PASSWORD = "db2inst1pw99";
052
053    private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-db2-contrib.xml";
054
055    private static final String DRIVER = "com.ibm.db2.jcc.DB2Driver";
056
057    private static final String XA_DATASOURCE = "com.ibm.db2.jcc.DB2XADataSource";
058
059    protected void setProperties() {
060        databaseName = DEFAULT_DATABASE_NAME;
061        setProperty(DATABASE_PROPERTY, databaseName);
062        setProperty(SERVER_PROPERTY, DEF_SERVER);
063        setProperty(PORT_PROPERTY, DEF_PORT);
064        setProperty(USER_PROPERTY, DEF_USER);
065        setProperty(PASSWORD_PROPERTY, DEF_PASSWORD);
066        // for sql directory tests
067        setProperty(DRIVER_PROPERTY, DRIVER);
068        String url = String.format("jdbc:db2://%s:%s/%s", Framework.getProperty(SERVER_PROPERTY),
069                Framework.getProperty(PORT_PROPERTY), Framework.getProperty(DATABASE_PROPERTY));
070        setProperty(URL_PROPERTY, url);
071    }
072
073    @Override
074    public void setUp() throws SQLException {
075        super.setUp();
076        try {
077            Class.forName(DRIVER);
078        } catch (ReflectiveOperationException e) {
079            throw new RuntimeException(e);
080        }
081        setProperties();
082        Connection connection = DriverManager.getConnection(Framework.getProperty(URL_PROPERTY),
083                Framework.getProperty(USER_PROPERTY), Framework.getProperty(PASSWORD_PROPERTY));
084        doOnAllTables(connection, null, Framework.getProperty(USER_PROPERTY).toUpperCase(), "DROP TABLE \"%s\"");
085        dropSequences(connection);
086        connection.close();
087    }
088
089    public void dropSequences(Connection connection) throws SQLException {
090        List<String> sequenceNames = new ArrayList<String>();
091        Statement st = connection.createStatement();
092        ResultSet rs = st.executeQuery("SELECT SEQUENCE_NAME FROM USER_SEQUENCES");
093        while (rs.next()) {
094            String sequenceName = rs.getString(1);
095            if (sequenceName.indexOf('$') != -1) {
096                continue;
097            }
098            sequenceNames.add(sequenceName);
099        }
100        rs.close();
101        for (String sequenceName : sequenceNames) {
102            String sql = String.format("DROP SEQUENCE \"%s\"", sequenceName);
103            log.trace("SQL: " + sql);
104            st.execute(sql);
105        }
106        st.close();
107    }
108
109    @Override
110    public String getDeploymentContrib() {
111        return CONTRIB_XML;
112    }
113
114    @Override
115    public RepositoryDescriptor getRepositoryDescriptor() {
116        RepositoryDescriptor descriptor = new RepositoryDescriptor();
117        descriptor.xaDataSourceName = XA_DATASOURCE;
118        Map<String, String> properties = new HashMap<String, String>();
119        properties.put("URL", Framework.getProperty(URL_PROPERTY));
120        properties.put("User", Framework.getProperty(USER_PROPERTY));
121        properties.put("Password", Framework.getProperty(PASSWORD_PROPERTY));
122        descriptor.properties = properties;
123        return descriptor;
124    }
125
126    @Override
127    public boolean supportsClustering() {
128        return true;
129    }
130
131}