org.apache.activemq.artemis.spi.core.security.jaas.ReloadableProperties 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 org.apache.activemq.artemis.spi.core.security.jaas;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import org.apache.activemq.artemis.core.server.ActiveMQServerLogger;
import org.jboss.logging.Logger;
public class ReloadableProperties {
private static final Logger logger = Logger.getLogger(ReloadableProperties.class);
// use this whenever writing to the underlying properties files from another component
public static final ReadWriteLock LOCK = new ReentrantReadWriteLock();
private Properties props = new Properties();
private Map invertedProps;
private Map> invertedValueProps;
private Map regexpProps;
private long reloadTime = -1;
private final PropertiesLoader.FileNameKey key;
public ReloadableProperties(PropertiesLoader.FileNameKey key) {
this.key = key;
}
public synchronized Properties getProps() {
return props;
}
public synchronized ReloadableProperties obtained() {
if (reloadTime < 0 || (key.isReload() && hasModificationAfter(reloadTime))) {
props = new Properties();
try {
load(key.file(), props);
invertedProps = null;
invertedValueProps = null;
regexpProps = null;
if (key.isDebug()) {
logger.debug("Load of: " + key);
}
} catch (IOException e) {
ActiveMQServerLogger.LOGGER.failedToLoadProperty(e, key.toString(), e.getLocalizedMessage());
if (key.isDebug()) {
logger.debug("Load of: " + key + ", failure exception" + e);
}
}
reloadTime = System.currentTimeMillis();
}
return this;
}
public synchronized Map invertedPropertiesMap() {
if (invertedProps == null) {
invertedProps = new HashMap<>(props.size());
for (Map.Entry