001 /* 002 * Sonar, open source software quality management tool. 003 * Copyright (C) 2009 SonarSource SA 004 * mailto:contact AT sonarsource DOT com 005 * 006 * Sonar is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * Sonar is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with Sonar; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 019 */ 020 package org.sonar.api.database; 021 022 import org.apache.commons.configuration.Configuration; 023 import org.apache.commons.configuration.PropertiesConfiguration; 024 025 import java.sql.Connection; 026 import javax.persistence.EntityManager; 027 import javax.persistence.EntityManagerFactory; 028 029 public class InMemoryDatabaseConnector extends DriverDatabaseConnector { 030 public static final String DRIVER = "org.hsqldb.jdbcDriver"; 031 public static final String URL = "jdbc:hsqldb:mem:sonar"; 032 public static final String USER = "sa"; 033 public static final String PASSWORD = ""; 034 public static final int ISOLATION = Connection.TRANSACTION_READ_UNCOMMITTED; 035 036 private int version; 037 038 public InMemoryDatabaseConnector(Configuration config) { 039 super(config); 040 version = SchemaMigration.LAST_VERSION; 041 } 042 043 public InMemoryDatabaseConnector() { 044 this(getInMemoryConfiguration(true)); 045 } 046 047 public InMemoryDatabaseConnector(int version) { 048 this(getInMemoryConfiguration(true)); 049 this.version = version; 050 } 051 052 protected static Configuration getInMemoryConfiguration(boolean createSchema) { 053 PropertiesConfiguration conf = new PropertiesConfiguration(); 054 conf.setProperty(DatabaseProperties.PROP_URL, URL); 055 conf.setProperty(DatabaseProperties.PROP_DRIVER, DRIVER); 056 conf.setProperty(DatabaseProperties.PROP_USER, USER); 057 conf.setProperty(DatabaseProperties.PROP_PASSWORD, PASSWORD); 058 conf.setProperty(DatabaseProperties.PROP_ISOLATION, ISOLATION); 059 if (createSchema) { 060 conf.setProperty(DatabaseProperties.PROP_HIBERNATE_HBM2DLL, "create-drop"); 061 } 062 return conf; 063 } 064 065 @Override 066 public void start() { 067 try { 068 super.start(); 069 } catch (DatabaseException ex) { 070 if (!isStarted()) { 071 throw ex; 072 } 073 setEntityManagerFactory(createEntityManagerFactory()); 074 setupSchemaVersion(version); 075 } 076 } 077 078 @Override 079 protected EntityManagerFactory createEntityManagerFactory() { 080 return super.createEntityManagerFactory(); 081 } 082 083 protected void setupSchemaVersion(int version) { 084 SchemaMigration migration = new SchemaMigration(); 085 migration.setVersion(version); 086 EntityManager manager = null; 087 try { 088 manager = createEntityManager(); 089 manager.getTransaction().begin(); 090 manager.persist(migration); 091 manager.getTransaction().commit(); 092 093 } finally { 094 if (manager != null) { 095 manager.close(); 096 } 097 } 098 } 099 }