All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ratpack.guice.internal.DefaultBindingsSpec Maven / Gradle / Ivy

Go to download

Integration with Google Guice for Ratpack applications - https://code.google.com/p/google-guice/

There is a newer version: 2.0.0-rc-1
Show newest version
/*
 * 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 moduleClass) {
    return module(createModule(moduleClass));
  }

  private  T createModule(Class clazz) {
    try {
      return clazz.getDeclaredConstructor().newInstance();
    } catch (ReflectiveOperationException e) {
      throw new IllegalStateException("Module " + clazz.getName() + " is not reflectively instantiable", e);
    }
  }

  @Override
  public  BindingsSpec module(ConfigurableModule module, Action configurer) {
    module.configure(configurer);
    return module(module);
  }

  @Override
  public > BindingsSpec module(Class moduleClass, Action configurer) {
    T t = createModule(moduleClass);
    return module(t, configurer);
  }

  @Override
  public  BindingsSpec moduleConfig(ConfigurableModule module, C config, Action configurer) {
    module.setConfig(config);
    return module(module, configurer);
  }

  @Override
  public > BindingsSpec moduleConfig(Class moduleClass, C config, Action configurer) {
    T t = createModule(moduleClass);
    return moduleConfig(t, config, configurer);
  }

  @Override
  public BindingsSpec binder(Action action) {
    binderActions.add(action);
    return this;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy