net.nikore.gozer.marathon.MarathonModule.java Source code

Java tutorial

Introduction

Here is the source code for net.nikore.gozer.marathon.MarathonModule.java

Source

/**
 * Copyright (C) 2015 Matt Christiansen (matt@nikore.net)
 *
 * 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 net.nikore.gozer.marathon;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.servlet.ServletModule;

public class MarathonModule extends ServletModule {
    @Override
    protected void configureServlets() {
        bind(GozerMarathonConfig.class).to(DefaultMarathonConfig.class).asEagerSingleton();
        bind(CallbackServlet.class).asEagerSingleton();
        serve("/marathon/callbacks").with(CallbackServlet.class);
    }

    @Provides
    @Singleton
    MarathonAppCache provideMarathonAppCache(MarathonHost marathonHost, GozerMarathonConfig config)
            throws Exception {
        MarathonAppCache marathonAppCache = new MarathonAppCache(marathonHost, config);
        marathonAppCache.populate();
        marathonHost.registerCallback();

        return marathonAppCache;
    }

    @Provides
    @Singleton
    ObjectMapper provideObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        mapper.setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
        return mapper;
    }
}