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        public int hashCode() {
077            return this.absPath.hashCode();
078        }
079
080        public boolean isReload() {
081            return reload;
082        }
083
084        public File file() {
085            return file;
086        }
087
088        public boolean isDecrypt() {
089            return decrypt;
090        }
091
092        public void setDecrypt(boolean decrypt) {
093            this.decrypt = decrypt;
094        }
095
096        private String stringOption(String key, String nameDefault, Map options) {
097            Object result = options.get(key);
098            return result != null ? result.toString() : nameDefault;
099        }
100
101        private File baseDir(Map options) {
102            File baseDir = null;
103            if (options.get("baseDir") != null) {
104                baseDir = new File((String) options.get("baseDir"));
105            } else {
106                if (System.getProperty("java.security.auth.login.config") != null) {
107                    baseDir = new File(System.getProperty("java.security.auth.login.config")).getParentFile();
108                }
109            }
110            if (debug) {
111                LOG.debug("Using basedir=" + baseDir.getAbsolutePath());
112            }
113            return baseDir;
114        }
115
116        public String toString() {
117            return "PropsFile=" + absPath;
118        }
119
120        public void setDebug(boolean debug) {
121            this.debug = debug;
122        }
123
124        public boolean isDebug() {
125            return debug;
126        }
127    }
128
129    /**
130     * For test-usage only.
131     */
132    public static void resetUsersAndGroupsCache() {
133        staticCache.clear();
134    }
135}