org.sonar.server.database.EmbeddedDatabase.java Source code

Java tutorial

Introduction

Here is the source code for org.sonar.server.database.EmbeddedDatabase.java

Source

/*
 * Sonar, open source software quality management tool.
 * Copyright (C) 2008-2012 SonarSource
 * mailto:contact AT sonarsource DOT com
 *
 * Sonar is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * Sonar is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sonar; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.server.database;

import org.h2.Driver;

import org.apache.commons.lang.StringUtils;
import org.h2.tools.Server;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.config.Settings;
import org.sonar.api.database.DatabaseProperties;
import org.sonar.api.utils.SonarException;
import org.sonar.server.platform.ServerStartException;

import java.io.File;
import java.sql.DriverManager;
import java.sql.SQLException;

public class EmbeddedDatabase {
    private static final Logger LOG = LoggerFactory.getLogger(EmbeddedDatabase.class);

    private final Settings settings;
    private Server server;

    public EmbeddedDatabase(Settings settings) {
        this.settings = settings;
    }

    public void start() {
        File dbHome = getDataDirectory(settings);
        if (dbHome.exists() && !dbHome.isDirectory()) {
            throw new SonarException("Database home " + dbHome.getPath() + " is not a directory");
        }
        if (!dbHome.exists()) {
            dbHome.mkdirs();
        }

        String port = getSetting(DatabaseProperties.PROP_EMBEDDED_PORT,
                DatabaseProperties.PROP_EMBEDDED_PORT_DEFAULT_VALUE);
        String user = getSetting(DatabaseProperties.PROP_USER, DatabaseProperties.PROP_USER_DEFAULT_VALUE);
        String password = getSetting(DatabaseProperties.PROP_PASSWORD,
                DatabaseProperties.PROP_PASSWORD_DEFAULT_VALUE);
        String url = getSetting(DatabaseProperties.PROP_URL, DatabaseProperties.PROP_USER_DEFAULT_VALUE);

        try {
            if (url.contains("/mem:")) {
                server = Server.createTcpServer("-tcpPort", port, "-tcpAllowOthers", "-baseDir",
                        dbHome.getAbsolutePath());
            } else {
                createDatabase(dbHome, user, password);
                server = Server.createTcpServer("-tcpPort", port, "-tcpAllowOthers", "-ifExists", "-baseDir",
                        dbHome.getAbsolutePath());
            }

            LOG.info("Starting embedded database on port " + server.getPort() + " with url " + url);
            server.start();

            LOG.info("Embedded database started. Data stored in: " + dbHome.getAbsolutePath());
        } catch (Exception e) {
            throw new SonarException("Unable to start database", e);
        }
    }

    public void stop() {
        if (server != null) {
            server.stop();
            server = null;
            LOG.info("Embedded database stopped");
        }
    }

    private String getSetting(String name, String defaultValue) {
        return StringUtils.defaultIfBlank(settings.getString(name), defaultValue);
    }

    private void createDatabase(File dbHome, String user, String password) throws SQLException {
        String url = String.format("jdbc:h2:%s/sonar;USER=%s;PASSWORD=%s", dbHome.getAbsolutePath(), user,
                password);

        DriverManager.registerDriver(new Driver());
        DriverManager.getConnection(url).close();
    }

    private static File getDataDirectory(Settings settings) {
        String dirName = settings.getString(DatabaseProperties.PROP_EMBEDDED_DATA_DIR);
        if (!StringUtils.isBlank(dirName)) {
            return new File(dirName);
        }

        File sonarHome = new File(settings.getString(CoreProperties.SONAR_HOME));
        if (sonarHome.isDirectory() && sonarHome.exists()) {
            return new File(sonarHome, "data");
        }

        throw new ServerStartException("Sonar home directory does not exist");
    }
}