Catch SocketTimeoutException

git-svn-id: https://svn.apache.org/repos/asf/openejb/trunk@1424553 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/openejb/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastSearch.java b/openejb/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastSearch.java
index 5beef03..4744601 100644
--- a/openejb/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastSearch.java
+++ b/openejb/server/openejb-client/src/main/java/org/apache/openejb/client/MulticastSearch.java
@@ -40,15 +40,15 @@
         this("239.255.3.2", 6142);
     }
 
-    public MulticastSearch(String host, int port) throws IOException {
-        InetAddress inetAddress = InetAddress.getByName(host);
+    public MulticastSearch(final String host, final int port) throws IOException {
+        final InetAddress inetAddress = InetAddress.getByName(host);
 
         multicast = new MulticastSocket(port);
         multicast.joinGroup(inetAddress);
         multicast.setSoTimeout(500);
     }
 
-    public URI search(int timeout, TimeUnit milliseconds) throws IOException {
+    public URI search(final int timeout, final TimeUnit milliseconds) throws IOException {
         return search(new DefaultFilter(), timeout, milliseconds);
     }
 
@@ -56,25 +56,26 @@
         return search(new DefaultFilter(), 0, TimeUnit.MILLISECONDS);
     }
 
-    public URI search(Filter filter) throws IOException {
+    public URI search(final Filter filter) throws IOException {
         return search(filter, 0, TimeUnit.MILLISECONDS);
     }
 
-    public URI search(Filter filter, long timeout, TimeUnit unit) throws IOException {
+    @SuppressWarnings("UseOfSystemOutOrSystemErr")
+    public URI search(final Filter filter, long timeout, final TimeUnit unit) throws IOException {
         timeout = TimeUnit.MILLISECONDS.convert(timeout, unit);
         long waited = 0;
 
-        byte[] buf = new byte[BUFF_SIZE];
-        DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
+        final byte[] buf = new byte[BUFF_SIZE];
+        final DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
 
         while (timeout == 0 || waited < timeout) {
-            long start = System.currentTimeMillis();
+            final long start = System.currentTimeMillis();
             try {
                 multicast.receive(packet);
                 if (packet.getLength() > 0) {
-                    String str = new String(packet.getData(), packet.getOffset(), packet.getLength());
+                    final String str = new String(packet.getData(), packet.getOffset(), packet.getLength());
                     try {
-                        URI service = new URI(str);
+                        final URI service = new URI(str);
                         if (service != null && filter.accept(service)) {
 
                             final String callerHost = ((InetSocketAddress) packet.getSocketAddress()).getAddress().getHostAddress();
@@ -98,7 +99,7 @@
             } catch (SocketException e) {
                 System.out.println(e.getClass().getName() + ": " + e.getMessage());
             } finally {
-                long stop = System.currentTimeMillis();
+                final long stop = System.currentTimeMillis();
                 waited += stop - start;
             }
         }
@@ -112,7 +113,7 @@
 
     public static class DefaultFilter implements Filter {
         @Override
-        public boolean accept(URI service) {
+        public boolean accept(final URI service) {
             return true;
         }
     }
diff --git a/openejb/server/openejb-multicast/src/main/java/org/apache/openejb/server/discovery/MulticastSearch.java b/openejb/server/openejb-multicast/src/main/java/org/apache/openejb/server/discovery/MulticastSearch.java
index 2faf0fc..bdc4c30 100644
--- a/openejb/server/openejb-multicast/src/main/java/org/apache/openejb/server/discovery/MulticastSearch.java
+++ b/openejb/server/openejb-multicast/src/main/java/org/apache/openejb/server/discovery/MulticastSearch.java
@@ -21,6 +21,7 @@
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.MulticastSocket;
+import java.net.SocketTimeoutException;
 import java.net.URI;
 import java.util.concurrent.TimeUnit;
 
@@ -37,15 +38,15 @@
         this("239.255.3.2", 6142);
     }
 
-    public MulticastSearch(String host, int port) throws IOException {
-        InetAddress inetAddress = InetAddress.getByName(host);
+    public MulticastSearch(final String host, final int port) throws IOException {
+        final InetAddress inetAddress = InetAddress.getByName(host);
 
         multicast = new MulticastSocket(port);
         multicast.joinGroup(inetAddress);
         multicast.setSoTimeout(500);
     }
 
-    public URI search(int timeout, TimeUnit milliseconds) throws IOException {
+    public URI search(final int timeout, final TimeUnit milliseconds) throws IOException {
         return search(new DefaultFilter(), timeout, milliseconds);
     }
 
@@ -53,24 +54,24 @@
         return search(new DefaultFilter(), 0, TimeUnit.MILLISECONDS);
     }
 
-    public URI search(Filter filter) throws IOException {
+    public URI search(final Filter filter) throws IOException {
         return search(filter, 0, TimeUnit.MILLISECONDS);
     }
 
-    public URI search(Filter filter, long timeout, TimeUnit unit) throws IOException {
+    public URI search(final Filter filter, long timeout, final TimeUnit unit) throws IOException {
         timeout = TimeUnit.MILLISECONDS.convert(timeout, unit);
         long waited = 0;
 
-        byte[] buf = new byte[BUFF_SIZE];
-        DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
+        final byte[] buf = new byte[BUFF_SIZE];
+        final DatagramPacket packet = new DatagramPacket(buf, 0, buf.length);
 
         while (timeout == 0 || waited < timeout) {
-            long start = System.currentTimeMillis();
+            final long start = System.currentTimeMillis();
             try {
                 multicast.receive(packet);
                 if (packet.getLength() > 0) {
-                    String str = new String(packet.getData(), packet.getOffset(), packet.getLength());
-                    URI service = URI.create(str);
+                    final String str = new String(packet.getData(), packet.getOffset(), packet.getLength());
+                    final URI service = URI.create(str);
                     if (service != null && filter.accept(service)) {
 
                         final String callerHost = ((InetSocketAddress) packet.getSocketAddress()).getAddress().getHostAddress();
@@ -86,8 +87,10 @@
                         return service;
                     }
                 }
+            } catch (SocketTimeoutException e) {
+                //Ignore
             } finally {
-                long stop = System.currentTimeMillis();
+                final long stop = System.currentTimeMillis();
                 waited += stop - start;
             }
         }
@@ -96,12 +99,14 @@
     }
 
     public interface Filter {
+
         boolean accept(URI service);
     }
 
     public static class DefaultFilter implements Filter {
+
         @Override
-        public boolean accept(URI service) {
+        public boolean accept(final URI service) {
             return true;
         }
     }
diff --git a/openejb/server/openejb-multicast/src/test/java/org/apache/openejb/server/discovery/MulticastDiscoveryAgentTest.java b/openejb/server/openejb-multicast/src/test/java/org/apache/openejb/server/discovery/MulticastDiscoveryAgentTest.java
index 1aeaad6..67cd546 100644
--- a/openejb/server/openejb-multicast/src/test/java/org/apache/openejb/server/discovery/MulticastDiscoveryAgentTest.java
+++ b/openejb/server/openejb-multicast/src/test/java/org/apache/openejb/server/discovery/MulticastDiscoveryAgentTest.java
@@ -17,29 +17,29 @@
 package org.apache.openejb.server.discovery;
 
 import junit.framework.TestCase;
+import org.apache.openejb.server.DiscoveryListener;
+import org.apache.openejb.server.ServiceException;
 
+import java.io.IOException;
 import java.net.URI;
 import java.net.URISyntaxException;
-import java.io.IOException;
 import java.util.HashSet;
 import java.util.Properties;
 import java.util.Set;
 
-import org.apache.openejb.server.ServiceException;
-import org.apache.openejb.server.DiscoveryListener;
-
 /**
  * @version $Rev$ $Date$
  */
+@SuppressWarnings("UseOfSystemOutOrSystemErr")
 public class MulticastDiscoveryAgentTest extends TestCase {
 
     //public void testNothing(){}
 
     public void test() throws Exception {
-        MulticastDiscoveryAgent[] agents = {agent("red"),agent("green"),agent("yellow"),agent("blue")};
+        final MulticastDiscoveryAgent[] agents = {agent("red"), agent("green"), agent("yellow"), agent("blue")};
 
-        MulticastSearch multicast = new MulticastSearch();
-        Filter filter = new Filter();
+        final MulticastSearch multicast = new MulticastSearch();
+        final Filter filter = new Filter();
 
         System.out.println("uri = " + multicast.search(filter));
         System.out.println("uri = " + multicast.search(filter));
@@ -49,14 +49,13 @@
         Thread.sleep(2000);
         System.out.println("--");
 
-
-        for (MulticastDiscoveryAgent agent : agents) {
+        for (final MulticastDiscoveryAgent agent : agents) {
             Thread.sleep(2000);
             System.out.println("--");
             agent.stop();
         }
 
-        for (MulticastDiscoveryAgent agent : agents) {
+        for (final MulticastDiscoveryAgent agent : agents) {
             Thread.sleep(2000);
             System.out.println("--");
             agent.start();
@@ -67,40 +66,44 @@
     }
 
     private static class Filter implements MulticastSearch.Filter {
+
         private final Set<URI> seen = new HashSet<URI>();
+
         @Override
-        public boolean accept(URI service) {
-            if (seen.contains(service)) return false;
+        public boolean accept(final URI service) {
+            if (seen.contains(service))
+                return false;
             seen.add(service);
             return true;
         }
     }
 
-    private MulticastDiscoveryAgent agent(String id) throws IOException, URISyntaxException, ServiceException {
-        MulticastDiscoveryAgent agent = new MulticastDiscoveryAgent();
+    private MulticastDiscoveryAgent agent(final String id) throws IOException, URISyntaxException, ServiceException {
+        final MulticastDiscoveryAgent agent = new MulticastDiscoveryAgent();
         agent.init(new Properties());
         agent.setDiscoveryListener(new MyDiscoveryListener(id));
-        agent.registerService(new URI("ejb:ejbd://"+id+":4201"));
+        agent.registerService(new URI("ejb:ejbd://" + id + ":4201"));
         agent.start();
         return agent;
     }
 
     private static class MyDiscoveryListener implements DiscoveryListener {
+
         private final String id;
 
         public MyDiscoveryListener(String id) {
             id += "        ";
-            id = id.substring(0,8);
+            id = id.substring(0, 8);
             this.id = id;
         }
 
         @Override
-        public void serviceAdded(URI service) {
+        public void serviceAdded(final URI service) {
             System.out.println(id + "add " + service.toString());
         }
 
         @Override
-        public void serviceRemoved(URI service) {
+        public void serviceRemoved(final URI service) {
             System.out.println(id + "remove " + service.toString());
         }
     }