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 java.util.regex.Pattern;
028import java.util.regex.PatternSyntaxException;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032public class ReloadableProperties {
033    private static final Logger LOG = LoggerFactory.getLogger(ReloadableProperties.class);
034
035    private Properties props = new Properties();
036    private Map<String, String> invertedProps;
037    private Map<String, Set<String>> invertedValueProps;
038    private Map<String, Pattern> regexpProps;
039    private long reloadTime = -1;
040    private final PropertiesLoader.FileNameKey key;
041
042    public ReloadableProperties(PropertiesLoader.FileNameKey key) {
043        this.key = key;
044    }
045
046    public synchronized Properties getProps() {
047        return props;
048    }
049
050    public synchronized ReloadableProperties obtained() {
051        if (reloadTime < 0 || (key.isReload() && hasModificationAfter(reloadTime))) {
052            props = new Properties();
053            try {
054                load(key.file(), props);
055                invertedProps = null;
056                invertedValueProps = null;
057                regexpProps = null;
058                if (key.isDebug()) {
059                    LOG.debug("Load of: " + key);
060                }
061            } catch (IOException e) {
062                LOG.error("Failed to load: " + key + ", reason:" + e.getLocalizedMessage());
063                if (key.isDebug()) {
064                    LOG.debug("Load of: " + key + ", failure exception" + e);
065                }
066            }
067            reloadTime = System.currentTimeMillis();
068        }
069        return this;
070    }
071
072    public synchronized Map<String, String> invertedPropertiesMap() {
073        if (invertedProps == null) {
074            invertedProps = new HashMap<>(props.size());
075            for (Map.Entry<Object, Object> val : props.entrySet()) {
076                String str = (String) val.getValue();
077                if (!looksLikeRegexp(str)) {
078                    invertedProps.put(str, (String) val.getKey());
079                }
080            }
081        }
082        return invertedProps;
083    }
084
085    public synchronized Map<String, Set<String>> invertedPropertiesValuesMap() {
086        if (invertedValueProps == null) {
087            invertedValueProps = new HashMap<>(props.size());
088            for (Map.Entry<Object, Object> val : props.entrySet()) {
089                String[] userList = ((String)val.getValue()).split(",");
090                for (String user : userList) {
091                    Set<String> set = invertedValueProps.get(user);
092                    if (set == null) {
093                        set = new HashSet<>();
094                        invertedValueProps.put(user, set);
095                    }
096                    set.add((String)val.getKey());
097                }
098            }
099        }
100        return invertedValueProps;
101    }
102
103    public synchronized Map<String, Pattern> regexpPropertiesMap() {
104        if (regexpProps == null) {
105            regexpProps = new HashMap<>(props.size());
106            for (Map.Entry<Object, Object> val : props.entrySet()) {
107                String str = (String) val.getValue();
108                if (looksLikeRegexp(str)) {
109                    try {
110                        Pattern p = Pattern.compile(str.substring(1, str.length() - 1));
111                        regexpProps.put((String) val.getKey(), p);
112                    } catch (PatternSyntaxException e) {
113                        LOG.warn("Ignoring invalid regexp: " + str);
114                    }
115                }
116            }
117        }
118        return regexpProps;
119    }
120
121    private void load(final File source, Properties props) throws IOException {
122        FileInputStream in = new FileInputStream(source);
123        try {
124            props.load(in);
125            if (key.isDecrypt()) {
126                try {
127                    EncryptionSupport.decrypt(this.props, key.getAlgorithm());
128                } catch (NoClassDefFoundError e) {
129                    // this Happens whe jasypt is not on the classpath..
130                    key.setDecrypt(false);
131                    LOG.info("jasypt is not on the classpath: password decryption disabled.");
132                }
133            }
134
135        } finally {
136            in.close();
137        }
138    }
139
140    private boolean hasModificationAfter(long reloadTime) {
141        return key.file.lastModified() > reloadTime;
142    }
143
144    private boolean looksLikeRegexp(String str) {
145        int len = str.length();
146        return len > 2 && str.charAt(0) == '/' && str.charAt(len - 1) == '/';
147    }
148
149}