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.HashSet;
024import java.util.Map;
025import java.util.Properties;
026import java.util.Set;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029
030public class ReloadableProperties {
031    private static final Logger LOG = LoggerFactory.getLogger(ReloadableProperties.class);
032
033    private Properties props = new Properties();
034    private Map<String, String> invertedProps;
035    private Map<String, Set<String>> invertedValueProps;
036    private long reloadTime = -1;
037    private final PropertiesLoader.FileNameKey key;
038
039    public ReloadableProperties(PropertiesLoader.FileNameKey key) {
040        this.key = key;
041    }
042
043    public synchronized Properties getProps() {
044        return props;
045    }
046
047    public synchronized ReloadableProperties obtained() {
048        if (reloadTime < 0 || (key.isReload() && hasModificationAfter(reloadTime))) {
049            props = new Properties();
050            try {
051                load(key.file(), props);
052                invertedProps = null;
053                invertedValueProps = null;
054                if (key.isDebug()) {
055                    LOG.debug("Load of: " + key);
056                }
057            } catch (IOException e) {
058                LOG.error("Failed to load: " + key + ", reason:" + e.getLocalizedMessage());
059                if (key.isDebug()) {
060                    LOG.debug("Load of: " + key + ", failure exception" + e);
061                }
062            }
063            reloadTime = System.currentTimeMillis();
064        }
065        return this;
066    }
067
068    public synchronized Map<String, String> invertedPropertiesMap() {
069        if (invertedProps == null) {
070            invertedProps = new HashMap<>(props.size());
071            for (Map.Entry<Object, Object> val : props.entrySet()) {
072                invertedProps.put((String) val.getValue(), (String) val.getKey());
073            }
074        }
075        return invertedProps;
076    }
077
078    public synchronized Map<String, Set<String>> invertedPropertiesValuesMap() {
079        if (invertedValueProps == null) {
080            invertedValueProps = new HashMap<>(props.size());
081            for (Map.Entry<Object, Object> val : props.entrySet()) {
082                String[] userList = ((String)val.getValue()).split(",");
083                for (String user : userList) {
084                    Set<String> set = invertedValueProps.get(user);
085                    if (set == null) {
086                        set = new HashSet<>();
087                        invertedValueProps.put(user, set);
088                    }
089                    set.add((String)val.getKey());
090                }
091            }
092        }
093        return invertedValueProps;
094    }
095
096    private void load(final File source, Properties props) throws IOException {
097        FileInputStream in = new FileInputStream(source);
098        try {
099            props.load(in);
100            if (key.isDecrypt()) {
101                try {
102                    EncryptionSupport.decrypt(this.props);
103                } catch (NoClassDefFoundError e) {
104                    // this Happens whe jasypt is not on the classpath..
105                    key.setDecrypt(false);
106                    LOG.info("jasypt is not on the classpath: password decryption disabled.");
107                }
108            }
109
110        } finally {
111            in.close();
112        }
113    }
114
115    private boolean hasModificationAfter(long reloadTime) {
116        return key.file.lastModified() > reloadTime;
117    }
118
119}