ratpack.guice.internal.DefaultBindingsSpec Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ratpack-guice Show documentation
Show all versions of ratpack-guice Show documentation
Integration with Google Guice for Ratpack applications - https://code.google.com/p/google-guice/
/*
* Copyright 2014 the original author or authors.
*
* 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 ratpack.guice.internal;
import com.google.inject.Binder;
import com.google.inject.Module;
import ratpack.func.Action;
import ratpack.guice.BindingsSpec;
import ratpack.guice.ConfigurableModule;
import ratpack.server.ServerConfig;
import java.util.List;
public class DefaultBindingsSpec implements BindingsSpec {
private final List modules;
private final ServerConfig serverConfig;
private final List> binderActions;
public DefaultBindingsSpec(ServerConfig serverConfig, List> binderActions, List modules) {
this.serverConfig = serverConfig;
this.binderActions = binderActions;
this.modules = modules;
}
public ServerConfig getServerConfig() {
return serverConfig;
}
@Override
public BindingsSpec module(Module module) {
this.modules.add(module);
return this;
}
public BindingsSpec module(Class extends Module> moduleClass) {
return module(createModule(moduleClass));
}
private T createModule(Class clazz) {
try {
return clazz.newInstance();
} catch (ReflectiveOperationException e) {
throw new IllegalStateException("Module " + clazz.getName() + " is not reflectively instantiable", e);
}
}
@Override
public BindingsSpec module(ConfigurableModule module, Action super C> configurer) {
module.configure(configurer);
return module(module);
}
@Override
public > BindingsSpec module(Class moduleClass, Action super C> configurer) {
T t = createModule(moduleClass);
return module(t, configurer);
}
@Override
public BindingsSpec moduleConfig(ConfigurableModule module, C config, Action super C> configurer) {
module.setConfig(config);
return module(module, configurer);
}
@Override
public > BindingsSpec moduleConfig(Class moduleClass, C config, Action super C> configurer) {
T t = createModule(moduleClass);
return moduleConfig(t, config, configurer);
}
@Override
public BindingsSpec binder(Action super Binder> action) {
binderActions.add(action);
return this;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy