Java tutorial
/* * MIT License * * Copyright (c) 2016 Nhan Nguyen * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package imbrobits.loosh.core; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.inject.AbstractModule; import com.google.inject.Provides; import com.google.inject.Singleton; import imbrobits.loosh.core.annotations.Controller; import imbrobits.loosh.dao.ProjectDao; import imbrobits.loosh.dao.ProjectDaoImpl; import imbrobits.loosh.handler.OAuth2Handler; import io.vertx.core.Vertx; import io.vertx.core.json.JsonObject; import io.vertx.ext.asyncsql.AsyncSQLClient; import io.vertx.ext.asyncsql.PostgreSQLClient; import io.vertx.ext.web.Router; import org.reflections.Reflections; import java.util.Set; public class BootstrapBinder extends AbstractModule { private final Vertx vertx; private final JsonObject config; public BootstrapBinder(Vertx vertx, JsonObject config) { this.vertx = vertx; this.config = config; } @Override protected void configure() { bind(EntryPoint.class); // scan packages for handler Reflections reflections = new Reflections("imbrobits.loosh"); Set<Class<?>> handlers = reflections.getTypesAnnotatedWith(Controller.class); handlers.stream().forEach(cls -> bind(cls)); reflections = new Reflections("imbrobits.loosh.handler"); Set<Class<? extends OAuth2Handler>> oauthHandlers = reflections.getSubTypesOf(OAuth2Handler.class); oauthHandlers.stream().forEach(cls -> bind(cls)); // bind config to object for future usage bind(ProjectDao.class).to(ProjectDaoImpl.class); } @Provides @Singleton private final Vertx getVertx() { return vertx; } @Provides @Singleton private final Config getConfig() { return (Config) config; } @Provides @Singleton private final Router getRouter() { return Router.router(getVertx()); } @Provides @Singleton private final AsyncSQLClient getDBClient() { JsonObject clientConfig = new JsonObject().put("host", config.getString(Config.DB_URL)) .put("port", config.getInteger(Config.DB_PORT)).put("database", config.getString(Config.DB_NAME)) .put("username", config.getString(Config.DB_USERNAME)) .put("password", config.getString(Config.DB_PWD)); return PostgreSQLClient.createShared(vertx, clientConfig); } @Provides @Singleton private final ObjectMapper getObjMapper() { return new ObjectMapper(); } }