You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by ga...@apache.org on 2017/08/27 03:20:55 UTC

jclouds git commit: JCLOUDS-1334: Guava 23 compatiblity

Repository: jclouds
Updated Branches:
  refs/heads/master 5e12796a3 -> e08acc6ed


JCLOUDS-1334: Guava 23 compatiblity

Reflective creation of SimpleTimeLimiter to allow compatibility with
Guava 23.0.  SimpleTimeLimiter.create(ExecutorService) was introduced
in Guava 22.0 to replace the SimpleTimeLimiter(ExecutorService)
constructor, which was deprecated in Guava 22.0 and removed in Guava
23.0.


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

Branch: refs/heads/master
Commit: e08acc6ed6b2761b6063f9ae105ee9f4077ef418
Parents: 5e12796
Author: Tim Peierls <ti...@peierls.net>
Authored: Sat Aug 26 10:41:36 2017 -0400
Committer: Andrew Gaul <ga...@apache.org>
Committed: Sat Aug 26 20:19:06 2017 -0700

----------------------------------------------------------------------
 .../config/ExecutorServiceModule.java           | 59 +++++++++++++++++++-
 .../rest/internal/BaseRestApiExpectTest.java    |  4 +-
 .../jclouds/rest/internal/BaseRestApiTest.java  |  4 +-
 3 files changed, 62 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/e08acc6e/core/src/main/java/org/jclouds/concurrent/config/ExecutorServiceModule.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/jclouds/concurrent/config/ExecutorServiceModule.java b/core/src/main/java/org/jclouds/concurrent/config/ExecutorServiceModule.java
index a78411c..8ccfd6d 100644
--- a/core/src/main/java/org/jclouds/concurrent/config/ExecutorServiceModule.java
+++ b/core/src/main/java/org/jclouds/concurrent/config/ExecutorServiceModule.java
@@ -20,6 +20,9 @@ import static com.google.common.util.concurrent.MoreExecutors.listeningDecorator
 import static org.jclouds.Constants.PROPERTY_USER_THREADS;
 import static org.jclouds.concurrent.DynamicExecutors.newScalingThreadPool;
 
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
 import java.io.Closeable;
 import java.io.IOException;
 import java.util.List;
