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 2020/04/12 17:06:16 UTC

[commons-jcs] branch master updated (68acfbf -> ec408ed)

This is an automated email from the ASF dual-hosted git repository.

tv pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git.


    from 68acfbf  Make tests run with log4j2
     new 5d308e9  JCS-182: Check for null
     new f4080fe  Better handling of multicast on multihomed machines
     new ec408ed  More lambda

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../dsfactory/SharedPoolDataSourceFactory.java     |  5 +-
 .../jcs/engine/control/CompositeCacheManager.java  | 53 ++++++++-------------
 .../jcs/utils/discovery/UDPDiscoveryReceiver.java  | 12 ++++-
 .../apache/commons/jcs/utils/net/HostNameUtil.java | 55 ++++++++++++++++++++--
 .../jcs/utils/discovery/UDPDiscoveryUnitTest.java  |  6 +--
 5 files changed, 88 insertions(+), 43 deletions(-)


[commons-jcs] 01/03: JCS-182: Check for null

Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git

commit 5d308e985f73282d03209e5b3e0a90ff9741db22
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Apr 12 19:04:20 2020 +0200

    JCS-182: Check for null
---
 .../auxiliary/disk/jdbc/dsfactory/SharedPoolDataSourceFactory.java   | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/dsfactory/SharedPoolDataSourceFactory.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/dsfactory/SharedPoolDataSourceFactory.java
index 27037b8..1104f96 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/dsfactory/SharedPoolDataSourceFactory.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/auxiliary/disk/jdbc/dsfactory/SharedPoolDataSourceFactory.java
@@ -92,7 +92,10 @@ public class SharedPoolDataSourceFactory implements DataSourceFactory
     {
         try
         {
-            ds.close();
+            if (ds != null)
+            {
+                ds.close();
+            }
         }
         catch (Exception e)
         {


[commons-jcs] 03/03: More lambda

Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git

commit ec408ed961edede52bb5ffd59bfaf026553c40b0
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Apr 12 19:06:03 2020 +0200

    More lambda
---
 .../jcs/engine/control/CompositeCacheManager.java  | 53 ++++++++--------------
 1 file changed, 19 insertions(+), 34 deletions(-)

diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
index 85bbb08..7f50bcb 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/engine/control/CompositeCacheManager.java
@@ -23,7 +23,8 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.lang.management.ManagementFactory;
 import java.security.AccessControlException;
-import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
 import java.util.Properties;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
@@ -31,6 +32,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.LinkedBlockingDeque;
 import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
 
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
@@ -622,16 +624,10 @@ public class CompositeCacheManager
             }
 
             // do the traditional shutdown of the regions.
-            for (String name : getCacheNames())
-            {
-                freeCache( name );
-            }
+            Arrays.stream(getCacheNames()).forEach(this::freeCache);
 
             // shut down factories
-            for (AuxiliaryCacheFactory factory : auxiliaryFactoryRegistry.values())
-            {
-                factory.dispose();
-            }
+            auxiliaryFactoryRegistry.values().forEach(AuxiliaryCacheFactory::dispose);
 
             auxiliaryAttributeRegistry.clear();
             auxiliaryFactoryRegistry.clear();
@@ -678,15 +674,11 @@ public class CompositeCacheManager
             log.debug( "Last client called release. There are {0} caches which will be disposed",
                     () -> caches.size());
 
-            for (ICache<?, ?> c : caches.values() )
-            {
-                CompositeCache<?, ?> cache = (CompositeCache<?, ?>) c;
-
-                if ( cache != null )
-                {
-                    cache.dispose( fromRemote );
-                }
-            }
+            caches.values().stream()
+                .filter(cache -> cache != null)
+                .forEach(cache -> {
+                    ((CompositeCache<?, ?>)cache).dispose( fromRemote );
+                });
         }
     }
 
@@ -798,12 +790,10 @@ public class CompositeCacheManager
 
         // force the array elements into a string.
         StringBuilder buf = new StringBuilder();
-        int statsLen = stats.length;
-        for ( int i = 0; i < statsLen; i++ )
-        {
+        Arrays.stream(stats).forEach(stat -> {
             buf.append( "\n---------------------------\n" );
-            buf.append( stats[i] );
-        }
+            buf.append( stat );
+        });
         return buf.toString();
     }
 
