use of com.peterphi.std.guice.apploader.impl.GuiceRegistry in project stdlib by petergeneric.
the class ResteasyDispatcher method init.
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
GuiceBuilder builder = new GuiceBuilder().withRole(new WebappGuiceRole(config));
this.registry = new GuiceRegistry(builder);
dispatcher = new GuicedResteasy(registry, config, new ServletBootstrap(config), true);
startInitialise();
}
use of com.peterphi.std.guice.apploader.impl.GuiceRegistry in project stdlib by petergeneric.
the class GuiceRegistryBuilder method createRegistry.
private static GuiceRegistry createRegistry(GuiceConfig config, TestClass clazz) {
GuiceRegistry registry = new GuiceRegistry();
ClassScannerFactory scanner = null;
GuiceRole[] roles = null;
if (config != null) {
if (config.packages().length > 0 || config.classPackages().length > 0) {
Set<String> packages = new HashSet<>();
packages.addAll(Arrays.asList(config.packages()));
for (Class c : config.classPackages()) packages.add(c.getPackage().getName());
scanner = new ClassScannerFactory(packages.toArray(new String[packages.size()]));
}
if (config.role().length > 0) {
List<GuiceRole> instances = new ArrayList<>();
for (Class<? extends GuiceRole> role : config.role()) {
try {
instances.add(role.newInstance());
} catch (Exception e) {
throw new IllegalArgumentException("Error instantiating GuiceRole " + role, e);
}
}
roles = instances.toArray(new GuiceRole[instances.size()]);
}
}
GuiceBuilder builder = registry.getBuilder();
if (scanner != null)
builder.withScannerFactory(scanner);
else
builder.withNoScannerFactory();
if (config != null && config.config().length > 0)
builder.withConfig(config.config());
if (config != null)
builder.withAutoLoadRoles(config.autoLoadRoles());
if (roles != null)
builder.withRole(roles);
// Add local method config sources
{
validateGuiceTestConfigMethods(clazz);
for (Object src : clazz.getAnnotatedMethodValues(null, TestConfig.class, Object.class)) {
if (src instanceof Properties)
builder.withConfig((Properties) src);
else if (src instanceof PropertyFile)
builder.withConfig((PropertyFile) src);
}
}
// Add local method module sources
{
validateGuiceTestModuleMethods(clazz);
builder.withRole(new ModuleAddingGuiceRole(clazz.getAnnotatedMethodValues(null, TestModule.class, Module.class)));
}
// Auto-detect @Automock annotated fields in the test and create mocks for them
{
List<FrameworkField> fields = clazz.getAnnotatedFields(Automock.class);
if (fields.size() > 0)
builder.withRole(new ModuleAddingGuiceRole(new AutomockAnnotatedMockModule(clazz.getJavaClass(), fields)));
}
// Make sure we set the unit test property so roles are aware they're running in a unit test (e.g. so they don't auto-register REST services)
{
PropertyFile props = new PropertyFile();
props.set(GuiceProperties.UNIT_TEST, "true");
builder.withConfig(props);
}
// Add the Setup class, or if none is specified then add local modules:
if (config != null && config.setup().length > 0) {
builder.withSetup(config.setup()[0]);
} else {
builder.withSetup(new BasicSetup());
}
return registry;
}
use of com.peterphi.std.guice.apploader.impl.GuiceRegistry in project stdlib by petergeneric.
the class GuiceServlet method service.
@Override
protected final void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
final HttpCallContext ctx = HttpCallContext.set(req, resp, getServletContext());
try {
// Share the call id to log4j
Tracing.start(ctx.getLogId(), ctx.isVerbose());
// If necessary set up Guice
if (!ready.get()) {
if (registry == null)
registry = new GuiceRegistry(new GuiceBuilder().withRole(new WebappGuiceRole(getServletConfig())));
registry.register(this, true);
}
// Make the call
doService(req, resp);
} finally {
HttpCallContext.clear();
Tracing.clear();
}
}
use of com.peterphi.std.guice.apploader.impl.GuiceRegistry in project stdlib by petergeneric.
the class ResteasyDispatcher method init.
@Override
public void init(FilterConfig config) throws ServletException {
GuiceBuilder builder = new GuiceBuilder().withRole(new WebappGuiceRole(config));
this.registry = new GuiceRegistry(builder);
dispatcher = new GuicedResteasy(registry, config, new FilterBootstrap(config), false);
startInitialise();
}
Aggregations