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.util.HashMap;
021import java.util.Map;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024
025public class PropertiesLoader {
026    private static final Logger LOG = LoggerFactory.getLogger(PropertiesLoader.class);
027    static Map<FileNameKey, ReloadableProperties> staticCache = new HashMap<FileNameKey, ReloadableProperties>();
028    protected boolean debug;
029
030    public void init(Map options) {
031        debug = booleanOption("debug", options);
032        if (debug) {
033            LOG.debug("Initialized debug");
034        }
035    }
036
037    public ReloadableProperties load(String nameProperty, String fallbackName, Map options) {
038        ReloadableProperties result;
039        FileNameKey key = new FileNameKey(nameProperty, fallbackName, options);
040        key.setDebug(debug);
041
042        synchronized (staticCache) {
043            result = staticCache.get(key);
044            if (result == null) {
045                result = new ReloadableProperties(key);
046                staticCache.put(key, result);
047            }
048        }
049
050        return result.obtained();
051    }
052
053    private static boolean booleanOption(String name, Map options) {
054        return Boolean.parseBoolean((String) options.get(name));
055    }
056
057    public class FileNameKey {
058        final File file;
059        final String absPath;
060        final boolean reload;
061        private boolean decrypt;
062        private boolean debug;
063
064        public FileNameKey(String nameProperty, String fallbackName, Map options) {
065            this.file = new File(baseDir(options), stringOption(nameProperty, fallbackName, options));
066            absPath = file.getAbsolutePath();
067            reload = booleanOption("reload", options);
068            decrypt = booleanOption("decrypt", options);
069        }
070
071        @Override
072        public boolean equals(Object other) {
073            return other instanceof FileNameKey && this.absPath.equals(((FileNameKey) other).absPath);
074        }
075
076        @Override
077        public int hashCode() {
078            return this.absPath.hashCode();
079        }
080
081        public boolean isReload() {
082            return reload;
083        }
084
085        public File file() {
086            return file;
087        }
088
089        public boolean isDecrypt() {
090            return decrypt;
091        }
092
093        public void setDecrypt(boolean decrypt) {
094            this.decrypt = decrypt;
095        }
096
097        private String stringOption(String key, String nameDefault, Map options) {
098            Object result = options.get(key);
099            return result != null ? result.toString() : nameDefault;
100        }
101
102        private File baseDir(Map options) {
103            File baseDir = null;
104            if (options.get("baseDir") != null) {
105                baseDir = new File((String) options.get("baseDir"));
106            } else {
107                if (System.getProperty("java.security.auth.login.config") != null) {
108                    baseDir = new File(System.getProperty("java.security.auth.login.config")).getParentFile();
109                }
110            }
111            if (debug) {
112                LOG.debug("Using basedir=" + baseDir.getAbsolutePath());
113            }
114            return baseDir;
115        }
116
117        @Override
118        public String toString() {
119            return "PropsFile=" + absPath;
120        }
121
122        public void setDebug(boolean debug) {
123            this.debug = debug;
124        }
125
126        public boolean isDebug() {
127            return debug;
128        }
129    }
130
131    /**
132     * For test-usage only.
133     */
134    public static void resetUsersAndGroupsCache() {
135        staticCache.clear();
136    }
137}