Skip to content

Commit

Permalink
Move database interactions to using try with resources
Browse files Browse the repository at this point in the history
  • Loading branch information
alsutton committed Sep 16, 2018
1 parent 656ab53 commit 747e428
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 1,098 deletions.
@@ -1,7 +1,5 @@
package com.enterprisepasswordsafe.engine.database;

import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
Expand Down Expand Up @@ -36,8 +34,7 @@ public T getByIds(final String itemId, final String actorId)

public Map<String,String> getAllForItem(final String id)
throws SQLException {
Map<String,String> results = new HashMap<String,String>();

Map<String,String> results = new HashMap<>();
try(PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(getAllSql)) {
ps.setString(1, id);
try(ResultSet rs = ps.executeQuery()) {
Expand All @@ -54,16 +51,12 @@ public Map<String,String> getAllForItem(final String id)

public void delete(final String itemId, final String actorId, final String role)
throws SQLException {
PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(deleteSql);
try {
try(PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(deleteSql)) {
ps.setString(1, itemId);
ps.setString(2, actorId);
ps.setString(3, role);
ps.executeUpdate();
} finally {
DatabaseConnectionUtils.close(ps);
}

}

}
Expand Up @@ -37,7 +37,6 @@

import com.enterprisepasswordsafe.engine.AccessControlDecryptor;
import com.enterprisepasswordsafe.engine.utils.Constants;
import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;
import com.enterprisepasswordsafe.engine.utils.KeyUtils;
import com.enterprisepasswordsafe.proguard.JavaBean;

Expand Down
Expand Up @@ -26,8 +26,6 @@
import java.util.ArrayList;
import java.util.List;

import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;


/**
* Summary class. Holds only the source name and ID
Expand Down
Expand Up @@ -16,23 +16,20 @@

package com.enterprisepasswordsafe.engine.database;

import com.enterprisepasswordsafe.engine.configuration.JDBCConfiguration;
import com.enterprisepasswordsafe.engine.database.exceptions.DatabaseUnavailableException;
import com.enterprisepasswordsafe.engine.dbabstraction.DALFactory;
import com.enterprisepasswordsafe.engine.dbabstraction.DALInterface;

import java.security.GeneralSecurityException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.enterprisepasswordsafe.engine.configuration.JDBCConfiguration;
import com.enterprisepasswordsafe.engine.database.exceptions.DatabaseUnavailableException;
import com.enterprisepasswordsafe.engine.dbabstraction.DALFactory;
import com.enterprisepasswordsafe.engine.dbabstraction.DALInterface;
import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;


/**
* Class managing all the business object which may be needed to service the EPS.
Expand Down Expand Up @@ -94,7 +91,7 @@ private void commitAndCloseConnection() {
connection.commit();
}
} finally {
DatabaseConnectionUtils.close(connection);
connection.close();
}
} catch(Exception ex) {
Logger.getAnonymousLogger().log(Level.WARNING, "Error commiting data on BOM close", ex);
Expand Down
Expand Up @@ -17,12 +17,8 @@
package com.enterprisepasswordsafe.engine.database;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;
import com.enterprisepasswordsafe.proguard.ExternalInterface;

public final class GroupAccessRoleDAO
Expand Down
@@ -1,7 +1,5 @@
package com.enterprisepasswordsafe.engine.database;

import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
import java.sql.PreparedStatement;
Expand Down
Expand Up @@ -27,7 +27,6 @@
import java.util.TreeSet;

import com.enterprisepasswordsafe.engine.database.derived.UserSummary;
import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;
import com.enterprisepasswordsafe.proguard.ExternalInterface;

/**
Expand Down
Expand Up @@ -29,12 +29,9 @@
import com.enterprisepasswordsafe.engine.database.actions.NodeObjectAction;
import com.enterprisepasswordsafe.engine.database.derived.HierarchyNodeChildren;
import com.enterprisepasswordsafe.engine.database.derived.HierarchyNodeSummary;
import com.enterprisepasswordsafe.engine.database.derived.UserSummary;
import com.enterprisepasswordsafe.engine.users.UserClassifier;
import com.enterprisepasswordsafe.engine.utils.Cache;
import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;
import com.enterprisepasswordsafe.proguard.ExternalInterface;
import com.enterprisepasswordsafe.proguard.JavaBean;

/**
* Data access object for nodes in the hierarchy.
Expand Down
Expand Up @@ -3,7 +3,6 @@
import com.enterprisepasswordsafe.engine.database.derived.UserSummary;
import com.enterprisepasswordsafe.engine.nodes.GroupNodeDefaultPermission;
import com.enterprisepasswordsafe.engine.nodes.UserNodeDefaultPermission;
import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;

import java.io.UnsupportedEncodingException;
import java.security.GeneralSecurityException;
Expand Down
Expand Up @@ -23,7 +23,6 @@
import java.sql.ResultSet;
import java.sql.SQLException;

import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;
import com.enterprisepasswordsafe.engine.utils.DateFormatter;
import com.enterprisepasswordsafe.engine.utils.PasswordUtils;
import com.enterprisepasswordsafe.proguard.ExternalInterface;
Expand Down
129 changes: 17 additions & 112 deletions src/main/java/com/enterprisepasswordsafe/engine/database/IPZoneDAO.java
Expand Up @@ -16,193 +16,98 @@

package com.enterprisepasswordsafe.engine.database;

import com.enterprisepasswordsafe.proguard.ExternalInterface;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;
import com.enterprisepasswordsafe.proguard.ExternalInterface;

/**
* Data access object for the IPZone objects.
*/

public class IPZoneDAO
implements ExternalInterface {

/**
* Get all defined IP Zones
*/

private static final String GET_ZONES =
"SELECT ip_zone_id, name, ip_version, ip_start, ip_end "
+ " FROM ip_zones "
+ " ORDER BY name ";

/**
* SQL to get an IP Zone by it's ID
*/
"SELECT ip_zone_id, name, ip_version, ip_start, ip_end FROM ip_zones ORDER BY name ";

private static final String GET_ZONE_BY_ID =
"SELECT ip_zone_id, name, ip_version, ip_start, ip_end "
+ " FROM ip_zones "
+ " WHERE ip_zone_id = ? ";

/**
* SQL to store the IP zone
*/
"SELECT ip_zone_id, name, ip_version, ip_start, ip_end FROM ip_zones WHERE ip_zone_id = ? ";

private static final String STORE_ZONE =
"INSERT INTO ip_zones(ip_zone_id, name, ip_version, ip_start, ip_end) "
+ " VALUES ( ?, ?, ?, ?, ?)";

/**
* SQL to update the IP Zone
*/
"INSERT INTO ip_zones(ip_zone_id, name, ip_version, ip_start, ip_end) VALUES( ?, ?, ?, ?, ?)";

private static final String UPDATE_ZONE =
"UPDATE ip_zones "+
" SET name = ?, ip_start = ?, ip_end = ? "+
" WHERE ip_zone_id = ?";

/**
* SQL to delete a zone.
*/
"UPDATE ip_zones SET name = ?, ip_start = ?, ip_end = ? WHERE ip_zone_id = ?";

private static final String DELETE_ZONE =
"DELETE FROM ip_zones WHERE ip_zone_id = ?";

/**
* Private constructor to prevent instantiation
*/

private IPZoneDAO() {
super();
}

/**
* Create a new IP Zone.
*/

public IPZone create( String name, int version, String firstIp, String lastIp )
throws SQLException {
IPZone newZone = new IPZone(name, version, firstIp, lastIp);
store(newZone);
return newZone;
}

/**
* Store this zone in the database
*
* @param zone The zone to store.
*/

public void store( final IPZone zone )
throws SQLException {
PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(STORE_ZONE);
try {
try(PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(STORE_ZONE)) {
int idx = 1;
ps.setString(idx++, zone.getId());
ps.setString(idx++, zone.getName());
ps.setInt (idx++, zone.getIpVersion());
ps.setString(idx++, zone.getStartIp());
ps.setString(idx, zone.getEndIp());
ps.executeUpdate();
} finally {
DatabaseConnectionUtils.close(ps);
}
}

/**
* Update the data stored in the database.
*
* @param zone The zone to update.
*/

public void update( final IPZone zone)
throws SQLException {
PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(UPDATE_ZONE);
try {
try(PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(UPDATE_ZONE)) {
int idx = 1;
ps.setString(idx++, zone.getName());
ps.setString(idx++, zone.getStartIp());
ps.setString(idx++, zone.getEndIp());
ps.setString(idx, zone.getId());
ps.executeUpdate();
} finally {
DatabaseConnectionUtils.close(ps);
}
}

/**
* Delete this zone from the database
*
* @param zone The zone to delete
*/

public void delete( final IPZone zone )
throws SQLException {
PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(DELETE_ZONE);
try {
try(PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(DELETE_ZONE)) {
ps.setString(1, zone.getId());
ps.executeUpdate();
} finally {
DatabaseConnectionUtils.close(ps);
}
}

/**
* Get a specific zone.
*
* @param id The ID of the zone to get.
*
* @return The requested zone or null if the zone does not exist.
*/

public IPZone getById( final String id )
throws SQLException {
PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(GET_ZONE_BY_ID);
ResultSet rs = null;
try {
try(PreparedStatement ps = BOMFactory.getCurrentConntection().prepareStatement(GET_ZONE_BY_ID)) {
ps.setString(1, id);
ps.setMaxRows(1);
rs = ps.executeQuery();
if(!rs.next()) {
return null;
try(ResultSet rs = ps.executeQuery()) {
return rs.next() ? new IPZone(rs) : null;
}

return new IPZone(rs);
} finally {
DatabaseConnectionUtils.close(rs);
DatabaseConnectionUtils.close(ps);
}
}

/**
* Get all of the defined zones.
*
* @return The list of zones defined.
*/

public List<IPZone> getAll( )
throws SQLException {
List<IPZone> zones = new ArrayList<IPZone>();

Statement stmt = BOMFactory.getCurrentConntection().createStatement();
ResultSet rs = null;
try {
rs = stmt.executeQuery(GET_ZONES);
while( rs.next() ) {
zones.add(new IPZone(rs));
try(Statement stmt = BOMFactory.getCurrentConntection().createStatement()) {
try(ResultSet rs = stmt.executeQuery(GET_ZONES)) {
while (rs.next()) {
zones.add(new IPZone(rs));
}
}
} finally {
DatabaseConnectionUtils.close(rs);
DatabaseConnectionUtils.close(stmt);
}

return zones;
}

Expand Down
Expand Up @@ -22,7 +22,6 @@
import java.util.HashMap;
import java.util.Map;

import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;
import com.enterprisepasswordsafe.proguard.ExternalInterface;

public final class IntegrationModuleConfigurationDAO implements ExternalInterface {
Expand Down
Expand Up @@ -25,7 +25,6 @@

import com.enterprisepasswordsafe.engine.integration.PasswordChanger;
import com.enterprisepasswordsafe.engine.integration.PasswordChangerProperty;
import com.enterprisepasswordsafe.engine.utils.DatabaseConnectionUtils;
import com.enterprisepasswordsafe.proguard.ExternalInterface;

/**
Expand Down

0 comments on commit 747e428

Please sign in to comment.