aurora-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From wfar...@apache.org
Subject incubator-aurora git commit: Add a mechanism to lazily instantiate module classes.
Date Tue, 24 Mar 2015 00:32:45 GMT
Repository: incubator-aurora
Updated Branches:
  refs/heads/master 3bb13720d -> 334f298e1


Add a mechanism to lazily instantiate module classes.

Bugs closed: AURORA-1217

Reviewed at https://reviews.apache.org/r/32377/


Project: http://git-wip-us.apache.org/repos/asf/incubator-aurora/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-aurora/commit/334f298e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-aurora/tree/334f298e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-aurora/diff/334f298e

Branch: refs/heads/master
Commit: 334f298e1fbc35ae5afe9a71de60384df70be668
Parents: 3bb1372
Author: Bill Farner <wfarner@apache.org>
Authored: Mon Mar 23 17:31:54 2015 -0700
Committer: Bill Farner <wfarner@apache.org>
Committed: Mon Mar 23 17:31:54 2015 -0700

----------------------------------------------------------------------
 .../apache/aurora/scheduler/app/Modules.java    | 28 ++++++++++----
 .../http/api/security/ApiSecurityModule.java    |  5 ++-
 .../http/api/security/ModuleParser.java         | 20 ++--------
 .../aurora/scheduler/app/ModulesTest.java       | 39 ++++++++++++++++++++
 4 files changed, 66 insertions(+), 26 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/334f298e/src/main/java/org/apache/aurora/scheduler/app/Modules.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/app/Modules.java b/src/main/java/org/apache/aurora/scheduler/app/Modules.java
index e95cb2a..cad7c38 100644
--- a/src/main/java/org/apache/aurora/scheduler/app/Modules.java
+++ b/src/main/java/org/apache/aurora/scheduler/app/Modules.java
@@ -13,6 +13,7 @@
  */
 package org.apache.aurora.scheduler.app;
 
+import com.google.inject.AbstractModule;
 import com.google.inject.Module;
 import com.google.inject.PrivateModule;
 
@@ -71,14 +72,27 @@ public final class Modules {
     };
   }
 
-  static Module wrapInPrivateModule(
-      Class<? extends Module> moduleClass,
-      final Iterable<Class<?>> exposedClasses) {
-
-    return wrapInPrivateModule(instantiateModule(moduleClass), exposedClasses);
-  }
-
   static Module getModule(Class<? extends Module> moduleClass) {
     return instantiateModule(moduleClass);
   }
