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

com.springleaf.cloud.discovery.config.datasource.property.DynamicCustomProperty Maven / Gradle / Ivy

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * 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.springleaf.cloud.discovery.config.datasource.property;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Implement the CustomProperty interface
 * @param 
 * @author leon
 */
public class DynamicCustomProperty implements CustomProperty {

    public static final Logger logger = LoggerFactory.getLogger(DynamicCustomProperty.class);

    protected Set> listeners = Collections.synchronizedSet(new HashSet>());
    private T value = null;

    public DynamicCustomProperty() {
    }

    public DynamicCustomProperty(T value) {
        super();
        this.value = value;
    }

    @Override
    public void addListener(PropertyListener listener) {
        listeners.add(listener);
        listener.configLoad(value);
    }

    @Override
    public void removeListener(PropertyListener listener) {
        listeners.remove(listener);
    }

    @Override
    public boolean updateValue(T newValue) {
        if (isEqual(value, newValue)) {
            return false;
        }
        logger.info("[DynamicProperty] Config will be updated to: " + newValue);

        value = newValue;
        for (PropertyListener listener : listeners) {
            listener.configUpdate(newValue);
        }
        return true;
    }

    private boolean isEqual(T oldValue, T newValue) {
        if (oldValue == null && newValue == null) {
            return true;
        }

        if (oldValue == null) {
            return false;
        }

        return oldValue.equals(newValue);
    }

    public void close() {
        listeners.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy