Search in sources :

Example 1 with Version

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;
}
Also used : DataException(com.autentia.tnt.manager.data.exception.DataException) Version(com.autentia.tnt.version.Version) MigrationManager(com.autentia.tnt.manager.data.MigrationManager)

Example 2 with Version

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() + ")";
    }
}
Also used : Version(com.autentia.tnt.version.Version)

Example 3 with Version

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);
    }
}
Also used : Locale(java.util.Locale) UsernameNotFoundException(org.acegisecurity.userdetails.UsernameNotFoundException) DataNotFoundException(com.autentia.tnt.dao.DataNotFoundException) User(com.autentia.tnt.businessobject.User) DataAccException(com.autentia.tnt.dao.DataAccException) SecException(com.autentia.tnt.manager.security.exception.SecException) SQLException(java.sql.SQLException) DataIntegrityException(com.autentia.tnt.dao.DataIntegrityException) GrantedAuthority(org.acegisecurity.GrantedAuthority) Setting(com.autentia.tnt.businessobject.Setting) DataIntegrityViolationException(org.springframework.dao.DataIntegrityViolationException) Version(com.autentia.tnt.version.Version) SettingSearch(com.autentia.tnt.dao.search.SettingSearch) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException)

Example 4 with Version

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;
}
Also used : InputStreamReader(java.io.InputStreamReader) SQLException(java.sql.SQLException) Statement(java.sql.Statement) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) IOException(java.io.IOException) IOException(java.io.IOException) DataException(com.autentia.tnt.manager.data.exception.DataException) SQLException(java.sql.SQLException) LineNumberReader(java.io.LineNumberReader) DataException(com.autentia.tnt.manager.data.exception.DataException) Version(com.autentia.tnt.version.Version) BufferedInputStream(java.io.BufferedInputStream) Session(org.hibernate.Session)

Aggregations

Version (com.autentia.tnt.version.Version)4 DataException (com.autentia.tnt.manager.data.exception.DataException)2 SQLException (java.sql.SQLException)2 Setting (com.autentia.tnt.businessobject.Setting)1 User (com.autentia.tnt.businessobject.User)1 DataAccException (com.autentia.tnt.dao.DataAccException)1 DataIntegrityException (com.autentia.tnt.dao.DataIntegrityException)1 DataNotFoundException (com.autentia.tnt.dao.DataNotFoundException)1 SettingSearch (com.autentia.tnt.dao.search.SettingSearch)1 MigrationManager (com.autentia.tnt.manager.data.MigrationManager)1 SecException (com.autentia.tnt.manager.security.exception.SecException)1 BufferedInputStream (java.io.BufferedInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 LineNumberReader (java.io.LineNumberReader)1 Connection (java.sql.Connection)1 Statement (java.sql.Statement)1 Locale (java.util.Locale)1 GrantedAuthority (org.acegisecurity.GrantedAuthority)1