/**
* This program is copyright (c) 2007 Hortis-GRC SA.
*
* This file is part of Sonar.
* Sonar is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
package ch.hortis.sonar.application;
import ch.hortis.sonar.db.DatabaseEmbedder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
public class SonarWebLauncher {
private static final int DEFAULT_WEB_PORT = 9000;
public static void main(String[] args) throws Exception {
String dbProp = System.getProperty( "sonar.embedded.db" );
List<String> arguments = Arrays.asList(args);
if ( dbProp == null || dbProp.equalsIgnoreCase( "true" ) ) {
DatabaseEmbedder db = dbStartup();
createDatabaseEmbedderShutdownHook( db );
}
int port = DEFAULT_WEB_PORT;
if (arguments.indexOf("-port")>=0) {
port = Integer.parseInt(arguments.get(arguments.indexOf("-port")+1));
}
startWebServer(port);
Thread.currentThread().join();
}
private static DatabaseEmbedder dbStartup() throws Exception {
Properties derbyProps = new Properties();
InputStream config = SonarWebLauncher.class.getResourceAsStream( "/derby.properties" );
if ( config == null ) {
throw new Exception( "Unable to find /derby.properties in classpath" );
}
derbyProps.load( config );
final DatabaseEmbedder db;
File dbHome = new File( "../data" );
if ( !dbHome.exists() ) {
throw new FileNotFoundException( "../data" );
}
if (!derbyProps.isEmpty()) {
db = new DatabaseEmbedder( dbHome, derbyProps );
} else {
db = new DatabaseEmbedder( dbHome );
}
db.start();
System.out.println( "Embedded database started" );
return db;
}
private static void createDatabaseEmbedderShutdownHook( final DatabaseEmbedder db ) {
Runtime.getRuntime().addShutdownHook( new Thread() {
public void run() {
try {
db.stop();
System.out.println( "Embedded database stopped" );
} catch ( Exception e ) {
System.err.println("Error during embedded database shutdown");
e.printStackTrace( System.err );
}
}
});
}
private static void startWebServer(int port) throws Exception {
JettyEmbedder embeddder = new JettyEmbedder();
embeddder.start(port);
}
}