@@ -814,17 +804,12 @@ public class CompositeCacheManager
      */
     public ICacheStats[] getStatistics()
     {
-        ArrayList<ICacheStats> cacheStats = new ArrayList<>();
-        for (ICache<?, ?> c :  caches.values())
-        {
-            CompositeCache<?, ?> cache = (CompositeCache<?, ?>) c;
-            if ( cache != null )
-            {
-                cacheStats.add( cache.getStatistics() );
-            }
-        }
-        ICacheStats[] stats = cacheStats.toArray( new CacheStats[0] );
-        return stats;
+        List<ICacheStats> cacheStats = caches.values().stream()
+            .filter(cache -> cache != null)
+            .map(cache -> ((CompositeCache<?, ?>)cache).getStatistics() )
+            .collect(Collectors.toList());
+
+        return cacheStats.toArray( new CacheStats[0] );
     }
 
     /**


[commons-jcs] 02/03: Better handling of multicast on multihomed machines

Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

tv pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-jcs.git

commit f4080fe78f15606df4f13bf2b9dbf893a7e8cb17
Author: Thomas Vandahl <tv...@apache.org>
AuthorDate: Sun Apr 12 19:05:00 2020 +0200

    Better handling of multicast on multihomed machines
---
 .../jcs/utils/discovery/UDPDiscoveryReceiver.java  | 12 ++++-
 .../apache/commons/jcs/utils/net/HostNameUtil.java | 55 ++++++++++++++++++++--
 .../jcs/utils/discovery/UDPDiscoveryUnitTest.java  |  6 +--
 3 files changed, 65 insertions(+), 8 deletions(-)

diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java
index 2fbc2eb..0438ee3 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryReceiver.java
@@ -25,6 +25,7 @@ import java.io.ObjectInputStream;
 import java.net.DatagramPacket;
 import java.net.InetAddress;
 import java.net.MulticastSocket;
+import java.net.NetworkInterface;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.atomic.AtomicInteger;
 
@@ -34,6 +35,7 @@ import org.apache.commons.jcs.io.ObjectInputStreamClassLoaderAware;
 import org.apache.commons.jcs.log.Log;
 import org.apache.commons.jcs.log.LogManager;
 import org.apache.commons.jcs.utils.discovery.UDPDiscoveryMessage.BroadcastType;
+import org.apache.commons.jcs.utils.net.HostNameUtil;
 import org.apache.commons.jcs.utils.threadpool.PoolConfiguration;
 import org.apache.commons.jcs.utils.threadpool.PoolConfiguration.WhenBlockedPolicy;
 import org.apache.commons.jcs.utils.threadpool.ThreadPoolManager;
@@ -117,11 +119,17 @@ public class UDPDiscoveryReceiver
         try
         {
             mSocket = new MulticastSocket( multicastPort );
+            InetAddress multicastAddress = InetAddress.getByName( multicastAddressString );
             if (log.isInfoEnabled())
             {
-                log.info( "Joining Group: [{0}]", InetAddress.getByName( multicastAddressString ) );
+                log.info( "Joining Group: [{0}]", multicastAddress );
             }
-            mSocket.joinGroup( InetAddress.getByName( multicastAddressString ) );
+            NetworkInterface multicastInterface = HostNameUtil.getMulticastNetworkInterface();
+            if (multicastInterface != null)
+            {
+                mSocket.setNetworkInterface(multicastInterface);
+            }
+            mSocket.joinGroup( multicastAddress );
         }
         catch ( IOException e )
         {
diff --git a/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/net/HostNameUtil.java b/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/net/HostNameUtil.java
index 4e96e43..2c3bce6 100644
--- a/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/net/HostNameUtil.java
+++ b/commons-jcs-core/src/main/java/org/apache/commons/jcs/utils/net/HostNameUtil.java
@@ -1,12 +1,30 @@
 package org.apache.commons.jcs.utils.net;
 
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
 import java.net.InetAddress;
 import java.net.NetworkInterface;
+import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.util.Enumeration;
-
 /*
- * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements.  See the NOTICE file
  * distributed with this work for additional information
  * regarding copyright ownership.  The ASF licenses this file
@@ -135,7 +153,7 @@ public class HostNameUtil
             }
             return jdkSuppliedAddress;
         }
-        catch ( Exception e )
+        catch ( SocketException e )
         {
             UnknownHostException unknownHostException = new UnknownHostException( "Failed to determine LAN address: "
                 + e );
@@ -143,4 +161,35 @@ public class HostNameUtil
             throw unknownHostException;
         }
     }
+
+    /**
+     * On systems with multiple network interfaces and mixed IPv6/IPv4 get a valid network
+     * interface for binding to multicast
+     *
+     * @return a network interface suitable for multicast
+     * @throws SocketException if a problem occurs while reading the network interfaces
+     */
+    public static NetworkInterface getMulticastNetworkInterface() throws SocketException
+    {
+        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
+        while (networkInterfaces.hasMoreElements())
+        {
+            NetworkInterface networkInterface = networkInterfaces.nextElement();
+            Enumeration<InetAddress> addressesFromNetworkInterface = networkInterface.getInetAddresses();
+            while (addressesFromNetworkInterface.hasMoreElements())
+            {
+                InetAddress inetAddress = addressesFromNetworkInterface.nextElement();
+                if (inetAddress.isSiteLocalAddress()
+                        && !inetAddress.isAnyLocalAddress()
+                        && !inetAddress.isLinkLocalAddress()
+                        && !inetAddress.isLoopbackAddress()
+                        && !inetAddress.isMulticastAddress())
+                {
+                    return networkInterface;
+                }
+            }
+        }
+
+        return null;
+    }
 }
diff --git a/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryUnitTest.java b/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryUnitTest.java
index 97af7bf..6a83885 100644
--- a/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryUnitTest.java
+++ b/commons-jcs-core/src/test/java/org/apache/commons/jcs/utils/discovery/UDPDiscoveryUnitTest.java
@@ -21,10 +21,10 @@ package org.apache.commons.jcs.utils.discovery;
 
 import java.util.ArrayList;
 
-import junit.framework.TestCase;
-
 import org.apache.commons.jcs.utils.timing.SleepUtil;
 
+import junit.framework.TestCase;
+
 /**
  * Unit tests for discovery
  */
@@ -39,7 +39,7 @@ public class UDPDiscoveryUnitTest
         throws Exception
     {
         UDPDiscoveryAttributes attributes = new UDPDiscoveryAttributes();
-        attributes.setUdpDiscoveryAddr( "228.5.6.7" );
+        attributes.setUdpDiscoveryAddr( /*"FF7E:230::1234"*/ "228.5.6.7" );
         attributes.setUdpDiscoveryPort( 6789 );
         attributes.setServicePort( 1000 );