You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tv...@apache.org on 2018/09/30 15:35:53 UTC
svn commit: r1842396 - in /commons/proper/jcs/trunk/commons-jcs-core/src:
main/java/org/apache/commons/jcs/admin/
main/java/org/apache/commons/jcs/auxiliary/disk/block/
main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/
main/java/org/apache/commons/...
Author: tv
Date: Sun Sep 30 15:35:53 2018
New Revision: 1842396
URL: http://svn.apache.org/viewvc?rev=1842396&view=rev
Log:
Modernize
Modified:
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/admin/JCSAdminBean.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskKeyStore.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySender.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderThread.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryService.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/props/PropertyLoader.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/CompressingSerializer.java
commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/StandardSerializer.java
commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderUnitTest.java
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/admin/JCSAdminBean.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/admin/JCSAdminBean.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/admin/JCSAdminBean.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/admin/JCSAdminBean.java Sun Sep 30 15:35:53 2018
@@ -231,10 +231,8 @@ public class JCSAdminBean implements JCS
//CountingOnlyOutputStream: Keeps track of the number of bytes written to it, but doesn't write them anywhere.
CountingOnlyOutputStream counter = new CountingOnlyOutputStream();
- ObjectOutputStream out = null;
- try
+ try (ObjectOutputStream out = new ObjectOutputStream(counter);)
{
- out = new ObjectOutputStream(counter);
out.writeObject(element);
}
catch (IOException e)
@@ -245,17 +243,6 @@ public class JCSAdminBean implements JCS
{
try
{
- if (out != null)
- {
- out.close();
- }
- }
- catch (IOException e)
- {
- // ignore
- }
- try
- {
counter.close();
}
catch (IOException e)
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskKeyStore.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskKeyStore.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskKeyStore.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/block/BlockDiskKeyStore.java Sun Sep 30 15:35:53 2018
@@ -150,8 +150,8 @@ public class BlockDiskKeyStore<K>
{
FileOutputStream fos = new FileOutputStream(keyFile);
BufferedOutputStream bos = new BufferedOutputStream(fos, 65536);
- ObjectOutputStream oos = new ObjectOutputStream(bos);
- try
+
+ try (ObjectOutputStream oos = new ObjectOutputStream(bos))
{
if (!verify())
{
@@ -168,11 +168,6 @@ public class BlockDiskKeyStore<K>
oos.writeUnshared(descriptor);
}
}
- finally
- {
- oos.flush();
- oos.close();
- }
}
if (log.isInfoEnabled())
@@ -264,8 +259,8 @@ public class BlockDiskKeyStore<K>
{
FileInputStream fis = new FileInputStream(keyFile);
BufferedInputStream bis = new BufferedInputStream(fis, 65536);
- ObjectInputStream ois = new ObjectInputStreamClassLoaderAware(bis, null);
- try
+
+ try (ObjectInputStream ois = new ObjectInputStreamClassLoaderAware(bis, null))
{
while (true)
{
@@ -282,10 +277,6 @@ public class BlockDiskKeyStore<K>
{
// nothing
}
- finally
- {
- ois.close();
- }
}
if (!keys.isEmpty())
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/JDBCDiskCache.java Sun Sep 30 15:35:53 2018
@@ -155,18 +155,7 @@ public class JDBCDiskCache<K, V>
log.debug( "updating, ce = " + ce );
}
- Connection con;
- try
- {
- con = getDataSource().getConnection();
- }
- catch ( SQLException e )
- {
- log.error( "Problem getting connection.", e );
- return;
- }
-
- try
+ try (Connection con = getDataSource().getConnection())
{
if ( log.isDebugEnabled() )
{
@@ -187,16 +176,9 @@ public class JDBCDiskCache<K, V>
insertOrUpdate( ce, con, element );
}
- finally
+ catch ( SQLException e )
{
- try
- {
- con.close();
- }
- catch ( SQLException e )
- {
- log.error( "Problem closing connection.", e );
- }
+ log.error( "Problem getting connection.", e );
}
if ( log.isInfoEnabled() )
@@ -252,16 +234,13 @@ public class JDBCDiskCache<K, V>
private boolean insertRow( ICacheElement<K, V> ce, Connection con, byte[] element )
{
boolean exists = false;
- PreparedStatement psInsert = null;
-
- try
- {
- String sqlI = "insert into "
+ String sqlI = "insert into "
+ getJdbcDiskCacheAttributes().getTableName()
+ " (CACHE_KEY, REGION, ELEMENT, MAX_LIFE_SECONDS, IS_ETERNAL, CREATE_TIME, UPDATE_TIME_SECONDS, SYSTEM_EXPIRE_TIME_SECONDS) "
+ " values (?, ?, ?, ?, ?, ?, ?, ?)";
- psInsert = con.prepareStatement( sqlI );
+ try (PreparedStatement psInsert = con.prepareStatement( sqlI ))
+ {
psInsert.setString( 1, (String) ce.getKey() );
psInsert.setString( 2, this.getCacheName() );
psInsert.setBytes( 3, element );
@@ -302,20 +281,6 @@ public class JDBCDiskCache<K, V>
exists = doesElementExist( ce, con );
}
}
- finally
- {
- if (psInsert != null)
- {
- try
- {
- psInsert.close();
- }
- catch (SQLException e)
- {
- log.error( "Problem closing statement.", e );
- }
- }
- }
return exists;
}
@@ -329,15 +294,12 @@ public class JDBCDiskCache<K, V>
*/
private void updateRow( ICacheElement<K, V> ce, Connection con, byte[] element )
{
- String sqlU = null;
- PreparedStatement psUpdate = null;
-
- try
- {
- sqlU = "update " + getJdbcDiskCacheAttributes().getTableName()
+ String sqlU = "update " + getJdbcDiskCacheAttributes().getTableName()
+ " set ELEMENT = ?, CREATE_TIME = ?, UPDATE_TIME_SECONDS = ?, " + " SYSTEM_EXPIRE_TIME_SECONDS = ? "
+ " where CACHE_KEY = ? and REGION = ?";
- psUpdate = con.prepareStatement( sqlU );
+
+ try (PreparedStatement psUpdate = con.prepareStatement( sqlU ))
+ {
psUpdate.setBytes( 1, element );
Timestamp createTime = new Timestamp( ce.getElementAttributes().getCreateTime() );
@@ -362,20 +324,6 @@ public class JDBCDiskCache<K, V>
{
log.error( "e2 sql [" + sqlU + "] Exception: ", e2 );
}
- finally
- {
- if (psUpdate != null)
- {
- try
- {
- psUpdate.close();
- }
- catch (SQLException e)
- {
- log.error( "Problem closing statement.", e );
- }
- }
- }
}
/**
@@ -388,24 +336,18 @@ public class JDBCDiskCache<K, V>
protected boolean doesElementExist( ICacheElement<K, V> ce, Connection con )
{
boolean exists = false;
- PreparedStatement psSelect = null;
- ResultSet rs = null;
+ // don't select the element, since we want this to be fast.
+ String sqlS = "select CACHE_KEY from " + getJdbcDiskCacheAttributes().getTableName()
+ + " where REGION = ? and CACHE_KEY = ?";
- try
+ try (PreparedStatement psSelect = con.prepareStatement( sqlS ))
{
- // don't select the element, since we want this to be fast.
- String sqlS = "select CACHE_KEY from " + getJdbcDiskCacheAttributes().getTableName()
- + " where REGION = ? and CACHE_KEY = ?";
-
- psSelect = con.prepareStatement( sqlS );
psSelect.setString( 1, this.getCacheName() );
psSelect.setString( 2, (String) ce.getKey() );
- rs = psSelect.executeQuery();
-
- if ( rs.next() )
+ try (ResultSet rs = psSelect.executeQuery())
{
- exists = true;
+ exists = rs.next();
}
if ( log.isDebugEnabled() )
@@ -417,31 +359,6 @@ public class JDBCDiskCache<K, V>
{
log.error( "Problem looking for item before insert.", e );
}
- finally
- {
- try
- {
- if ( rs != null )
- {
- rs.close();
- }
- }
- catch ( SQLException e )
- {
- log.error( "Problem closing result set.", e );
- }
- try
- {
- if ( psSelect != null )
- {
- psSelect.close();
- }
- }
- catch ( SQLException e )
- {
- log.error( "Problem closing statement.", e );
- }
- }
return exists;
}
@@ -477,20 +394,14 @@ public class JDBCDiskCache<K, V>
String selectString = "select ELEMENT from " + getJdbcDiskCacheAttributes().getTableName()
+ " where REGION = ? and CACHE_KEY = ?";
- Connection con = getDataSource().getConnection();
- try
+ try (Connection con = getDataSource().getConnection())
{
- PreparedStatement psSelect = null;
-
- try
+ try (PreparedStatement psSelect = con.prepareStatement( selectString ))
{
- psSelect = con.prepareStatement( selectString );
psSelect.setString( 1, this.getCacheName() );
psSelect.setString( 2, key.toString() );
- ResultSet rs = psSelect.executeQuery();
-
- try
+ try (ResultSet rs = psSelect.executeQuery())
{
if ( rs.next() )
{
@@ -513,27 +424,6 @@ public class JDBCDiskCache<K, V>
}
}
}
- finally
- {
- if ( rs != null )
- {
- rs.close();
- }
- }
- }
- finally
- {
- if ( psSelect != null )
- {
- psSelect.close();
- }
- }
- }
- finally
- {
- if ( con != null )
- {
- con.close();
}
}
}
@@ -583,18 +473,14 @@ public class JDBCDiskCache<K, V>
String selectString = "select CACHE_KEY, ELEMENT from " + getJdbcDiskCacheAttributes().getTableName()
+ " where REGION = ? and CACHE_KEY like ?";
- Connection con = getDataSource().getConnection();
- try
+ try (Connection con = getDataSource().getConnection())
{
- PreparedStatement psSelect = null;
- try
+ try (PreparedStatement psSelect = con.prepareStatement( selectString ))
{
- psSelect = con.prepareStatement( selectString );
psSelect.setString( 1, this.getCacheName() );
psSelect.setString( 2, constructLikeParameterFromPattern( pattern ) );
- ResultSet rs = psSelect.executeQuery();
- try
+ try (ResultSet rs = psSelect.executeQuery())
{
while ( rs.next() )
{
@@ -619,27 +505,6 @@ public class JDBCDiskCache<K, V>
}
}
}
- finally
- {
- if ( rs != null )
- {
- rs.close();
- }
- }
- }
- finally
- {
- if ( psSelect != null )
- {
- psSelect.close();
- }
- }
- }
- finally
- {
- if ( con != null )
- {
- con.close();
}
}
}
@@ -690,7 +555,7 @@ public class JDBCDiskCache<K, V>
String sql = "delete from " + getJdbcDiskCacheAttributes().getTableName()
+ " where REGION = ? and CACHE_KEY = ?";
- try
+ try (Connection con = getDataSource().getConnection())
{
boolean partial = false;
if ( key instanceof String && key.toString().endsWith( CacheConstants.NAME_COMPONENT_DELIMITER ) )
@@ -700,11 +565,9 @@ public class JDBCDiskCache<K, V>
+ " where REGION = ? and CACHE_KEY like ?";
partial = true;
}
- Connection con = getDataSource().getConnection();
- PreparedStatement psSelect = null;
- try
+
+ try (PreparedStatement psSelect = con.prepareStatement( sql ))
{
- psSelect = con.prepareStatement( sql );
psSelect.setString( 1, this.getCacheName() );
if ( partial )
{
@@ -724,21 +587,6 @@ public class JDBCDiskCache<K, V>
log.error( "Problem creating statement. sql [" + sql + "]", e );
setAlive(false);
}
- finally
- {
- try
- {
- if ( psSelect != null )
- {
- psSelect.close();
- }
- con.close();
- }
- catch ( SQLException e1 )
- {
- log.error( "Problem closing statement.", e1 );
- }
- }
}
catch ( SQLException e )
{
@@ -758,14 +606,12 @@ public class JDBCDiskCache<K, V>
// it should never get here from the abstract disk cache.
if ( this.jdbcDiskCacheAttributes.isAllowRemoveAll() )
{
- try
+ try (Connection con = getDataSource().getConnection())
{
String sql = "delete from " + getJdbcDiskCacheAttributes().getTableName() + " where REGION = ?";
- Connection con = getDataSource().getConnection();
- PreparedStatement psDelete = null;
- try
+
+ try (PreparedStatement psDelete = con.prepareStatement( sql ))
{
- psDelete = con.prepareStatement( sql );
psDelete.setString( 1, this.getCacheName() );
setAlive(true);
psDelete.executeUpdate();
@@ -775,21 +621,6 @@ public class JDBCDiskCache<K, V>
log.error( "Problem creating statement.", e );
setAlive(false);
}
- finally
- {
- try
- {
- if ( psDelete != null )
- {
- psDelete.close();
- }
- con.close();
- }
- catch ( SQLException e1 )
- {
- log.error( "Problem closing statement.", e1 );
- }
- }
}
catch ( Exception e )
{
@@ -815,7 +646,7 @@ public class JDBCDiskCache<K, V>
{
int deleted = 0;
- try
+ try (Connection con = getDataSource().getConnection())
{
getTableState().setState( TableState.DELETE_RUNNING );
@@ -830,11 +661,8 @@ public class JDBCDiskCache<K, V>
String sql = "delete from " + getJdbcDiskCacheAttributes().getTableName()
+ " where IS_ETERNAL = ? and REGION = ? and ? > SYSTEM_EXPIRE_TIME_SECONDS";
- Connection con = getDataSource().getConnection();
- PreparedStatement psDelete = null;
- try
+ try (PreparedStatement psDelete = con.prepareStatement( sql ))
{
- psDelete = con.prepareStatement( sql );
psDelete.setString( 1, "F" );
psDelete.setString( 2, this.getCacheName() );
psDelete.setLong( 3, now );
@@ -848,21 +676,7 @@ public class JDBCDiskCache<K, V>
log.error( "Problem creating statement.", e );
setAlive(false);
}
- finally
- {
- try
- {
- if ( psDelete != null )
- {
- psDelete.close();
- }
- con.close();
- }
- catch ( SQLException e1 )
- {
- log.error( "Problem closing statement.", e1 );
- }
- }
+
logApplicationEvent( getAuxiliaryCacheAttributes().getName(), "deleteExpired",
"Deleted expired elements. URL: " + getDiskLocation() );
}
@@ -925,65 +739,26 @@ public class JDBCDiskCache<K, V>
String selectString = "select count(*) from " + getJdbcDiskCacheAttributes().getTableName()
+ " where REGION = ?";
- Connection con;
- try
- {
- con = getDataSource().getConnection();
- }
- catch ( SQLException e )
- {
- log.error( "Problem getting connection.", e );
- return size;
- }
-
- try
+ try (Connection con = getDataSource().getConnection())
{
- PreparedStatement psSelect = null;
- try
+ try (PreparedStatement psSelect = con.prepareStatement( selectString ))
{
- psSelect = con.prepareStatement( selectString );
psSelect.setString( 1, this.getCacheName() );
- ResultSet rs = null;
- rs = psSelect.executeQuery();
- try
+ try (ResultSet rs = psSelect.executeQuery())
{
if ( rs.next() )
{
size = rs.getInt( 1 );
}
}
- finally
- {
- if ( rs != null )
- {
- rs.close();
- }
- }
- }
- finally
- {
- if ( psSelect != null )
- {
- psSelect.close();
- }
}
}
catch ( SQLException e )
{
log.error( "Problem getting size.", e );
}
- finally
- {
- try
- {
- con.close();
- }
- catch ( SQLException e )
- {
- log.error( "Problem closing connection.", e );
- }
- }
+
return size;
}
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/hsql/HSQLDiskCacheFactory.java Sun Sep 30 15:35:53 2018
@@ -161,9 +161,7 @@ public class HSQLDiskCacheFactory
createSql.append( "PRIMARY KEY (CACHE_KEY, REGION) " );
createSql.append( ");" );
- Statement sStatement = cConn.createStatement();
-
- try
+ try (Statement sStatement = cConn.createStatement())
{
sStatement.execute( createSql.toString() );
}
@@ -174,9 +172,5 @@ public class HSQLDiskCacheFactory
throw e;
}
}
- finally
- {
- sStatement.close();
- }
}
}
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/mysql/MySQLTableOptimizer.java Sun Sep 30 15:35:53 2018
@@ -133,25 +133,12 @@ public class MySQLTableOptimizer
log.info( "Optimizing table [" + this.getTableName() + "]" );
}
- Connection con;
- try
- {
- con = dataSource.getConnection();
- }
- catch ( SQLException e )
- {
- log.error( "Problem getting connection.", e );
- return false;
- }
-
- try
+ try (Connection con = dataSource.getConnection())
{
// TEST
- Statement sStatement = null;
- try
- {
- sStatement = con.createStatement();
+ try (Statement sStatement = con.createStatement())
+ {
ResultSet rs = sStatement.executeQuery( "optimize table " + this.getTableName() );
// first row is error, then status
@@ -195,31 +182,10 @@ public class MySQLTableOptimizer
log.error( "Problem optimizing table [" + this.getTableName() + "]", e );
return false;
}
- finally
- {
- if (sStatement != null)
- {
- try
- {
- sStatement.close();
- }
- catch ( SQLException e )
- {
- log.error( "Problem closing statement.", e );
- }
- }
- }
}
- finally
+ catch ( SQLException e )
{
- try
- {
- con.close();
- }
- catch ( SQLException e )
- {
- log.error( "Problem closing connection.", e );
- }
+ log.error( "Problem getting connection.", e );
}
}
finally
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPListener.java Sun Sep 30 15:35:53 2018
@@ -503,7 +503,7 @@ public class LateralTCPListener<K, V>
@Override
public void run()
{
- try
+ try (ServerSocket ssck = serverSocket)
{
ConnectionHandler handler;
@@ -529,7 +529,7 @@ public class LateralTCPListener<K, V>
try
{
- socket = serverSocket.accept();
+ socket = ssck.accept();
break inner;
}
catch (SocketTimeoutException e)
@@ -553,20 +553,6 @@ public class LateralTCPListener<K, V>
{
log.error( "Exception caught in TCP listener", e );
}
- finally
- {
- if (serverSocket != null)
- {
- try
- {
- serverSocket.close();
- }
- catch (IOException e)
- {
- log.error( "Exception caught closing socket", e );
- }
- }
- }
}
}
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/lateral/socket/tcp/LateralTCPSender.java Sun Sep 30 15:35:53 2018
@@ -228,12 +228,10 @@ public class LateralTCPSender
// write object to listener
oos.writeUnshared( led );
oos.flush();
- ObjectInputStream ois = null;
- try
+ try (ObjectInputStream ois = new ObjectInputStreamClassLoaderAware( socket.getInputStream(), null ))
{
socket.setSoTimeout( socketSoTimeOut );
- ois = new ObjectInputStreamClassLoaderAware( socket.getInputStream(), null );
response = ois.readObject();
}
catch ( IOException ioe )
@@ -248,13 +246,6 @@ public class LateralTCPSender
{
log.error( e );
}
- finally
- {
- if (ois != null)
- {
- ois.close();
- }
- }
}
return response;
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java Sun Sep 30 15:35:53 2018
@@ -93,7 +93,7 @@ public class UDPDiscoveryReceiver
// create a small thread pool to handle a barrage
pooledExecutor = ThreadPoolManager.getInstance().createPool(
- new PoolConfiguration(false, 0, maxPoolSize, maxPoolSize, 0, WhenBlockedPolicy.DISCARDOLDEST, maxPoolSize),
+ new PoolConfiguration(false, 0, maxPoolSize, maxPoolSize, 0, WhenBlockedPolicy.DISCARDOLDEST, maxPoolSize),
"JCS-UDPDiscoveryReceiver-", Thread.MIN_PRIORITY);
if ( log.isInfoEnabled() )
@@ -150,7 +150,6 @@ public class UDPDiscoveryReceiver
throws IOException
{
final DatagramPacket packet = new DatagramPacket( mBuffer, mBuffer.length );
- ObjectInputStream objectStream = null;
Object obj = null;
try
{
@@ -167,8 +166,11 @@ public class UDPDiscoveryReceiver
}
final ByteArrayInputStream byteStream = new ByteArrayInputStream( mBuffer, 0, packet.getLength() );
- objectStream = new ObjectInputStreamClassLoaderAware( byteStream, null );
- obj = objectStream.readObject();
+
+ try (ObjectInputStream objectStream = new ObjectInputStreamClassLoaderAware( byteStream, null ))
+ {
+ obj = objectStream.readObject();
+ }
if ( obj instanceof UDPDiscoveryMessage )
{
@@ -188,20 +190,7 @@ public class UDPDiscoveryReceiver
{
log.error( "Error receiving multicast packet", e );
}
- finally
- {
- if (objectStream != null)
- {
- try
- {
- objectStream.close();
- }
- catch (IOException e)
- {
- log.error( "Error closing object stream", e );
- }
- }
- }
+
return obj;
}
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySender.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySender.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySender.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySender.java Sun Sep 30 15:35:53 2018
@@ -1,5 +1,12 @@
package org.apache.commons.jcs.utils.discovery;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.InetAddress;
+import java.net.MulticastSocket;
+import java.util.ArrayList;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -25,19 +32,12 @@ import org.apache.commons.jcs.utils.seri
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.net.DatagramPacket;
-import java.net.InetAddress;
-import java.net.MulticastSocket;
-import java.util.ArrayList;
-
/**
* This is a generic sender for the UDPDiscovery process.
* <p>
* @author Aaron Smuts
*/
-public class UDPDiscoverySender
+public class UDPDiscoverySender implements AutoCloseable
{
/** The logger. */
private static final Log log = LogFactory.getLog( UDPDiscoverySender.class );
@@ -92,7 +92,8 @@ public class UDPDiscoverySender
/**
* Closes the socket connection.
*/
- public void destroy()
+ @Override
+ public void close()
{
try
{
@@ -103,7 +104,7 @@ public class UDPDiscoverySender
}
catch ( Exception e )
{
- log.error( "Problem destrying sender", e );
+ log.error( "Problem closing sender", e );
}
}
@@ -117,7 +118,7 @@ public class UDPDiscoverySender
throws Throwable
{
super.finalize();
- destroy();
+ close();
}
/**
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderThread.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderThread.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderThread.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderThread.java Sun Sep 30 15:35:53 2018
@@ -1,5 +1,8 @@
package org.apache.commons.jcs.utils.discovery;
+import java.io.IOException;
+import java.util.ArrayList;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -22,8 +25,6 @@ package org.apache.commons.jcs.utils.dis
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
-import java.util.ArrayList;
-
/**
* Used to periodically broadcast our location to other caches that might be listening.
*/
@@ -81,11 +82,10 @@ public class UDPDiscoverySenderThread
+ attributes.getServiceAddress() + "] and port = [" + attributes.getServicePort() + "]" );
}
- UDPDiscoverySender sender = null;
- try
+ try (UDPDiscoverySender sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(),
+ attributes.getUdpDiscoveryPort() ))
{
// move this to the run method and determine how often to call it.
- sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes.getUdpDiscoveryPort() );
sender.requestBroadcast();
if ( log.isDebugEnabled() )
@@ -93,24 +93,10 @@ public class UDPDiscoverySenderThread
log.debug( "Sent a request broadcast to the group" );
}
}
- catch ( Exception e )
+ catch ( IOException e )
{
log.error( "Problem sending a Request Broadcast", e );
}
- finally
- {
- try
- {
- if ( sender != null )
- {
- sender.destroy();
- }
- }
- catch ( Exception e )
- {
- log.error( "Problem closing Request Broadcast sender", e );
- }
- }
}
/**
@@ -119,13 +105,11 @@ public class UDPDiscoverySenderThread
@Override
public void run()
{
- UDPDiscoverySender sender = null;
- try
+ // create this connection each time.
+ // more robust
+ try (UDPDiscoverySender sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(),
+ attributes.getUdpDiscoveryPort() ))
{
- // create this connection each time.
- // more robust
- sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes.getUdpDiscoveryPort() );
-
sender.passiveBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames );
// todo we should consider sending a request broadcast every so
@@ -135,27 +119,12 @@ public class UDPDiscoverySenderThread
{
log.debug( "Called sender to issue a passive broadcast" );
}
-
}
- catch ( Exception e )
+ catch ( IOException e )
{
log.error( "Problem calling the UDP Discovery Sender [" + attributes.getUdpDiscoveryAddr() + ":"
+ attributes.getUdpDiscoveryPort() + "]", e );
}
- finally
- {
- if (sender != null)
- {
- try
- {
- sender.destroy();
- }
- catch ( Exception e )
- {
- log.error( "Problem closing Passive Broadcast sender", e );
- }
- }
- }
}
/**
@@ -163,13 +132,11 @@ public class UDPDiscoverySenderThread
*/
protected void shutdown()
{
- UDPDiscoverySender sender = null;
- try
+ // create this connection each time.
+ // more robust
+ try (UDPDiscoverySender sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(),
+ attributes.getUdpDiscoveryPort() ))
{
- // create this connection each time.
- // more robust
- sender = new UDPDiscoverySender( attributes.getUdpDiscoveryAddr(), attributes.getUdpDiscoveryPort() );
-
sender.removeBroadcast( attributes.getServiceAddress(), attributes.getServicePort(), cacheNames );
if ( log.isDebugEnabled() )
@@ -177,23 +144,9 @@ public class UDPDiscoverySenderThread
log.debug( "Called sender to issue a remove broadcast in shudown." );
}
}
- catch ( Exception e )
+ catch ( IOException e )
{
log.error( "Problem calling the UDP Discovery Sender", e );
}
- finally
- {
- try
- {
- if ( sender != null )
- {
- sender.destroy();
- }
- }
- catch ( Exception e )
- {
- log.error( "Problem closing Remote Broadcast sender", e );
- }
- }
}
}
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryService.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryService.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryService.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryService.java Sun Sep 30 15:35:53 2018
@@ -79,7 +79,7 @@ public class UDPDiscoveryService
*/
public UDPDiscoveryService( UDPDiscoveryAttributes attributes)
{
- udpDiscoveryAttributes = (UDPDiscoveryAttributes) attributes.clone();
+ udpDiscoveryAttributes = attributes.clone();
try
{
@@ -135,15 +135,13 @@ public class UDPDiscoveryService
*/
protected void serviceRequestBroadcast()
{
- UDPDiscoverySender sender1 = null;
- try
+ // create this connection each time.
+ // more robust
+ try (UDPDiscoverySender sender = new UDPDiscoverySender(
+ getUdpDiscoveryAttributes().getUdpDiscoveryAddr(),
+ getUdpDiscoveryAttributes().getUdpDiscoveryPort() ))
{
- // create this connection each time.
- // more robust
- sender1 = new UDPDiscoverySender( getUdpDiscoveryAttributes().getUdpDiscoveryAddr(),
- getUdpDiscoveryAttributes().getUdpDiscoveryPort() );
-
- sender1.passiveBroadcast( getUdpDiscoveryAttributes().getServiceAddress(), getUdpDiscoveryAttributes()
+ sender.passiveBroadcast( getUdpDiscoveryAttributes().getServiceAddress(), getUdpDiscoveryAttributes()
.getServicePort(), this.getCacheNames() );
// todo we should consider sending a request broadcast every so
@@ -154,26 +152,12 @@ public class UDPDiscoveryService
log.debug( "Called sender to issue a passive broadcast" );
}
}
- catch ( Exception e )
+ catch ( IOException e )
{
log.error( "Problem calling the UDP Discovery Sender. address ["
+ getUdpDiscoveryAttributes().getUdpDiscoveryAddr() + "] port ["
+ getUdpDiscoveryAttributes().getUdpDiscoveryPort() + "]", e );
}
- finally
- {
- try
- {
- if ( sender1 != null )
- {
- sender1.destroy();
- }
- }
- catch ( Exception e )
- {
- log.error( "Problem closing Passive Broadcast sender, while servicing a request broadcast.", e );
- }
- }
}
/**
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/props/PropertyLoader.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/props/PropertyLoader.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/props/PropertyLoader.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/props/PropertyLoader.java Sun Sep 30 15:35:53 2018
@@ -1,5 +1,7 @@
package org.apache.commons.jcs.utils.props;
+import java.io.IOException;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -83,7 +85,9 @@ public abstract class PropertyLoader
boolean isCCFSuffix = true;
if ( name == null )
+ {
throw new IllegalArgumentException( "null input: name" );
+ }
ClassLoader classLoader = ( loader == null ) ? ClassLoader.getSystemClassLoader() : loader;
@@ -100,45 +104,27 @@ public abstract class PropertyLoader
isCCFSuffix = false;
}
- Properties result = null;
+ fileName = fileName.replace( '.', '/' );
- InputStream in = null;
- try
+ if ( !fileName.endsWith( SUFFIX ) && isCCFSuffix )
{
- fileName = fileName.replace( '.', '/' );
-
- if ( !fileName.endsWith( SUFFIX ) && isCCFSuffix )
- {
- fileName = fileName.concat( SUFFIX );
- }
- else if ( !fileName.endsWith( SUFFIX_PROPERTIES ) && !isCCFSuffix )
- {
- fileName = fileName.concat( SUFFIX_PROPERTIES );
- }
-
- // returns null on lookup failures:
- in = classLoader.getResourceAsStream( fileName );
- if ( in != null )
- {
- result = new Properties();
- result.load( in ); // can throw IOException
- }
+ fileName = fileName.concat( SUFFIX );
}
- catch ( Exception e )
+ else if ( !fileName.endsWith( SUFFIX_PROPERTIES ) && !isCCFSuffix )
{
- result = null;
+ fileName = fileName.concat( SUFFIX_PROPERTIES );
}
- finally
+
+ Properties result = null;
+
+ try (InputStream in = classLoader.getResourceAsStream( fileName ))
{
- if ( in != null )
- try
- {
- in.close();
- }
- catch ( Throwable ignore )
- {
- // swallow
- }
+ result = new Properties();
+ result.load( in ); // can throw IOException
+ }
+ catch ( IOException e )
+ {
+ result = null;
}
if ( THROW_ON_LOAD_FAILURE && result == null )
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/CompressingSerializer.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/CompressingSerializer.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/CompressingSerializer.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/CompressingSerializer.java Sun Sep 30 15:35:53 2018
@@ -63,17 +63,13 @@ public class CompressingSerializer
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream( baos );
- try
+
+ try (ObjectOutputStream oos = new ObjectOutputStream( baos ))
{
oos.writeObject( obj );
}
- finally
- {
- oos.close();
- }
- byte[] uncompressed = baos.toByteArray();
- return uncompressed;
+
+ return baos.toByteArray();
}
/**
@@ -109,18 +105,12 @@ public class CompressingSerializer
throws IOException, ClassNotFoundException
{
ByteArrayInputStream bais = new ByteArrayInputStream( decompressedByteArray );
- BufferedInputStream bis = new BufferedInputStream( bais );
- ObjectInputStream ois = new ObjectInputStreamClassLoaderAware( bis, null );
- try
+ try (ObjectInputStream ois = new ObjectInputStreamClassLoaderAware( bais, null ))
{
@SuppressWarnings("unchecked") // Need to cast from Object
T readObject = (T) ois.readObject();
return readObject;
}
- finally
- {
- ois.close();
- }
}
}
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/StandardSerializer.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/StandardSerializer.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/StandardSerializer.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/serialization/StandardSerializer.java Sun Sep 30 15:35:53 2018
@@ -49,15 +49,12 @@ public class StandardSerializer
throws IOException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
- ObjectOutputStream oos = new ObjectOutputStream( baos );
- try
+
+ try (ObjectOutputStream oos = new ObjectOutputStream( baos ))
{
oos.writeObject( obj );
}
- finally
- {
- oos.close();
- }
+
return baos.toByteArray();
}
@@ -75,17 +72,12 @@ public class StandardSerializer
throws IOException, ClassNotFoundException
{
ByteArrayInputStream bais = new ByteArrayInputStream( data );
- BufferedInputStream bis = new BufferedInputStream( bais );
- ObjectInputStream ois = new ObjectInputStreamClassLoaderAware( bis, loader );
- try
+
+ try (ObjectInputStream ois = new ObjectInputStreamClassLoaderAware( bais, loader ))
{
@SuppressWarnings("unchecked") // Need to cast from Object
T readObject = (T) ois.readObject();
return readObject;
}
- finally
- {
- ois.close();
- }
}
}
Modified: commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderUnitTest.java
URL: http://svn.apache.org/viewvc/commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderUnitTest.java?rev=1842396&r1=1842395&r2=1842396&view=diff
==============================================================================
--- commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderUnitTest.java (original)
+++ commons/proper/jcs/trunk/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoverySenderUnitTest.java Sun Sep 30 15:35:53 2018
@@ -1,5 +1,9 @@
package org.apache.commons.jcs.utils.discovery;
+import java.util.ArrayList;
+
+import org.apache.commons.jcs.utils.discovery.UDPDiscoveryMessage.BroadcastType;
+
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -20,9 +24,6 @@ package org.apache.commons.jcs.utils.dis
*/
import junit.framework.TestCase;
-import org.apache.commons.jcs.utils.discovery.UDPDiscoveryMessage.BroadcastType;
-
-import java.util.ArrayList;
/**
* Tests for the sender.
@@ -73,7 +74,7 @@ public class UDPDiscoverySenderUnitTest
throws Exception
{
receiver.shutdown();
- sender.destroy();
+ sender.close();
super.tearDown();
}