cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cohei...@apache.org
Subject [1/2] cxf git commit: Taking advantage of try-with-resources in a few places
Date Wed, 18 Feb 2015 16:37:31 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 2b505b8c8 -> 33083c068


Taking advantage of try-with-resources in a few places


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/ba293c6e
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/ba293c6e
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/ba293c6e

Branch: refs/heads/master
Commit: ba293c6e7456e80a5fe92f25a4893d3e45380bf0
Parents: 2b505b8
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Wed Feb 18 15:08:19 2015 +0000
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Wed Feb 18 15:08:19 2015 +0000

----------------------------------------------------------------------
 .../cxf/bus/spring/BusApplicationContext.java       | 16 ++++++++--------
 .../apache/cxf/common/jaxb/JAXBContextCache.java    |  6 ++----
 .../apache/cxf/binding/corba/utils/OrbConfig.java   | 10 +++++-----
 .../cxf/transport/jms/BackChannelConduit.java       |  5 +----
 .../org/apache/cxf/transport/jms/JMSConduit.java    |  5 +----
 .../org/apache/cxf/transport/jms/util/JMSUtil.java  |  7 +------
 .../cxf/transport/jms/util/ResourceCloser.java      |  2 +-
 .../apache/cxf/transport/jms/util/TestReceiver.java | 10 ++--------
 .../apache/cxf/transport/jms/util/JMSUtilTest.java  |  5 +----
 9 files changed, 22 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java b/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java
