Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored app creation, allow annotation to specify a factory
- Loading branch information
Showing
11 changed files
with
296 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
helpers/src/main/java/com/linkedin/plugin/j/FakeApplicationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package com.linkedin.plugin.j; | ||
|
||
import play.Application; | ||
|
||
/** | ||
* Knows how to build applications using the data that is available from annotations. | ||
*/ | ||
public interface FakeApplicationFactory { | ||
|
||
Application buildApplication(FakeApplicationFactoryArgs args); | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
helpers/src/main/java/com/linkedin/plugin/j/FakeApplicationFactoryArgs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.linkedin.plugin.j; | ||
|
||
import play.GlobalSettings; | ||
import play.api.inject.Binding; | ||
import play.inject.guice.GuiceBuilder; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class FakeApplicationFactoryArgs { | ||
private final File _path; | ||
private final Optional<Class<? extends GuiceBuilder>> _builderClass; | ||
private final Optional<GlobalSettings> _global; | ||
private final List<Binding<?>> _overrides; | ||
private final Map<String, Object> _config; | ||
private final List<String> _plugins; | ||
|
||
public FakeApplicationFactoryArgs(File path, | ||
Optional<Class<? extends GuiceBuilder>> builderClass, | ||
Optional<GlobalSettings> global, | ||
List<Binding<?>> overrides, | ||
Map<String, Object> config, | ||
List<String> plugins) { | ||
_path = path; | ||
_builderClass = builderClass; | ||
_global = global; | ||
_overrides = overrides; | ||
_config = config; | ||
_plugins = plugins; | ||
} | ||
|
||
public File getPath() { | ||
return _path; | ||
} | ||
|
||
public Optional<Class<? extends GuiceBuilder>> getBuilderClass() { | ||
return _builderClass; | ||
} | ||
|
||
public Optional<GlobalSettings> getGlobal() { | ||
return _global; | ||
} | ||
|
||
public List<Binding<?>> getOverrides() { | ||
return _overrides; | ||
} | ||
|
||
public Map<String, Object> getConfig() { | ||
return _config; | ||
} | ||
|
||
public List<String> getPlugins() { | ||
return _plugins; | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
helpers/src/main/java/com/linkedin/plugin/j/FakeApplicationFactoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.linkedin.plugin.j; | ||
|
||
import play.Application; | ||
import play.Environment; | ||
import play.GlobalSettings; | ||
import play.Mode; | ||
import play.api.inject.Binding; | ||
import play.api.inject.package$; | ||
import play.inject.guice.GuiceApplicationBuilder; | ||
import play.inject.guice.GuiceBuilder; | ||
import play.test.FakeApplication; | ||
import play.test.Helpers; | ||
|
||
/** | ||
* Default implementation for building an Application. If a builder isn't specified and no overrides are present the Play | ||
* FakeApplication is used. Otherwise a builder is used. Using @WithPlugins and a builder or overrides isn't supported. | ||
*/ | ||
public class FakeApplicationFactoryImpl implements FakeApplicationFactory { | ||
@Override | ||
public Application buildApplication(FakeApplicationFactoryArgs args) { | ||
return shouldUseBuilder(args) ? buildFromBuilder(args) : buildFromFakeApp(args); | ||
} | ||
|
||
protected boolean shouldUseBuilder(FakeApplicationFactoryArgs args) { | ||
return args.getBuilderClass().isPresent() || !args.getOverrides().isEmpty(); | ||
} | ||
|
||
protected Application buildFromFakeApp(FakeApplicationFactoryArgs args) { | ||
return new FakeApplication(args.getPath(), Helpers.class.getClassLoader(), args.getConfig(), args.getPlugins(), args.getGlobal().orElse(null)); | ||
} | ||
|
||
protected Application buildFromBuilder(FakeApplicationFactoryArgs args) { | ||
Class<? extends GuiceBuilder> builderClass = args.getBuilderClass().orElse(GuiceApplicationBuilder.class); | ||
GuiceBuilder builder; | ||
try { | ||
builder = builderClass.newInstance(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Unable to instantiate application builder " + builderClass, e); | ||
} | ||
if (!args.getPlugins().isEmpty()) { | ||
throw new RuntimeException("Using plugins isn't supported when using binding overrides or a GuiceBuilder."); | ||
} | ||
|
||
builder = (GuiceBuilder) builder.in(new Environment(args.getPath(), Helpers.class.getClassLoader(), Mode.TEST)); | ||
builder = (GuiceBuilder) builder.configure(args.getConfig()); | ||
if (args.getGlobal().isPresent()) { | ||
GlobalSettings global = args.getGlobal().get(); | ||
if (builder instanceof GuiceApplicationBuilder) { | ||
builder = ((GuiceApplicationBuilder) builder).global(global); | ||
} else { | ||
play.api.GlobalSettings scalaGlobal = new play.core.j.JavaGlobalSettingsAdapter(global); | ||
builder = (GuiceBuilder) builder.bindings(package$.MODULE$.bind(play.api.GlobalSettings.class).toInstance(scalaGlobal)); | ||
} | ||
} | ||
builder = (GuiceBuilder) builder.overrides(args.getOverrides().toArray(new Binding[args.getOverrides().size()])); | ||
return builder.injector().instanceOf(Application.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
helpers/src/main/java/com/linkedin/plugin/s/FakeApplicationFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.linkedin.plugin.s; | ||
|
||
import play.api.Application; | ||
|
||
/** | ||
* Knows how to build applications using the data that is available from annotations. | ||
*/ | ||
public interface FakeApplicationFactory { | ||
Application buildScalaApplication(FakeApplicationFactoryArgs args); | ||
} |
57 changes: 57 additions & 0 deletions
57
helpers/src/main/java/com/linkedin/plugin/s/FakeApplicationFactoryArgs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.linkedin.plugin.s; | ||
|
||
import play.api.GlobalSettings; | ||
import play.api.inject.Binding; | ||
import play.api.inject.guice.GuiceBuilder; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class FakeApplicationFactoryArgs { | ||
private final File _path; | ||
private final Optional<Class<? extends GuiceBuilder>> _builderClass; | ||
private final Optional<GlobalSettings> _global; | ||
private final List<Binding<?>> _overrides; | ||
private final Map<String, Object> _config; | ||
private final List<String> _plugins; | ||
|
||
public FakeApplicationFactoryArgs(File path, | ||
Optional<Class<? extends GuiceBuilder>> builderClass, | ||
Optional<GlobalSettings> global, | ||
List<Binding<?>> overrides, | ||
Map<String, Object> config, | ||
List<String> plugins) { | ||
_path = path; | ||
_builderClass = builderClass; | ||
_global = global; | ||
_overrides = overrides; | ||
_config = config; | ||
_plugins = plugins; | ||
} | ||
|
||
public File getPath() { | ||
return _path; | ||
} | ||
|
||
public Optional<Class<? extends GuiceBuilder>> getBuilderClass() { | ||
return _builderClass; | ||
} | ||
|
||
public Optional<GlobalSettings> getGlobal() { | ||
return _global; | ||
} | ||
|
||
public List<Binding<?>> getOverrides() { | ||
return _overrides; | ||
} | ||
|
||
public Map<String, Object> getConfig() { | ||
return _config; | ||
} | ||
|
||
public List<String> getPlugins() { | ||
return _plugins; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
helpers/src/main/java/com/linkedin/plugin/s/FakeApplicationFactoryImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.linkedin.plugin.s; | ||
|
||
import play.api.Application; | ||
import play.api.Environment; | ||
import play.api.GlobalSettings; | ||
import play.api.Mode; | ||
import play.api.inject.guice.GuiceApplicationBuilder; | ||
import play.api.inject.guice.GuiceBuilder; | ||
import play.api.inject.package$; | ||
import play.api.mvc.Handler; | ||
import play.api.test.FakeApplication; | ||
import play.api.test.Helpers; | ||
import play.libs.Scala; | ||
import scala.PartialFunction$; | ||
import scala.Tuple2; | ||
|
||
import java.util.Collections; | ||
|
||
/** | ||
* Default implementation for building an Application. If a builder isn't specified and no overrides are present the Play | ||
* FakeApplication is used. Otherwise a builder is used. Using @WithPlugins and a builder or overrides isn't supported. | ||
*/ | ||
public class FakeApplicationFactoryImpl implements FakeApplicationFactory { | ||
@Override | ||
public Application buildScalaApplication(FakeApplicationFactoryArgs args) { | ||
return shouldUseBuilder(args) ? buildFromBuilder(args) : buildFromFakeApp(args); | ||
} | ||
|
||
protected boolean shouldUseBuilder(FakeApplicationFactoryArgs args) { | ||
return args.getBuilderClass().isPresent() || !args.getOverrides().isEmpty(); | ||
} | ||
|
||
protected Application buildFromBuilder(FakeApplicationFactoryArgs args) { | ||
Class<? extends GuiceBuilder> builderClass = args.getBuilderClass().orElse(GuiceApplicationBuilder.class); | ||
GuiceBuilder builder; | ||
try { | ||
builder = builderClass.newInstance(); | ||
} catch (Exception e) { | ||
throw new RuntimeException("Unable to instantiate application builder " + builderClass, e); | ||
} | ||
if (!args.getPlugins().isEmpty()) { | ||
throw new RuntimeException("Using plugins isn't supported when using binding overrides or a GuiceBuilder."); | ||
} | ||
builder = (GuiceBuilder) builder.in(new Environment(args.getPath(), play.test.Helpers.class.getClassLoader(), Mode.Test())); | ||
builder = (GuiceBuilder) builder.configure(Scala.asScala(args.getConfig())); | ||
if (args.getGlobal().isPresent()) { | ||
GlobalSettings global = args.getGlobal().get(); | ||
if (builder instanceof GuiceApplicationBuilder) { | ||
builder = ((GuiceApplicationBuilder) builder).global(global); | ||
} else { | ||
builder = (GuiceBuilder) builder.bindings(Scala.varargs(package$.MODULE$.bind(play.api.GlobalSettings.class).toInstance(global))); | ||
} | ||
} | ||
builder = (GuiceBuilder) builder.overrides(Scala.asScala(args.getOverrides())); | ||
return builder.injector().instanceOf(Application.class); | ||
} | ||
|
||
protected Application buildFromFakeApp(FakeApplicationFactoryArgs args) { | ||
return new FakeApplication( | ||
args.getPath(), | ||
Helpers.class.getClassLoader(), | ||
Scala.toSeq(args.getPlugins()), | ||
Scala.toSeq(Collections.<String>emptyList()), | ||
Scala.asScala(args.getConfig()), | ||
scala.Option.apply(args.getGlobal().orElse(null)), | ||
PartialFunction$.MODULE$.<Tuple2<String, String>, Handler>empty() | ||
); | ||
|
||
} | ||
} |
Oops, something went wrong.