group.rxcloud.capa.spi.aws.config.entity.Configuration Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 group.rxcloud.capa.spi.aws.config.entity;
import group.rxcloud.capa.component.configstore.ConfigurationItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author Reckless Xu
*/
public class Configuration {
private static final Logger LOGGER = LoggerFactory.getLogger(Configuration.class);
public static final Configuration EMPTY = new Configuration<>();
@Override
public String toString() {
return "Configuration{" +
"clientConfigurationVersion='" + clientConfigurationVersion + '\'' +
", configurationItem=" + configurationItem.getContent().toString() +
", listenersCount=" + listeners.size() +
", initialized=" + initialized +
", subscribed=" + subscribed +
'}';
}
private String clientConfigurationVersion;
private ConfigurationItem configurationItem;
public final Object lock = new Object();
private final CopyOnWriteArraySet> listeners = new CopyOnWriteArraySet<>();
private AtomicBoolean initialized = new AtomicBoolean(false);
private AtomicBoolean subscribed = new AtomicBoolean(false);
public synchronized void addListener(ConfigurationListener listener) {
if (initialized.get()) {
trigger(listener, configurationItem);
}
listeners.add(listener);
}
public boolean triggers(ConfigurationItem data) {
boolean result = true;
for (ConfigurationListener listener : listeners) {
if (!trigger(listener, data)) {
result = false;
}
}
return result;
}
private boolean trigger(ConfigurationListener listener, ConfigurationItem data) {
try {
listener.onLoad(data);
return true;
} catch (Exception e) {
LOGGER.error("listener onLoad error,fileName:{},", data.getKey(), e);
return false;
}
}
public String getClientConfigurationVersion() {
return clientConfigurationVersion;
}
public void setClientConfigurationVersion(String clientConfigurationVersion) {
this.clientConfigurationVersion = clientConfigurationVersion;
}
public ConfigurationItem getConfigurationItem() {
return configurationItem;
}
public void setConfigurationItem(ConfigurationItem configurationItem) {
this.configurationItem = configurationItem;
}
public AtomicBoolean getInitialized() {
return initialized;
}
public AtomicBoolean getSubscribed() {
return subscribed;
}
}