index 5f086b7..dc37c2c 100644
--- a/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java
+++ b/core/src/main/java/org/apache/cxf/bus/spring/BusApplicationContext.java
@@ -155,16 +155,16 @@ public class BusApplicationContext extends ClassPathXmlApplicationContext
{
 
                 Resource[] exts = resolver.getResources(DEFAULT_CXF_EXT_CFG_FILE);
                 for (Resource r : exts) {
-                    InputStream is = r.getInputStream();
-                    BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
-                    String line = rd.readLine();
-                    while (line != null) {
-                        if (!"".equals(line)) {
-                            resources.add(resolver.getResource(line));
+                    try (InputStream is = r.getInputStream();
+                        BufferedReader rd = new BufferedReader(new InputStreamReader(is,
"UTF-8"))) {
+                        String line = rd.readLine();
+                        while (line != null) {
+                            if (!"".equals(line)) {
+                                resources.add(resolver.getResource(line));
+                            }
+                            line = rd.readLine();
                         }
-                        line = rd.readLine();
                     }
-                    is.close();
                 }
 
             } catch (IOException ex) {

http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java b/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java
index bb859f4..55bcd78 100644
--- a/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java
+++ b/core/src/main/java/org/apache/cxf/common/jaxb/JAXBContextCache.java
@@ -402,9 +402,8 @@ public final class JAXBContextCache {
         } catch (Exception ex) {
             //ignore
         }
-        try {
-            InputStream ins = loader.getResourceAsStream("/" + pkg.replace('.', '/') + "/jaxb.index");
-            BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
+        try (InputStream ins = loader.getResourceAsStream("/" + pkg.replace('.', '/') + "/jaxb.index");
+            BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "UTF-8")))
{
             if (!StringUtils.isEmpty(pkg)) {
                 pkg += ".";
             }
@@ -426,7 +425,6 @@ public final class JAXBContextCache {
                 line = reader.readLine();
             }
 
-            ins.close();
         } catch (Exception ex) {
             //ignore
         }

http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/utils/OrbConfig.java
----------------------------------------------------------------------
diff --git a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/utils/OrbConfig.java
b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/utils/OrbConfig.java
index 8cbbd48..c814f83 100644
--- a/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/utils/OrbConfig.java
+++ b/rt/bindings/corba/src/main/java/org/apache/cxf/binding/corba/utils/OrbConfig.java
@@ -182,11 +182,11 @@ public class OrbConfig {
         } else {
             f = new File(iorFile);
         }
-        FileOutputStream file = new FileOutputStream(f);
-        PrintWriter out = new PrintWriter(file);
-        out.println(ref);
-        out.flush();
-        file.close();
+        try (FileOutputStream file = new FileOutputStream(f);
+            PrintWriter out = new PrintWriter(file)) {
+            out.println(ref);
+            out.flush();
+        }
     }
 
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/BackChannelConduit.java
----------------------------------------------------------------------
diff --git a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/BackChannelConduit.java
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/BackChannelConduit.java
index 08bb4f3..9403fd3 100644
--- a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/BackChannelConduit.java
+++ b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/BackChannelConduit.java
@@ -111,8 +111,7 @@ class BackChannelConduit extends AbstractConduit implements JMSExchangeSender
{
 
         final Message outMessage = exchange.getOutMessage();
 
-        ResourceCloser closer = new ResourceCloser();
-        try {
+        try (ResourceCloser closer = new ResourceCloser()) {
             Session session = closer
                 .register(connection.createSession(false, Session.AUTO_ACKNOWLEDGE));
 
@@ -163,8 +162,6 @@ class BackChannelConduit extends AbstractConduit implements JMSExchangeSender
{
             sender.sendMessage(session, replyTo, reply);
         } catch (JMSException ex) {
             throw JMSUtil.convertJmsException(ex);
-        } finally {
-            closer.close();
         }
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
----------------------------------------------------------------------
diff --git a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
index 00eba50..1a8afc9 100644
--- a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
+++ b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/JMSConduit.java
@@ -134,8 +134,7 @@ public class JMSConduit extends AbstractConduit implements JMSExchangeSender,
Me
         jmsConfig.ensureProperlyConfigured();        
         assertIsNotTextMessageAndMtom(outMessage);
 
-        ResourceCloser closer = new ResourceCloser();
-        try {
+        try (ResourceCloser closer = new ResourceCloser()) {
             Connection c = getConnection();
             Session session = closer.register(c.createSession(false, 
                                                               Session.AUTO_ACKNOWLEDGE));
@@ -160,8 +159,6 @@ public class JMSConduit extends AbstractConduit implements JMSExchangeSender,
Me
                 // Ignore
             }
             throw JMSUtil.convertJmsException(e);
-        } finally {
-            closer.close();
         }
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JMSUtil.java
----------------------------------------------------------------------
diff --git a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JMSUtil.java
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JMSUtil.java
index 1665f74..634cffa 100644
--- a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JMSUtil.java
+++ b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JMSUtil.java
@@ -46,8 +46,7 @@ public final class JMSUtil {
                                   String correlationId,
                                   long receiveTimeout,
                                   boolean pubSubNoLocal) {
-        ResourceCloser closer = new ResourceCloser();
-        try {
+        try (ResourceCloser closer = new ResourceCloser()) {
             String messageSelector = correlationId == null ? null : "JMSCorrelationID = '"
+ correlationId + "'";
             MessageConsumer consumer = closer.register(session.createConsumer(replyToDestination,
messageSelector,
                                                  pubSubNoLocal));
@@ -59,8 +58,6 @@ public final class JMSUtil {
             return replyMessage;
         } catch (JMSException e) {
             throw convertJmsException(e);
-        } finally {
-            closer.close();
         }
     }
 
@@ -120,8 +117,6 @@ public final class JMSUtil {
             actualNum++;
             messages.nextElement();
         }
-        browser.close();
-        session.close();
         return actualNum;
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/ResourceCloser.java
----------------------------------------------------------------------
diff --git a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/ResourceCloser.java
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/ResourceCloser.java
index da9a41d..40c74f7 100644
--- a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/ResourceCloser.java
+++ b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/ResourceCloser.java
@@ -28,7 +28,7 @@ import javax.jms.MessageProducer;
 import javax.jms.Session;
 import javax.naming.Context;
 
-public class ResourceCloser implements Closeable {
+public class ResourceCloser implements Closeable, AutoCloseable {
     private AbstractSequentialList<Object> resources;
 
     public ResourceCloser() {

http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/TestReceiver.java
----------------------------------------------------------------------
diff --git a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/TestReceiver.java
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/TestReceiver.java
index da09c0d..ee6e3f6 100644
--- a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/TestReceiver.java
+++ b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/TestReceiver.java
@@ -65,8 +65,7 @@ public class TestReceiver {
     }
 
     private void drainQueue() {
-        ResourceCloser closer = new ResourceCloser();
-        try {
+        try (ResourceCloser closer = new ResourceCloser()) {
             Connection connection = closer.register(connectionFactory.createConnection());
             connection.start();
             Session session = closer.register(connection.createSession(false, Session.AUTO_ACKNOWLEDGE));
@@ -77,14 +76,11 @@ public class TestReceiver {
             } while (message != null);
         } catch (JMSException e) {
             throw JMSUtil.convertJmsException(e);
-        } finally {
-            closer.close();
         }
     }
 
     private void receiveAndRespond() {
-        ResourceCloser closer = new ResourceCloser();
-        try {
+        try (ResourceCloser closer = new ResourceCloser()) {
             Connection connection = closer.register(connectionFactory.createConnection());
             connection.start();
             Session session = closer.register(connection.createSession(false, Session.AUTO_ACKNOWLEDGE));
@@ -111,8 +107,6 @@ public class TestReceiver {
             }
         } catch (Throwable e) {
             ex = e;
-        } finally {
-            closer.close();
         }
     }
     

http://git-wip-us.apache.org/repos/asf/cxf/blob/ba293c6e/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/JMSUtilTest.java
----------------------------------------------------------------------
diff --git a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/JMSUtilTest.java
b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/JMSUtilTest.java
index f4bcf74..3dbdab4 100644
--- a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/JMSUtilTest.java
+++ b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/JMSUtilTest.java
@@ -73,15 +73,12 @@ public class JMSUtilTest extends Assert {
         JMSConfiguration jmsConfig = new JMSConfiguration();
         jmsConfig.setConnectionFactory(new ActiveMQConnectionFactory("vm://tesstMarshal?broker.persistent=false"));
         
-        ResourceCloser closer = new ResourceCloser();
-        try {
+        try (ResourceCloser closer = new ResourceCloser()) {
             Connection connection = JMSFactory.createConnection(jmsConfig);
             Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
             javax.jms.Message jmsMessage = 
                 JMSUtil.createAndSetPayload(testBytes, session, JMSConstants.BYTE_MESSAGE_TYPE);
             assertTrue("Message should have been of type BytesMessage ", jmsMessage instanceof
BytesMessage);
-        } finally {
-            closer.close();
         }
         
     }


Mime
View raw message