package org.apache.tomcat.vault.util;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.tomcat.util.IntrospectionUtils.PropertySource;
import org.apache.tomcat.vault.security.vault.SecurityVault;
import org.apache.tomcat.vault.security.vault.SecurityVaultException;
import org.apache.tomcat.vault.security.vault.SecurityVaultFactory;
import org.apache.tomcat.vault.security.vault.PicketBoxSecurityVault;

import org.apache.juli.logging.Log;
import org.apache.juli.logging.LogFactory;

public class PropertySourceVault implements PropertySource {
    private static final Log log = LogFactory.getLog(PropertySourceVault.class);

    private String PROPERTY_FILE_RELATIVE_PATH = "/conf/vault.properties";

    private SecurityVault vault;
    private PropertyFileManager pfm;
    private Properties properties;

    public PropertySourceVault() {
        vault = null;
        properties = null;
        String catalinaHome = System.getProperty("catalina.home");
        if (catalinaHome == null)
           catalinaHome = System.getProperty("catalina.base");
        if (catalinaHome == null) {
           // Here probably need to guess the location...
           catalinaHome = ".";
        }
        pfm = new PropertyFileManager(catalinaHome + PROPERTY_FILE_RELATIVE_PATH);

        this.init();
    }

    public void init() {
        try {
            vault = SecurityVaultFactory.get();

            // Load vault property file
            properties = pfm.load();

            Map<String, Object> options = new HashMap<String, Object>();
            options.put(PicketBoxSecurityVault.KEYSTORE_URL, properties.getProperty("KEYSTORE_URL"));
            options.put(PicketBoxSecurityVault.KEYSTORE_PASSWORD, properties.getProperty("KEYSTORE_PASSWORD"));
            options.put(PicketBoxSecurityVault.KEYSTORE_ALIAS, properties.getProperty("KEYSTORE_ALIAS"));
            options.put(PicketBoxSecurityVault.SALT, properties.getProperty("SALT"));
            options.put(PicketBoxSecurityVault.ITERATION_COUNT, properties.getProperty("ITERATION_COUNT"));
            options.put(PicketBoxSecurityVault.ENC_FILE_DIR, properties.getProperty("ENC_FILE_DIR"));

            vault.init(options);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public String getProperty(String arg0) {
        String result = null;

        if (arg0.startsWith("VAULT::")) {
            String vaultdata[] = arg0.split("::");
            if (vaultdata.length == 3) {
                if (vault.isInitialized()) {
                    try {
                        result = new String(vault.retrieve(vaultdata[1], vaultdata[2], null));
                    } catch (SecurityVaultException e) {
                        log.error(e.getMessage(), e);
                    }
                }
            }
        }
        return result;
    }
}

@@ -108,16 +108,37 @@
 perties;
+%0Aimport java.io.File;
 %0A%0Aimport
@@ -906,32 +906,37 @@
 ult() %7B%0A        
+this.
 vault = null;%0A  
@@ -933,32 +933,37 @@
 = null;%0A        
+this.
 properties = nul
@@ -957,32 +957,33 @@
 perties = null;%0A
+%0A
         String c
@@ -1037,36 +1037,39 @@
 %22);%0A        
-if (
+String 
 catalina
 Home == null
@@ -1060,117 +1060,364 @@
 lina
-Home == null)%0A           catalinaHome = System.getProperty(%22catalina.base%22);%0A        if (catalinaHome == null
+Base = System.getProperty(%22catalina.base%22);%0A        String catalina = null;%0A%0A        if (new File(catalinaBase + PROPERTY_FILE_RELATIVE_PATH).exists()) %7B%0A            catalina = catalinaBase;%0A            log.debug(%22vault.properties found in catalina.base %5B%22 + catalina + %22%5D%22);%0A        %7D else if (new File(catalinaHome + PROPERTY_FILE_RELATIVE_PATH).exists()
 ) %7B%0A
@@ -1431,84 +1431,411 @@
     
+ 
 // 
-Here probably need to guess the location...%0A           catalinaHome = %22.%22
+Using catalina.home is kept for backwards compat%0A            catalina = catalinaHome;%0A            log.debug(%22vault.properties found in catalina.home %5B%22 + catalina + %22%5D%22);%0A        %7D else %7B%0A            // Always default to catalina.base if it doesn't exist in either place%0A            catalina = catalinaBase;%0A            log.debug(%22vault.properties not found, using catalina.base %5B%22 + catalina + %22%5D%22)
 ;%0A  
@@ -1842,24 +1842,25 @@
       %7D%0A
+%0A
         
 pfm = ne
@@ -1851,16 +1851,21 @@
         
+this.
 pfm = ne
@@ -1886,36 +1886,32 @@
 Manager(catalina
-Home
  + PROPERTY_FILE

Check catalina.base before catalina.home like tomcat does