com.pandich.dropwizard.curator.refresh.MethodRefresher.java Source code

Java tutorial

Introduction

Here is the source code for com.pandich.dropwizard.curator.refresh.MethodRefresher.java

Source

/*
 * This file is part of dropwizard-curator.
 *
 * dropwizard-curator is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * dropwizard-curator is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with dropwizard-curator.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.pandich.dropwizard.curator.refresh;

import com.google.common.base.Predicate;
import com.pandich.dropwizard.curator.CuratorRootConfiguration;
import com.pandich.dropwizard.curator.PropertySources.PropertySource;
import com.pandich.dropwizard.curator.mapper.CuratorMapper;
import org.apache.commons.lang3.reflect.MethodUtils;
import org.apache.curator.framework.CuratorFramework;

import javax.annotation.Nullable;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.regex.Pattern;

import static java.lang.reflect.Modifier.isStatic;

public final class MethodRefresher extends Refresher<Method> {

    private static final Pattern SETTER_METHOD_NAME = Pattern.compile("^set[^a-z].*$");

    public static final Predicate<Method> methodIsValid = new Predicate<Method>() {
        @Override
        public boolean apply(@Nullable final Method input) {

            return input != null && !isStatic(input.getModifiers()) && input.getReturnType().equals(Void.TYPE)
                    && input.getParameterTypes().length == 1
                    && SETTER_METHOD_NAME.matcher(input.getName()).matches();
        }
    };

    public MethodRefresher(final Map<Class<?>, CuratorMapper<?>> mappers, final CuratorFramework client,
            final CuratorRootConfiguration configuration) {
        super(mappers, client, configuration);
    }

    @Override
    protected Method doCastMember(final Object o) {
        return (Method) o;
    }

    @Override
    protected Class<?> getType(final Method element) {
        return element.getParameterTypes()[0];
    }

    @Override
    protected void doWrite(final Method element, final Object value) throws ReflectiveOperationException {
        MethodUtils.invokeMethod(this.configuration, element.getName(), value);
    }

    @Override
    protected void register(final Class<? extends CuratorRootConfiguration> configurationClass,
            final Method element, final PropertySource source) {
        this.propertySources.register(configurationClass, element, source);
    }
}