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 if (key.isDebug()) { 054 LOG.debug("Load of: " + key); 055 } 056 } catch (IOException e) { 057 LOG.error("Failed to load: " + key + ", reason:" + e.getLocalizedMessage()); 058 if (key.isDebug()) { 059 LOG.debug("Load of: " + key + ", failure exception" + e); 060 } 061 } 062 reloadTime = System.currentTimeMillis(); 063 } 064 return this; 065 } 066 067 public synchronized Map<String, String> invertedPropertiesMap() { 068 if (invertedProps == null) { 069 invertedProps = new HashMap<>(props.size()); 070 for (Map.Entry<Object, Object> val : props.entrySet()) { 071 invertedProps.put((String) val.getValue(), (String) val.getKey()); 072 } 073 } 074 return invertedProps; 075 } 076 077 public synchronized Map<String, Set<String>> invertedPropertiesValuesMap() { 078 if (invertedValueProps == null) { 079 invertedValueProps = new HashMap<>(props.size()); 080 for (Map.Entry<Object, Object> val : props.entrySet()) { 081 String[] userList = ((String)val.getValue()).split(","); 082 for (String user : userList) { 083 Set<String> set = invertedValueProps.get(user); 084 if (set == null) { 085 set = new HashSet<>(); 086 invertedValueProps.put(user, set); 087 } 088 set.add((String)val.getKey()); 089 } 090 } 091 } 092 return invertedValueProps; 093 } 094 095 private void load(final File source, Properties props) throws IOException { 096 FileInputStream in = new FileInputStream(source); 097 try { 098 props.load(in); 099 if (key.isDecrypt()) { 100 try { 101 EncryptionSupport.decrypt(this.props); 102 } catch (NoClassDefFoundError e) { 103 // this Happens whe jasypt is not on the classpath.. 104 key.setDecrypt(false); 105 LOG.info("jasypt is not on the classpath: password decryption disabled."); 106 } 107 } 108 109 } finally { 110 in.close(); 111 } 112 } 113 114 private boolean hasModificationAfter(long reloadTime) { 115 return key.file.lastModified() > reloadTime; 116 } 117 118}