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.io.File; 023import java.sql.DriverManager; 024import java.sql.SQLException; 025import java.util.HashMap; 026import java.util.Map; 027 028import org.nuxeo.common.utils.FileUtils; 029import org.nuxeo.runtime.api.Framework; 030 031/** 032 * @author Florent Guillaume 033 */ 034public class DatabaseDerby extends DatabaseHelper { 035 036 public static final DatabaseHelper INSTANCE = new DatabaseDerby(); 037 038 /** This directory will be deleted and recreated. */ 039 private static final String DIRECTORY = "target/test/derby"; 040 041 private static final String DEF_USER = "sa"; 042 043 private static final String DEF_PASSWORD = ""; 044 045 private static final String CONTRIB_XML = "OSGI-INF/test-repo-repository-derby-contrib.xml"; 046 047 private static final String LOG = "target/test/derby.log"; 048 049 private static final String DRIVER = "org.apache.derby.jdbc.EmbeddedDriver"; 050 051 protected String url; 052 053 protected void setProperties() { 054 setProperty(DATABASE_PROPERTY, new File(DIRECTORY).getAbsolutePath()); 055 setProperty(USER_PROPERTY, DEF_USER); 056 setProperty(PASSWORD_PROPERTY, DEF_PASSWORD); 057 // for sql directory tests 058 setProperty(DRIVER_PROPERTY, DRIVER); 059 url = String.format("jdbc:derby:%s;create=true", Framework.getProperty(DATABASE_PROPERTY)); 060 setProperty(URL_PROPERTY, url); 061 } 062 063 @Override 064 public void setUp() throws SQLException { 065 super.setUp(); 066 System.setProperty("derby.stream.error.file", new File(LOG).getAbsolutePath()); 067 // newInstance needed after a previous shutdown 068 try { 069 Class.forName(DRIVER).newInstance(); 070 } catch (ReflectiveOperationException e) { 071 throw new RuntimeException(e); 072 } 073 File dbdir = new File(DIRECTORY); 074 File parent = dbdir.getParentFile(); 075 FileUtils.deleteTree(dbdir); 076 parent.mkdirs(); 077 // the following noticeably improves performance 078 System.setProperty("derby.system.durability", "test"); 079 setProperties(); 080 } 081 082 @Override 083 public void tearDown() throws SQLException { 084 Exception ex = null; 085 try { 086 DriverManager.getConnection("jdbc:derby:;shutdown=true"); 087 // after this to reboot the driver a newInstance is needed 088 } catch (SQLException e) { 089 String message = e.getMessage(); 090 if ("Derby system shutdown.".equals(message)) { 091 return; 092 } 093 if ("org.apache.derby.jdbc.EmbeddedDriver is not registered with the JDBC driver manager".equals(message)) { 094 // huh? happens for testClustering 095 return; 096 } 097 ex = e; 098 } finally { 099 super.tearDown(); 100 } 101 throw new RuntimeException("Expected Derby shutdown exception instead", ex); 102 } 103 104 @Override 105 public String getDeploymentContrib() { 106 return CONTRIB_XML; 107 } 108 109 @Override 110 public RepositoryDescriptor getRepositoryDescriptor() { 111 RepositoryDescriptor descriptor = new RepositoryDescriptor(); 112 descriptor.xaDataSourceName = "org.apache.derby.jdbc.EmbeddedXADataSource"; 113 Map<String, String> properties = new HashMap<String, String>(); 114 properties.put("createDatabase", "create"); 115 properties.put("databaseName", Framework.getProperty(DATABASE_PROPERTY)); 116 properties.put("user", Framework.getProperty(USER_PROPERTY)); 117 properties.put("password", Framework.getProperty(PASSWORD_PROPERTY)); 118 descriptor.properties = properties; 119 return descriptor; 120 } 121 122 @Override 123 public boolean supportsMultipleFulltextIndexes() { 124 return false; 125 } 126 127}