+
+  /**
+   * Creates a module that will lazily instantiate and install another module.
+   * <p/>
+   * This serves as an indirection between module procurement and installation, which is
necessary
+   * in cases where a module is referenced within a static initializer.  In this scenario,
a module
+   * must not be instantiated if it reads command line arguments, as the args system has
not yet
+   * had a chance to populate them.
+   *
+   * @param moduleClass Module to install.
+   * @return An installer that will install {@code moduleClass}.
+   */
+  public static Module lazilyInstantiated(final Class<? extends Module> moduleClass)
{
+    return new AbstractModule() {
+      @Override
+      protected void configure() {
+        install(getModule(moduleClass));
+      }
+    };
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/334f298e/src/main/java/org/apache/aurora/scheduler/http/api/security/ApiSecurityModule.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/http/api/security/ApiSecurityModule.java
b/src/main/java/org/apache/aurora/scheduler/http/api/security/ApiSecurityModule.java
index cc9cfd3..ec6a02c 100644
--- a/src/main/java/org/apache/aurora/scheduler/http/api/security/ApiSecurityModule.java
+++ b/src/main/java/org/apache/aurora/scheduler/http/api/security/ApiSecurityModule.java
@@ -32,6 +32,7 @@ import org.aopalliance.intercept.MethodInterceptor;
 import org.apache.aurora.GuiceUtils;
 import org.apache.aurora.gen.AuroraAdmin;
 import org.apache.aurora.gen.AuroraSchedulerManager;
+import org.apache.aurora.scheduler.app.Modules;
 import org.apache.aurora.scheduler.http.api.ApiModule;
 import org.apache.shiro.SecurityUtils;
 import org.apache.shiro.guice.aop.ShiroAopModule;
@@ -57,8 +58,8 @@ public class ApiSecurityModule extends ServletModule {
 
   @CmdLine(name = "shiro_realm_modules",
       help = "Guice modules for configuring Shiro Realms.")
-  private static final Arg<Set<Module>> SHIRO_REALM_MODULE =
-      Arg.<Set<Module>>create(ImmutableSet.<Module>of(new IniShiroRealmModule()));
+  private static final Arg<Set<Module>> SHIRO_REALM_MODULE = Arg.<Set<Module>>create(
+      ImmutableSet.of(Modules.lazilyInstantiated(IniShiroRealmModule.class)));
 
   private final boolean enableApiSecurity;
   private final Set<Module> shiroConfigurationModules;

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/334f298e/src/main/java/org/apache/aurora/scheduler/http/api/security/ModuleParser.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/aurora/scheduler/http/api/security/ModuleParser.java
b/src/main/java/org/apache/aurora/scheduler/http/api/security/ModuleParser.java
index fe6409a..c968216 100644
--- a/src/main/java/org/apache/aurora/scheduler/http/api/security/ModuleParser.java
+++ b/src/main/java/org/apache/aurora/scheduler/http/api/security/ModuleParser.java
@@ -13,13 +13,12 @@
  */
 package org.apache.aurora.scheduler.http.api.security;
 
-import java.lang.reflect.Constructor;
-import java.lang.reflect.InvocationTargetException;
-
 import com.google.inject.Module;
 import com.twitter.common.args.ArgParser;
 import com.twitter.common.args.parsers.NonParameterizedTypeParser;
 
+import org.apache.aurora.scheduler.app.Modules;
+
 /**
  * ArgParser for Guice modules. Constructs an instance of a Module with a given FQCN if it
has a
  * public no-args constructor.
@@ -42,19 +41,6 @@ public class ModuleParser extends NonParameterizedTypeParser<Module>
{
     @SuppressWarnings("unchecked")
     Class<? extends Module> moduleClass = (Class<? extends Module>) rawClass;
 
-    Constructor<? extends Module> moduleConstructor;
-    try {
-      moduleConstructor = moduleClass.getConstructor();
-    } catch (NoSuchMethodException e) {
-      throw new IllegalArgumentException(
-          "Module " + raw + " must have a public no-args constructor.",
-          e);
-    }
-
-    try {
-      return moduleConstructor.newInstance();
-    } catch (InvocationTargetException | InstantiationException | IllegalAccessException
e) {
-      throw new IllegalArgumentException(e);
-    }
+    return Modules.lazilyInstantiated(moduleClass);
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-aurora/blob/334f298e/src/test/java/org/apache/aurora/scheduler/app/ModulesTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/aurora/scheduler/app/ModulesTest.java b/src/test/java/org/apache/aurora/scheduler/app/ModulesTest.java
new file mode 100644
index 0000000..01137e9
--- /dev/null
+++ b/src/test/java/org/apache/aurora/scheduler/app/ModulesTest.java
@@ -0,0 +1,39 @@
+/**
+ * Licensed 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.
+ */
+package org.apache.aurora.scheduler.app;
+
+import com.google.inject.AbstractModule;
+import com.google.inject.Guice;
+import com.google.inject.Injector;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class ModulesTest {
+  private static final String STRING = "string";
+
+  @Test
+  public void testLazilyInstantiated() {
+    Injector injector = Guice.createInjector(Modules.lazilyInstantiated(StringInstaller.class));
+    assertEquals(STRING, injector.getInstance(String.class));
+  }
+
+  static class StringInstaller extends AbstractModule {
+    @Override
+    protected void configure() {
+      bind(String.class).toInstance(STRING);
+    }
+  }
+}


Mime
View raw message