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

com.arpnetworking.configuration.jackson.BaseJacksonConfiguration Maven / Gradle / Ivy

Go to download

(Re)Aggregates host level statistics across clusters and writes both host and cluster statistics to various destinations.

There is a newer version: 1.13.7
Show newest version
/*
 * Copyright 2014 Groupon.com
 *
 * 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 com.arpnetworking.configuration.jackson;

import com.arpnetworking.commons.builder.OvalBuilder;
import com.arpnetworking.commons.jackson.databind.ObjectMapperFactory;
import com.arpnetworking.configuration.Configuration;
import com.arpnetworking.logback.annotations.LogValue;
import com.arpnetworking.steno.LogValueMapFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.TypeFactory;
import net.sf.oval.constraint.NotNull;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Optional;
import java.util.function.Function;

/**
 * Common base class for Configuration implementations based on
 * Jackson's ObjectMapper.
 *
 * @author Ville Koskela (ville dot koskela at inscopemetrics dot com)
 */
public abstract class BaseJacksonConfiguration extends com.arpnetworking.configuration.BaseConfiguration {

    @Override
    public Optional getProperty(final String name) {
        final Optional jsonNode = getJsonSource().getValue(name.split("\\."));
        if (jsonNode.isPresent()) {
            return Optional.ofNullable(jsonNode.get().asText());
        }
        return Optional.empty();
    }

    @Override
    public  Optional getPropertyAs(final String name, final Class clazz) throws IllegalArgumentException {
        final Optional property = getProperty(name);
        if (!property.isPresent()) {
            return Optional.empty();
        }
        try {
            return Optional.ofNullable(_objectMapper.readValue(property.get(), clazz));
        } catch (final IOException e) {
            throw new IllegalArgumentException(
                    String.format(
                            "Unable to construct object from configuration; name=%s, class=%s, property=%s",
                            name,
                            clazz,
                            property),
                    e);
        }
    }

    @Override
    public  Optional getAs(final Class clazz) throws IllegalArgumentException {
        final Optional property = getJsonSource().getValue();
        if (!property.isPresent()) {
            return Optional.empty();
        }
        try {
            return Optional.ofNullable(_objectMapper.treeToValue(property.get(), clazz));
        } catch (final JsonProcessingException e) {
            throw new IllegalArgumentException(
                    String.format(
                            "Unable to construct object from configuration; class=%s, property=%s",
                            clazz,
                            property),
                    e);
        }
    }

    @Override
    public  Optional getPropertyAs(final String name, final Type type) throws IllegalArgumentException {
        final Optional property = getProperty(name);
        if (!property.isPresent()) {
            return Optional.empty();
        }
        try {
            final TypeFactory typeFactory = _objectMapper.getTypeFactory();
            @SuppressWarnings("unchecked")
            final Optional value = Optional.ofNullable((T) _objectMapper.readValue(
                    property.get(),
                    typeFactory.constructType(type)));
            return value;
        } catch (final IOException e) {
            throw new IllegalArgumentException(
                    String.format(
                            "Unable to construct object from configuration; name=%s, type=%s, property=%s",
                            name,
                            type,
                            property),
                    e);
        }
    }

    @Override
    public  Optional getAs(final Type type) throws IllegalArgumentException {
        final Optional property = getJsonSource().getValue();
        if (!property.isPresent()) {
            return Optional.empty();
        }
        try {
            final TypeFactory typeFactory = _objectMapper.getTypeFactory();
            @SuppressWarnings("unchecked")
            final Optional value = Optional.ofNullable((T) _objectMapper.readValue(
                    _objectMapper.treeAsTokens(property.get()), typeFactory.constructType(type)));
            return value;
        } catch (final IOException e) {
            throw new IllegalArgumentException(
                    String.format(
                            "Unable to construct object from configuration; type=%s, property=%s",
                            type,
                            property),
                    e);
        }
    }

    @LogValue
    @Override
    public Object toLogValue() {
        return LogValueMapFactory.builder(this)
                .put("super", super.toLogValue())
                .put("ObjectMapper", _objectMapper)
                .build();
    }

    /**
     * Accessor for active root JsonSource instance.
     *
     * @return Instance of JsonSource.
     */
    protected abstract JsonNodeSource getJsonSource();

    /**
     * Protected constructor.
     *
     * @param builder The Builder instance to construct from.
     */
    protected BaseJacksonConfiguration(final Builder builder) {
        _objectMapper = builder._objectMapper;
    }

    protected final ObjectMapper _objectMapper;

    /**
     * Builder for BaseObjectMapperConfiguration.
     *
     * @param  type of the builder
     * @param  type of the object to be built
     */
    protected abstract static class Builder, S extends Configuration> extends OvalBuilder {

        /**
         * Protected constructor for subclasses.
         *
         * @param targetConstructor The constructor for the concrete type to be created by this builder.
         */
        @SuppressWarnings(value = {"rawtypes", "unchecked"})
        protected Builder(final Function targetConstructor) {
            super((Function) targetConstructor);
        }

        /**
         * Set the ObjectMapper instance. Optional. Default is
         * created by ObjectMapperFactory. Cannot be null.
         *
         * @param value The ObjectMapper instance.
         * @return This Builder instance.
         */
        public T setObjectMapper(final ObjectMapper value) {
            _objectMapper = value;
            return self();
        }

        /**
         * Called by setters to always return appropriate subclass of
         * Builder, even from setters of base class.
         *
         * @return instance with correct Builder class type.
         */
        protected abstract T self();

        @NotNull
        private ObjectMapper _objectMapper = ObjectMapperFactory.getInstance();
    }
}