/*
* 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.core.batch;
import ch.hortis.sonar.jpa.Persistence;
import ch.hortis.sonar.jpa.WrongDatabaseVersionException;
import ch.hortis.sonar.model.JdbcData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
public final class Batch {
protected static final Logger LOG = LoggerFactory.getLogger(Batch.class);
private static final Batch INSTANCE = new Batch();
private static final int RECURRENT_TASKS_DELAY_MILLISECONDS = 24 * 60 * 60 * 1000; // 24 hours
private static final int THREAD_POOL_SIZE = 1;
private Batch() {
}
public static Batch getInstance() {
return INSTANCE;
}
private JdbcData jdbcData;
private Scheduler scheduler;
public void start( String jdbcURL, String jdbcDriver, String username, String password ) {
start( new JdbcData( jdbcURL, jdbcDriver, username, password ) );
}
public void start( String datasource, String jdbcDialect ) {
start( new JdbcData( datasource, jdbcDialect) );
}
public final void start(JdbcData jdbcData) {
LOG.info("Start batch");
this.jdbcData = jdbcData;
logDatabaseStatus();
scheduler = new Scheduler(THREAD_POOL_SIZE);
scheduler.scheduleAtFixedRate(new ProjectAnalyserTask(jdbcData), RECURRENT_TASKS_DELAY_MILLISECONDS);
scheduler.scheduleAtFixedRate(new ProjectEraserTask(jdbcData), RECURRENT_TASKS_DELAY_MILLISECONDS);
scheduler.scheduleAtFixedRate(new PurgeMeasuresTask(jdbcData), RECURRENT_TASKS_DELAY_MILLISECONDS);
scheduler.scheduleAtFixedRate(new PurgeSnapshotsTask(jdbcData), RECURRENT_TASKS_DELAY_MILLISECONDS);
}
public void processProject() {
scheduler.runOnce(new ProjectAnalyserTask(jdbcData));
}
public void shutdown() {
if(scheduler!=null) {
scheduler.shutdown();
}
}
private void logDatabaseStatus() {
try {
if (!Persistence.databaseExists(jdbcData)) {
LOG.warn("Database tables must be created. Please browse to your sonar server.");
}
} catch (SQLException e) {
LOG.error("Error with database", e);
} catch (WrongDatabaseVersionException e) {
LOG.warn("Database must be updated. Please browse to your sonar server. " + e.getMessage());
}
}
/*public static void main( String[] args ) {
Batch.getInstance().start("jdbc:mysql://192.168.1.33:3306/sonar", "com.mysql.jdbc.Driver", "sonar", "sonar");
}*/
}