use of com.autentia.tnt.version.Version in project TNTConcept by autentia.
the class ConsoleBean method migrateDB.
public String migrateDB() {
MigrationManager mig = MigrationManager.getDefault();
try {
// Migrate database
Version oldVersion = mig.upgradeDatabase();
// Tell user
FacesUtils.addInfoMessage(null, "msg.migrationSuccess", oldVersion, Version.getApplicationVersion());
// Refresh lock filter state
ApplicationLock.refresh();
} catch (DataException e) {
FacesUtils.addErrorMessage(null, "error.migration");
log.error("migrateDB - exception", e);
}
return null;
}
use of com.autentia.tnt.version.Version in project TNTConcept by autentia.
the class ApplicationLock method refresh.
public static void refresh() {
try {
Version db = Version.getDatabaseVersion();
Version code = Version.getApplicationVersion();
int cmp = db.compareTo(code, Version.MINOR);
if (cmp == 0) {
locked = false;
reason = "msg.notLocked";
log.info("refresh - database version correct: application unlocked");
} else if (cmp < 0) {
locked = true;
reason = "msg.dbNeedsUpdate";
log.info("refresh - database version not correct: database needs update");
} else {
locked = true;
reason = "msg.appNeedsUpdate";
log.info("refresh - database version not correct: application code needs update");
}
} catch (Exception e) {
log.fatal("refresh - exception initializing ApplicationLock: application will not be available", e);
locked = true;
reason = e.getMessage() + " (" + e.getClass().getSimpleName() + ")";
}
}
use of com.autentia.tnt.version.Version in project TNTConcept by autentia.
the class AuthenticationManager method loadUserByUsername.
/**
* Load a User for ACEGI given its user name
*
* @param username user name
* @throws org.acegisecurity.userdetails.UsernameNotFoundException
* @throws org.springframework.dao.DataAccessException
* @return the user object description as specified by ACEGI
*/
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
try {
Version db = Version.getDatabaseVersion();
Version code = Version.getApplicationVersion();
if (db.compareTo(code, Version.MINOR) == 0) {
log.info("loadUserByUsername - getting user " + username + " using Hibernate");
User user = userDAO.searchByLogin(username);
GrantedAuthority[] auths = userRolesService.getAuthorities(user);
if (log.isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
for (GrantedAuthority auth : auths) {
sb.append(auth);
sb.append(" ");
}
log.debug("loadUserByUsername - user roles: " + sb);
}
final Principal principal = new Principal(user, auths);
// setting user preferred Locale
final SettingSearch s = new SettingSearch();
s.setName(SettingPath.GENERAL_PREFERRED_LOCALE);
s.setOwnerId(user.getId());
final List<Setting> vals = settings.search(s, null);
final Setting val = (vals != null && vals.size() > 0) ? vals.get(0) : null;
if (val != null) {
final Locale local = new Locale(val.getValue());
principal.setLocale(local);
}
return principal;
} else {
log.info("loadUserByUsername - getting user " + username + " using JDBC");
return jdbcSearchByLogin(username);
}
} catch (SecException e) {
log.warn("loadUserByUsername - exception", e);
throw new DataRetrievalFailureException("Error getting roles for user: " + username, e);
} catch (DataIntegrityException e) {
log.warn("loadUserByUsername - exception", e);
throw new DataIntegrityViolationException("Inconsistent user name: " + username, e);
} catch (DataNotFoundException e) {
log.warn("loadUserByUsername - exception", e);
throw new UsernameNotFoundException("User not found: " + username, e);
} catch (DataAccException e) {
log.warn("loadUserByUsername - exception", e);
throw new DataRetrievalFailureException("Error getting user: " + username, e);
} catch (SQLException e) {
log.warn("loadUserByUsername - exception", e);
throw new DataRetrievalFailureException("Error getting user: " + username, e);
}
}
use of com.autentia.tnt.version.Version in project TNTConcept by autentia.
the class MigrationManager method upgradeDatabase.
/**
* @return the original database version
* @throws com.autentia.tnt.manager.data.exception.DataException
*/
public Version upgradeDatabase() throws DataException {
Version ret = null;
Version db = null;
Version code = Version.getApplicationVersion();
Session ses = HibernateUtil.getSessionFactory().openSession();
Connection con = ses.connection();
Statement stmt = null;
LineNumberReader file = null;
String delimiter = ";";
try {
db = Version.getDatabaseVersion();
ret = db.clone();
log.info("upgradeDatabase - >>>> STARTING MIGRATION FROM " + db + " TO " + code + " <<<<");
// Disable auto-commit (just in case...)
log.info("upgradeDatabase - disabling auto commit");
con.setAutoCommit(false);
// Create statement
stmt = con.createStatement();
// While necessary, upgrade database
while (db.compareTo(code, Version.MINOR) < 0) {
log.info("upgradeDatabase - " + db);
// Compose script name and open it
String script = SCRIPT_PREFIX + db.toString(Version.MINOR) + SCRIPT_SUFFIX;
log.info("upgradeDatabase - loading script " + script);
InputStream sqlScript = Thread.currentThread().getContextClassLoader().getResourceAsStream(script);
if (sqlScript == null) {
throw FileNotFoundException(script);
}
file = new LineNumberReader(new InputStreamReader(new BufferedInputStream(sqlScript), "UTF-8"));
int _c;
// Add batched SQL sentences to statement
StringBuilder sentence = new StringBuilder();
String line;
while ((line = file.readLine()) != null) {
line = line.trim();
if (!line.startsWith("--")) {
// Interpret "DELIMITER" sentences
if (line.trim().toUpperCase(Locale.ENGLISH).startsWith("DELIMITER")) {
delimiter = line.trim().substring("DELIMITER".length()).trim();
} else {
// Add line to sentence
if (line.endsWith(delimiter)) {
// Remove delimiter
String lastLine = line.substring(0, line.length() - delimiter.length());
// Append line to sentence
sentence.append(lastLine);
// Execute sentence
log.info(" " + sentence.toString());
stmt.addBatch(sentence.toString());
// Prepare new sentence
sentence = new StringBuilder();
} else {
// Append line to sentence
sentence.append(line);
// Append separator for next line
sentence.append(" ");
}
}
}
}
// Execute batch
log.info("upgradeDatabase - executing batch of commands");
stmt.executeBatch();
// Re-read database version
Version old = db;
db = Version.getDatabaseVersion(con);
if (old.equals(db)) {
throw new DataException("Script was applied but did not upgrade database: " + script);
}
log.info("upgradeDatabase - database upgraded to version " + db);
}
// Commit transaction
log.info("upgradeDatabase - commiting changes to database");
con.commit();
// Report end of migration
log.info("upgradeDatabase - >>>> MIGRATION SUCCESSFULLY FINISHED <<<<");
} catch (Exception e) {
log.error("upgradeDatabase - >>>> MIGRATION FAILED: WILL BE ROLLED BACK <<<<", e);
try {
con.rollback();
} catch (SQLException e2) {
log.error("upgradeDatabase - Error al realizar el rollback");
}
throw new DataException("Script was applied but did not upgrade database: ", e);
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e2) {
log.error("upgradeDatabase - Error al cerrar el fichero de script ", e2);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e2) {
log.error("upgradeDatabase - Error al cerrar el statement");
}
}
ses.close();
}
return ret;
}
Aggregations