@@ -54,6 +57,60 @@ import com.google.inject.Provides;
 @ConfiguresExecutorService
 public class ExecutorServiceModule extends AbstractModule {
 
+   private static final Method CREATE_STL;
+   private static final Constructor<SimpleTimeLimiter> CONSTRUCT_STL;
+   static {
+      Method create = null;
+      Constructor ctor = null;
+      try {
+         create = SimpleTimeLimiter.class.getDeclaredMethod("create", ExecutorService.class);
+      } catch (NoSuchMethodException nsme) {
+         try {
+            ctor = SimpleTimeLimiter.class.getConstructor(ExecutorService.class);
+         } catch (NoSuchMethodException nsme2) {
+            throw new UnsupportedOperationException(
+               "Can't find SimpleTimeLimiter creator or constructor taking ExecutorService", nsme2);
+         }
+      }
+      CREATE_STL = create;
+      CONSTRUCT_STL = ctor;
+   }
+
+   /**
+    * Reflective creation of SimpleTimeLimiter to allow compatibility with Guava 23.0.
+    * SimpleTimeLimiter.create(ExecutorService) was introduced in Guava 22.0 to replace
+    * the SimpleTimeLimiter(ExecutorService) constructor, which was deprecated in
+    * Guava 22.0 and removed in Guava 23.0. The method is public to allow test methods
+    * in other packages to use it.
+    * @param executorService the execution service to use when running time-limited tasks
+    * @return a new instance of SimpleTimeLimiter that uses executorService
+    */
+   public static SimpleTimeLimiter createSimpleTimeLimiter(ExecutorService executorService) {
+      try {
+         if (CREATE_STL != null) {
+            return (SimpleTimeLimiter) CREATE_STL.invoke(null, executorService);
+         } else if (CONSTRUCT_STL != null) {
+            return CONSTRUCT_STL.newInstance(executorService);
+         }
+         throw new UnsupportedOperationException(
+            "Can't find SimpleTimeLimiter creator or constructor taking ExecutorService");
+      } catch (IllegalAccessException iae) {
+         throw new UnsupportedOperationException("Can't access SimpleTimeLimiter method/ctor", iae);
+      } catch (InstantiationException ie) {
+         throw new UnsupportedOperationException("Can't construct SimpleTimeLimiter", ie);
+      } catch (InvocationTargetException ite) {
+         Throwable throwable = ite.getCause();
+         if (throwable instanceof RuntimeException) {
+            throw (RuntimeException) throwable;
+         }
+         if (throwable instanceof Error) {
+            throw (Error) throwable;
+         }
+         throw new UnsupportedOperationException(
+            "Checked exception thrown while creating SimpleTimeLimiter", throwable);
+      }
+   }
+
    static final class ShutdownExecutorOnClose implements Closeable {
       @Resource
       private Logger logger = Logger.NULL;
@@ -113,7 +170,7 @@ public class ExecutorServiceModule extends AbstractModule {
    @Provides
    @Singleton
    final TimeLimiter timeLimiter(@Named(PROPERTY_USER_THREADS) ListeningExecutorService userExecutor) {
-      return new SimpleTimeLimiter(userExecutor);
+      return createSimpleTimeLimiter(userExecutor);
    }
 
    @Provides

http://git-wip-us.apache.org/repos/asf/jclouds/blob/e08acc6e/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java b/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java
index c6c66b3..144ad7e 100644
--- a/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java
+++ b/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java
@@ -22,6 +22,7 @@ import static com.google.inject.name.Names.named;
 import static org.jclouds.Constants.PROPERTY_IDEMPOTENT_METHODS;
 import static org.jclouds.Constants.PROPERTY_MAX_RETRIES;
 import static org.jclouds.Constants.PROPERTY_USER_THREADS;
+import static org.jclouds.concurrent.config.ExecutorServiceModule.createSimpleTimeLimiter;
 import static org.testng.Assert.assertEquals;
 
 import java.io.IOException;
@@ -80,7 +81,6 @@ import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableSet;
 import com.google.common.io.ByteSource;
 import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.SimpleTimeLimiter;
 import com.google.common.util.concurrent.TimeLimiter;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;
@@ -235,7 +235,7 @@ public abstract class BaseRestApiExpectTest<S> {
       @Provides
       @Singleton
       TimeLimiter timeLimiter(@Named(PROPERTY_USER_THREADS) ListeningExecutorService userExecutor) {
-         return new SimpleTimeLimiter(userExecutor);
+         return createSimpleTimeLimiter(userExecutor);
       }
    }
 

http://git-wip-us.apache.org/repos/asf/jclouds/blob/e08acc6e/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java b/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java
index 915aa99..cee3a5a 100644
--- a/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java
+++ b/core/src/test/java/org/jclouds/rest/internal/BaseRestApiTest.java
@@ -22,6 +22,7 @@ import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorS
 import static com.google.inject.name.Names.named;
 import static org.easymock.EasyMock.createMock;
 import static org.jclouds.Constants.PROPERTY_USER_THREADS;
+import static org.jclouds.concurrent.config.ExecutorServiceModule.createSimpleTimeLimiter;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNull;
 
@@ -50,7 +51,6 @@ import com.google.common.collect.SortedSetMultimap;
 import com.google.common.collect.TreeMultimap;
 import com.google.common.reflect.Invokable;
 import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.common.util.concurrent.SimpleTimeLimiter;
 import com.google.common.util.concurrent.TimeLimiter;
 import com.google.inject.AbstractModule;
 import com.google.inject.Injector;
@@ -84,7 +84,7 @@ public abstract class BaseRestApiTest {
       @Provides
       @Singleton
       TimeLimiter timeLimiter(@Named(PROPERTY_USER_THREADS) ListeningExecutorService userExecutor) {
-         return new SimpleTimeLimiter(userExecutor);
+         return createSimpleTimeLimiter(userExecutor);
       }
    }