protected void initializeDatabaseLookup(String passwordColumn, String userTable, String userNameColumn) throws IllegalAccessException, ClassNotFoundException, InstantiationException { DBUtil dbUtil = new DBUtil(getDatabaseURL(), getDatabaseUserName(), getDatabasePassword(), getDatabaseDriver()); DataSource dataSource = dbUtil.getDataSource(); jdbcRealm.setDataSource(dataSource); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("SELECT ").append(passwordColumn).append(" FROM ").append(userTable).append(" WHERE ") .append(userNameColumn).append(" = ?"); jdbcRealm.setAuthenticationQuery(stringBuilder.toString()); }
private synchronized Connection getConnection() throws SQLException { Connection connection = dbUtil.getConnection(); connection.setAutoCommit(true); return connection; } }
/** * Utility method to close statements and connections. * * @param preparedStatement * The prepared statement to close. */ public static void cleanup(PreparedStatement preparedStatement, ResultSet resultSet) { if (resultSet != null) { try { resultSet.close(); } catch (SQLException e) { log.error("Error closing prepared statement.", e); } } cleanup(preparedStatement); }
public static void waitTillServerStarts() { DBUtil dbUtil = null; try { dbUtil = new DBUtil(getJDBCUrl(), getUserName(), getPassword(), getDriver()); } catch (Exception e) { // ignore } Connection connection = null; try { if (dbUtil != null) { connection = dbUtil.getConnection(); } } catch (Throwable e) { // ignore } while (connection == null) { try { Thread.sleep(1000); try { if (dbUtil != null) { connection = dbUtil.getConnection(); } } catch (SQLException e) { // ignore } } catch (InterruptedException e) { // ignore } } }
private void initializeDatabaseLookup() throws RuntimeException { try { this.dbUtil = new DBUtil(getDatabaseURL(), getDatabaseUserName(), getDatabasePassword(), getDatabaseDriver()); } catch (ClassNotFoundException e) { throw new RuntimeException("Error loading database driver. Driver class not found.", e); } catch (InstantiationException e) { throw new RuntimeException("Error loading database driver. Error instantiating driver object.", e); } catch (IllegalAccessException e) { throw new RuntimeException("Error loading database driver. Illegal access to driver object.", e); } } }
public void writeCredentials(Credential credential) throws CredentialStoreException { SSHCredential sshCredential = (SSHCredential) credential; Connection connection = null; try { connection = dbUtil.getConnection(); // First delete existing credentials credentialsDAO.deleteCredentials(sshCredential.getGateway(), sshCredential.getToken(), connection); // Add the new certificate credentialsDAO.addCredentials(sshCredential.getGateway(), credential, connection); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException e1) { logger.error("Unable to rollback transaction", e1); } } throw new CredentialStoreException("Unable to retrieve database connection.", e); } finally { DBUtil.cleanup(connection); } }
/** * Creates a DBUtil object based on servlet context configurations. * * @return DBUtil object. * @throws Exception * If an error occurred while reading configurations or while creating database object. */ public static DBUtil getCredentialStoreDBUtil() throws ApplicationSettingsException, IllegalAccessException, ClassNotFoundException, InstantiationException { String jdbcUrl = ServerSettings.getCredentialStoreDBURL(); String userName = ServerSettings.getCredentialStoreDBUser(); String password = ServerSettings.getCredentialStoreDBPassword(); String driverName = ServerSettings.getCredentialStoreDBDriver(); StringBuilder stringBuilder = new StringBuilder("Starting credential store, connecting to database - "); stringBuilder.append(jdbcUrl).append(" DB user - ").append(userName).append(" driver name - ") .append(driverName); log.debug(stringBuilder.toString()); DBUtil dbUtil = new DBUtil(jdbcUrl, userName, password, driverName); dbUtil.init(); return dbUtil; }
public static void executeSQL(String sql) throws Exception { DBUtil dbUtil = new DBUtil(getJDBCUrl(), getUserName(), getPassword(), getDriver()); dbUtil.executeSQL(sql); }
/** * Generic method to query values in the database. * * @param tableName * Table name to query * @param selectColumn * The column selecting * @param whereValue * The condition query * @return The value appropriate to the query. * @throws SQLException * If an error occurred while querying */ public String getMatchingColumnValue(String tableName, String selectColumn, String whereValue) throws SQLException { return getMatchingColumnValue(tableName, selectColumn, selectColumn, whereValue); }
/** * Creates credential reader using default configurations for credential store database. * @return The credential reader. * @throws ClassNotFoundException If an error occurred while instantiating jdbc driver * @throws ApplicationSettingsException If an error occurred while reading database configurations. * @throws InstantiationException If an error occurred while instantiating jdbc driver * @throws IllegalAccessException A security exception accessing jdbc driver. */ public static CredentialReader createCredentialStoreReader() throws ClassNotFoundException, ApplicationSettingsException, InstantiationException, IllegalAccessException { return new CredentialReaderImpl(DBUtil.getCredentialStoreDBUtil()); } }
public static CredentialReader getCredentialReader() throws ApplicationSettingsException, IllegalAccessException, InstantiationException { try { String jdbcUrl = ServerSettings.getCredentialStoreDBURL(); String jdbcUsr = ServerSettings.getCredentialStoreDBUser(); String jdbcPass = ServerSettings.getCredentialStoreDBPassword(); String driver = ServerSettings.getCredentialStoreDBDriver(); return new CredentialReaderImpl(new DBUtil(jdbcUrl, jdbcUsr, jdbcPass, driver)); } catch (ClassNotFoundException e) { log.error("Not able to find driver: " + e.getLocalizedMessage()); return null; } }
public void writeCredentials(Credential credential) throws CredentialStoreException { CertificateCredential certificateCredential = (CertificateCredential) credential; Connection connection = null; try { connection = dbUtil.getConnection(); // Write community user writeCommunityUser(certificateCredential.getCommunityUser(), credential.getToken(), connection); // First delete existing credentials credentialsDAO.deleteCredentials(certificateCredential.getCommunityUser().getGatewayName(), certificateCredential.getToken(), connection); // Add the new certificate credentialsDAO.addCredentials(certificateCredential.getCommunityUser().getGatewayName(), credential, connection); if (!connection.getAutoCommit()) { connection.commit(); } } catch (SQLException e) { if (connection != null) { try { connection.rollback(); } catch (SQLException e1) { log.error("Unable to rollback transaction", e1); } } throw new CredentialStoreException("Unable to retrieve database connection.", e); } finally { DBUtil.cleanup(connection); } }
@Override public boolean authenticate(Object credentials) throws UserStoreException { String sessionTicket = (String) credentials; try { String sessionString = dbUtil.getMatchingColumnValue(sessionTable, sessionColumn, sessionTicket); return (sessionString != null); } catch (SQLException e) { throw new UserStoreException("Error querying database for session information.", e); } }
public void init() throws ServletException { DBUtil dbUtil; try { dbUtil = DBUtil.getCredentialStoreDBUtil(); } catch (Exception e) { throw new ServletException("Error initializing database operations.", e); } try { configurationReader = new ConfigurationReader(); super.init(); certificateCredentialWriter = new CertificateCredentialWriter(dbUtil); } catch (Exception e) { throw new ServletException("Error initializing configuration reader.", e); } // initialize notifier try { boolean enabled = Boolean.parseBoolean(ApplicationSettings.getCredentialStoreNotifierEnabled()); if (enabled) { EmailNotifierConfiguration notifierConfiguration = EmailNotifierConfiguration.getEmailNotifierConfigurations(); long duration = Long.parseLong(ApplicationSettings.getCredentialStoreNotifierDuration()); notifierBootstrap = new NotifierBootstrap(duration, dbUtil, notifierConfiguration); } } catch (ApplicationSettingsException e) { throw new ServletException("Error initializing notifier.", e); } info("Credential store callback initialized successfully."); }
public void deleteCredentials(String gatewayName, String tokenId, Connection connection) throws CredentialStoreException { String sql = "DELETE FROM CREDENTIALS WHERE GATEWAY_ID=? AND TOKEN_ID=?"; PreparedStatement preparedStatement = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, gatewayName); preparedStatement.setString(2, tokenId); preparedStatement.executeUpdate(); connection.commit(); } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error deleting credentials for ."); stringBuilder.append("gateway - ").append(gatewayName); stringBuilder.append("token id - ").append(tokenId); log.error(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement); } }
public DBUtil getDbUtil () throws Exception { return new DBUtil(getJDBCUrl(), getUserName(), getPassword(), getDriver()); }
private Connection getConnection() throws CredentialStoreException { try { return this.dbUtil.getConnection(); } catch (SQLException e) { throw new CredentialStoreException("Unable to retrieve database connection.", e); } }
/** * */ public String getGatewayID(String tokenId, Connection connection) throws CredentialStoreException { String sql = "SELECT GATEWAY_ID FROM CREDENTIALS WHERE TOKEN_ID=?"; PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, tokenId); resultSet = preparedStatement.executeQuery(); if (resultSet.next()) { return resultSet.getString("GATEWAY_ID"); } } catch (SQLException e) { StringBuilder stringBuilder = new StringBuilder("Error retrieving credentials for user."); stringBuilder.append("token id - ").append(tokenId); log.debug(stringBuilder.toString(), e); throw new CredentialStoreException(stringBuilder.toString(), e); } finally { DBUtil.cleanup(preparedStatement, resultSet); } return null; } /**
public CredentialStoreServerHandler() throws ApplicationSettingsException, IllegalAccessException, ClassNotFoundException, InstantiationException { String jdbcUrl = ServerSettings.getCredentialStoreDBURL(); String userName = ServerSettings.getCredentialStoreDBUser(); String password = ServerSettings.getCredentialStoreDBPassword(); String driverName = ServerSettings.getCredentialStoreDBDriver(); log.debug("Starting credential store, connecting to database - " + jdbcUrl + " DB user - " + userName + " driver name - " + driverName); dbUtil = new DBUtil(jdbcUrl, userName, password, driverName); sshCredentialWriter = new SSHCredentialWriter(dbUtil); certificateCredentialWriter = new CertificateCredentialWriter(dbUtil); credentialReader = new CredentialReaderImpl(dbUtil); }
public Connection getConnection() throws Exception { DBUtil dbUtil = getDbUtil (); Connection connection = dbUtil.getConnection(); connection.setAutoCommit(true); return connection; }