Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright 2014 Netflix, Inc.
*
* 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.netflix.config;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.configuration.AbstractConfiguration;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertyConverter;
import org.apache.commons.configuration.event.ConfigurationErrorEvent;
import org.apache.commons.configuration.event.ConfigurationErrorListener;
import org.apache.commons.configuration.event.ConfigurationEvent;
import org.apache.commons.configuration.event.ConfigurationListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.netflix.config.validation.ValidationException;
/**
* This class uses a ConcurrentHashMap for reading/writing a property to achieve high
* throughput and thread safety. The implementation is lock free for {@link #getProperty(String)}
* and {@link #setProperty(String, Object)}, but has some synchronization cost for
* {@link #addProperty(String, Object)} if the object to add is not a String or the key already exists.
*
* The methods from AbstractConfiguration related to listeners and event generation are overridden
* so that adding/deleting listeners and firing events are no longer synchronized.
* Also, it catches Throwable when it invokes the listeners, making
* it more robust.
*
* This configuration does not allow null as key or value and will throw NullPointerException
* when trying to add or set properties with empty key or value.
*
* @author awang
*
*/
public class ConcurrentMapConfiguration extends AbstractConfiguration {
protected ConcurrentHashMap map;
private Collection listeners = new CopyOnWriteArrayList();
private Collection errorListeners = new CopyOnWriteArrayList();
private static final Logger logger = LoggerFactory.getLogger(ConcurrentMapConfiguration.class);
private static final int NUM_LOCKS = 32;
private ReentrantLock[] locks = new ReentrantLock[NUM_LOCKS];
/**
* System property to disable delimiter parsing Apache Commons configurations
*/
public static final String DISABLE_DELIMITER_PARSING = "archaius.configuration.disableDelimiterParsing";
/**
* Create an instance with an empty map.
*/
public ConcurrentMapConfiguration() {
map = new ConcurrentHashMap();
for (int i = 0; i < NUM_LOCKS; i++) {
locks[i] = new ReentrantLock();
}
String disableDelimiterParsing = System.getProperty(DISABLE_DELIMITER_PARSING, "false");
super.setDelimiterParsingDisabled(Boolean.valueOf(disableDelimiterParsing));
}
public ConcurrentMapConfiguration(Map mapToCopy) {
this();
map = new ConcurrentHashMap(mapToCopy);
}
/**
* Create an instance by copying the properties from an existing Configuration.
* Future changes to the Configuration passed in will not be reflected in this
* object.
*
* @param config Configuration to be copied
*/
public ConcurrentMapConfiguration(Configuration config) {
this();
for (Iterator i = config.getKeys(); i.hasNext();) {
String name = (String) i.next();
Object value = config.getProperty(name);
map.put(name, value);
}
}
public Object getProperty(String key)
{
return map.get(key);
}
protected void addPropertyDirect(String key, Object value)
{
ReentrantLock lock = locks[Math.abs(key.hashCode()) % NUM_LOCKS];
lock.lock();
try {
Object previousValue = map.putIfAbsent(key, value);
if (previousValue == null) {
return;
}
if (previousValue instanceof List)
{
// the value is added to the existing list
((List) previousValue).add(value);
}
else
{
// the previous value is replaced by a list containing the previous value and the new value
List