001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * Florent Guillaume 011 */ 012 013package org.nuxeo.ecm.core.storage.sql; 014 015import java.sql.Connection; 016import java.sql.DriverManager; 017import java.sql.SQLException; 018import java.sql.Statement; 019import java.util.HashMap; 020import java.util.Map; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.runtime.api.Framework; 025 026/** 027 * @author Florent Guillaume 028 */ 029public class DatabaseH2 extends DatabaseHelper { 030 031 public static final DatabaseHelper INSTANCE = new DatabaseH2(); 032 033 private static final Log log = LogFactory.getLog(DatabaseH2.class); 034 035 /** This directory will be deleted and recreated. */ 036 protected static final String DIRECTORY = "target"; 037 038 protected static final String DEF_USER = "sa"; 039 040 protected static final String DEF_PASSWORD = ""; 041 042 protected static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-h2-contrib.xml"; 043 044 protected static final String DRIVER = "org.h2.Driver"; 045 046 protected static final String URL_FORMAT = "jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1"; 047 048 protected String url; 049 050 protected String url2; 051 052 protected String user; 053 054 protected String password; 055 056 protected void setProperties() { 057 url = setProperty(URL_PROPERTY, String.format(URL_FORMAT, databaseName)); 058 059 setProperty(REPOSITORY_PROPERTY, repositoryName); 060 setProperty(DATABASE_PROPERTY, databaseName); 061 user = setProperty(USER_PROPERTY, DEF_USER); 062 password = setProperty(PASSWORD_PROPERTY, DEF_PASSWORD); 063 // for sql directory tests 064 setProperty(DRIVER_PROPERTY, DRIVER); 065 } 066 067 protected void setProperties2() { 068 url2 = String.format(URL_FORMAT, databaseName + "2"); 069 setProperty(URL_PROPERTY + "2", url2); 070 Framework.getProperties().setProperty(REPOSITORY_PROPERTY + "2", repositoryName + "2"); 071 } 072 073 @Override 074 public void setUp() throws Exception { 075 super.setUp(); 076 Class.forName(DRIVER); 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 public void setUp2() throws Exception { 091 setProperties2(); 092 } 093 094 protected String getId() { 095 return "nuxeo"; 096 } 097 098 @Override 099 public void tearDown() throws SQLException { 100 if (owner == null) { 101 return; 102 } 103 try { 104 tearDownDatabase(url); 105 if (url2 != null) { 106 tearDownDatabase(url2); 107 } 108 } finally { 109 super.tearDown(); 110 } 111 } 112 113 protected void tearDownDatabase(String url) throws SQLException { 114 Connection connection = DriverManager.getConnection(url, user, password); 115 try { 116 Statement st = connection.createStatement(); 117 try { 118 String sql = "SHUTDOWN"; 119 log.trace(sql); 120 st.execute(sql); 121 } finally { 122 st.close(); 123 } 124 } finally { 125 connection.close(); 126 } 127 } 128 129 @Override 130 public String getDeploymentContrib() { 131 return CONTRIB_XML; 132 } 133 134 @Override 135 public RepositoryDescriptor getRepositoryDescriptor() { 136 RepositoryDescriptor descriptor = new RepositoryDescriptor(); 137 descriptor.xaDataSourceName = "org.h2.jdbcx.JdbcDataSource"; 138 Map<String, String> properties = new HashMap<String, String>(); 139 properties.put("URL", url); 140 properties.put("User", Framework.getProperty(USER_PROPERTY)); 141 properties.put("Password", Framework.getProperty(PASSWORD_PROPERTY)); 142 descriptor.properties = properties; 143 return descriptor; 144 } 145 146 @Override 147 public boolean supportsClustering() { 148 return true; 149 } 150 151}