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