mirror of
https://github.com/Athou/commafeed.git
synced 2026-03-21 21:37:29 +00:00
64
src/main/java/com/commafeed/backend/DatabaseUpdater.java
Normal file
64
src/main/java/com/commafeed/backend/DatabaseUpdater.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.commafeed.backend;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
import javax.ejb.Stateless;
|
||||
import javax.ejb.TransactionManagement;
|
||||
import javax.ejb.TransactionManagementType;
|
||||
import javax.naming.Context;
|
||||
import javax.naming.InitialContext;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import liquibase.Liquibase;
|
||||
import liquibase.database.Database;
|
||||
import liquibase.database.DatabaseFactory;
|
||||
import liquibase.database.jvm.JdbcConnection;
|
||||
import liquibase.resource.ClassLoaderResourceAccessor;
|
||||
import liquibase.resource.ResourceAccessor;
|
||||
|
||||
@Stateless
|
||||
@TransactionManagement(TransactionManagementType.BEAN)
|
||||
public class DatabaseUpdater {
|
||||
|
||||
public void update() {
|
||||
String datasourceName = ResourceBundle.getBundle("application")
|
||||
.getString("datasource");
|
||||
try {
|
||||
Context context = null;
|
||||
Connection connection = null;
|
||||
try {
|
||||
Thread currentThread = Thread.currentThread();
|
||||
ClassLoader classLoader = currentThread.getContextClassLoader();
|
||||
ResourceAccessor accessor = new ClassLoaderResourceAccessor(
|
||||
classLoader);
|
||||
|
||||
context = new InitialContext();
|
||||
DataSource dataSource = (DataSource) context
|
||||
.lookup(datasourceName);
|
||||
connection = dataSource.getConnection();
|
||||
|
||||
Database database = DatabaseFactory.getInstance()
|
||||
.findCorrectDatabaseImplementation(
|
||||
new JdbcConnection(connection));
|
||||
|
||||
Liquibase liq = new Liquibase(
|
||||
"changelogs/db.changelog-master.xml", accessor,
|
||||
database);
|
||||
liq.update("prod");
|
||||
} finally {
|
||||
if (context != null) {
|
||||
context.close();
|
||||
}
|
||||
if (connection != null) {
|
||||
connection.close();
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -43,6 +43,9 @@ public class StartupBean {
|
||||
public static final String USERNAME_ADMIN = "admin";
|
||||
public static final String USERNAME_DEMO = "demo";
|
||||
|
||||
@Inject
|
||||
DatabaseUpdater databaseUpdater;
|
||||
|
||||
@Inject
|
||||
FeedDAO feedDAO;
|
||||
|
||||
@@ -74,6 +77,8 @@ public class StartupBean {
|
||||
private void init() {
|
||||
|
||||
startupTime = Calendar.getInstance().getTimeInMillis();
|
||||
databaseUpdater.update();
|
||||
|
||||
if (userDAO.getCount() == 0) {
|
||||
initialData();
|
||||
}
|
||||
@@ -148,5 +153,4 @@ public class StartupBean {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user