public SMDApplication() {
mainThread = Thread.currentThread();
try {
DatabaseProvider provider = null;
String database = InjectHelper.instanceWithName(String.class, "org.socialmusicdiscovery.server.database");
if (database != null) {
provider = InjectHelper.instanceWithName(DatabaseProvider.class, database);
if (provider == null) {
throw new RuntimeException("No database provider exists for: " + database);
}
} else {
throw new RuntimeException("No database provider configured");
}
provider.start();
Connection connection = provider.getConnection();
Liquibase liquibase = new Liquibase("org/socialmusicdiscovery/server/database/smd-database.changelog.xml", new
ClassLoaderResourceAccessor(),
new JdbcConnection(connection));
if (System.getProperty("liquibase") == null || !System.getProperty("liquibase").equals("false")) {
// If database is locked, retry 10 times before we give up and force the lock to be released
for(int i=0;i<10 && liquibase.listLocks().length>0;i++) {
Thread.sleep(1000);
}
if(liquibase.listLocks().length>0) {
liquibase.forceReleaseLocks();
}
liquibase.update("");
}
InjectHelper.injectMembers(this);
// Add shutdown hook to exit cleanly after receiving a CTRL-C
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
System.out.println("Initiating shutdown...");
shutdownRequest = true;
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
// Initialize all installed plugins
pluginManager.startAll();
System.out.println("Hit Ctrl+C to stop it...");
while(!shutdownRequest) {
try {
Thread.sleep(1000);
}catch(InterruptedException e) {
// Do nothing
}
}
System.out.println("\n\nExiting...\n");
this.shutdown();
provider.stop();
System.out.flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}