diff options
| author | Shireesh Anjal <shireesh@gluster.com> | 2011-08-09 22:25:27 +0530 |
|---|---|---|
| committer | Shireesh Anjal <shireesh@gluster.com> | 2011-08-09 22:26:56 +0530 |
| commit | e878e1aecb3fd21615102e3806390efa8277851c (patch) | |
| tree | 8ae9677002d71bb84637ec2a8cfdeed29b08a59c /src | |
| parent | 78c81c7de0c3f99525935eaab6b76ebc9394e152 (diff) | |
Modified password manager to shutdown derby after resetting the password
Diffstat (limited to 'src')
2 files changed, 63 insertions, 19 deletions
diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/security/UserAuthDao.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/security/UserAuthDao.java index c930c295..cb2ecb24 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/security/UserAuthDao.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/security/UserAuthDao.java @@ -20,15 +20,11 @@ */ package com.gluster.storage.management.gateway.security; -import java.sql.Connection; -import java.sql.SQLException; - -import javax.persistence.EntityManager; -import javax.persistence.EntityManagerFactory; -import javax.persistence.EntityTransaction; -import javax.persistence.PersistenceUnit; - +import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.security.core.userdetails.jdbc.JdbcDaoImpl; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionDefinition; import com.gluster.storage.management.core.exceptions.GlusterRuntimeException; @@ -45,19 +41,17 @@ public class UserAuthDao extends JdbcDaoImpl implements GlusterUserDetailsServic */ @Override public void changePassword(String username, String password) { + DataSourceTransactionManager txnManager = new DataSourceTransactionManager(); + txnManager.setDataSource(getDataSource()); + + TransactionDefinition def = new DefaultTransactionDefinition(); + TransactionStatus status = txnManager.getTransaction(def); try { getJdbcTemplate().update("UPDATE USERS SET PASSWORD = ? WHERE USERNAME = ?", password, username); - Connection connection = getDataSource().getConnection(); - connection.commit(); - connection.close(); + txnManager.commit(status); } catch(Exception e) { - String errMsg = "Exception while changing password of user [" + username + "]. Error: " + e.getMessage(); - try { - getDataSource().getConnection().rollback(); - } catch (SQLException e1) { - throw new GlusterRuntimeException(errMsg + ", " + e1.getMessage()); - } - throw new GlusterRuntimeException(errMsg); + txnManager.rollback(status); + throw new GlusterRuntimeException("Exception while changing password of user [" + username + "]. Error: " + e.getMessage()); } } } diff --git a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/PasswordManager.java b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/PasswordManager.java index 503a6ea0..df3aeff7 100644 --- a/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/PasswordManager.java +++ b/src/com.gluster.storage.management.gateway/src/com/gluster/storage/management/gateway/utils/PasswordManager.java @@ -18,7 +18,8 @@ *******************************************************************************/ package com.gluster.storage.management.gateway.utils; -import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; import org.apache.derby.jdbc.EmbeddedDriver; import org.springframework.jdbc.datasource.SimpleDriverDataSource; @@ -44,8 +45,11 @@ public class PasswordManager { saltSource.getSalt(userAuthDao.loadUserByUsername(username))); userAuthDao.changePassword(username, encodedPassword); + System.out.println("Password for user [" + username + "] reset successsfully to default value of [" + CoreConstants.DEFAULT_PASSWORD + "]." + CoreConstants.NEWLINE); + + shutdownDerby(); } catch (Exception e) { System.err.println(CoreConstants.NEWLINE + CoreConstants.NEWLINE + "Password reset for user [" + username + "] failed! " + CoreConstants.NEWLINE @@ -54,6 +58,52 @@ public class PasswordManager { System.exit(SQL_ERR); } } + + private void shutdownDerby() { + try { + // the shutdown=true attribute shuts down Derby + DriverManager.getConnection("jdbc:derby:;shutdown=true"); + + // To shut down a specific database only, but keep the + // engine running (for example for connecting to other + // databases), specify a database in the connection URL: + //DriverManager.getConnection("jdbc:derby:" + dbName + ";shutdown=true"); + } catch (SQLException se) { + if (((se.getErrorCode() == 50000) && ("XJ015".equals(se.getSQLState())))) { + // we got the expected exception + System.out.println("Derby shut down normally"); + // Note that for single database shutdown, the expected + // SQL state is "08006", and the error code is 45000. + } else { + // if the error code or SQLState is different, we have + // an unexpected exception (shutdown failed) + System.err.println("Derby did not shut down normally"); + printSQLException(se); + } + } + } + + /** + * Prints details of an SQLException chain to <code>System.err</code>. + * Details included are SQL State, Error code, Exception message. + * + * @param e the SQLException from which to print details. + */ + private void printSQLException(SQLException e) + { + // Unwraps the entire exception chain to unveil the real cause of the + // Exception. + while (e != null) + { + System.err.println("\n----- SQLException -----"); + System.err.println(" SQL State: " + e.getSQLState()); + System.err.println(" Error Code: " + e.getErrorCode()); + System.err.println(" Message: " + e.getMessage()); + // for stack traces, refer to derby.log or uncomment this: + //e.printStackTrace(System.err); + e = e.getNextException(); + } + } private ReflectionSaltSource createSaltSource() { ReflectionSaltSource saltSource = new ReflectionSaltSource(); |
