001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.jaas;
018
019import java.io.File;
020import java.io.FileInputStream;
021import java.io.IOException;
022import java.util.HashMap;
023import java.util.Map;
024import java.util.Properties;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028public class ReloadableProperties {
029    private static final Logger LOG = LoggerFactory.getLogger(ReloadableProperties.class);
030
031    private Properties props = new Properties();
032    private Map<String, String> invertedProps;
033    private long reloadTime = -1;
034    private final PropertiesLoader.FileNameKey key;
035
036    public ReloadableProperties(PropertiesLoader.FileNameKey key) {
037        this.key = key;
038    }
039
040    public synchronized Properties getProps() {
041        return props;
042    }
043
044    public synchronized ReloadableProperties obtained() {
045        if (reloadTime < 0 || (key.isReload() && hasModificationAfter(reloadTime))) {
046            props = new Properties();
047            try {
048                load(key.file(), props);
049                invertedProps = null;
050                if (key.isDebug()) {
051                    LOG.debug("Load of: " + key);
052                }
053            } catch (IOException e) {
054                LOG.error("Failed to load: " + key + ", reason:" + e.getLocalizedMessage());
055                if (key.isDebug()) {
056                    LOG.debug("Load of: " + key + ", failure exception" + e);
057                }
058            }
059            reloadTime = System.currentTimeMillis();
060        }
061        return this;
062    }
063
064    public synchronized Map<String, String> invertedPropertiesMap() {
065        if (invertedProps == null) {
066            invertedProps = new HashMap<>(props.size());
067            for (Map.Entry<Object, Object> val : props.entrySet()) {
068                invertedProps.put((String) val.getValue(), (String) val.getKey());
069            }
070        }
071        return invertedProps;
072    }
073
074    private void load(final File source, Properties props) throws IOException {
075        FileInputStream in = new FileInputStream(source);
076        try {
077            props.load(in);
078            if (key.isDecrypt()) {
079                try {
080                    EncryptionSupport.decrypt(this.props);
081                } catch (NoClassDefFoundError e) {
082                    // this Happens whe jasypt is not on the classpath..
083                    key.setDecrypt(false);
084                    LOG.info("jasypt is not on the classpath: password decryption disabled.");
085                }
086            }
087
088        } finally {
089            in.close();
090        }
091    }
092
093    private boolean hasModificationAfter(long reloadTime) {
094        return key.file.lastModified() > reloadTime;
095    }
096
097}