/**
 * Copyright (C) 2011-2018 Red Hat, Inc. (https://github.com/Commonjava/indy)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.commonjava.indy.subsys.infinispan.config;

import org.apache.commons.lang.StringUtils;
import org.commonjava.indy.conf.IndyConfigInfo;
import org.commonjava.indy.conf.SystemPropertyProvider;
import org.commonjava.web.config.annotation.ConfigName;
import org.commonjava.web.config.annotation.SectionName;

import javax.enterprise.context.ApplicationScoped;
import java.io.InputStream;
import java.util.Properties;

@SectionName( "infinispan-db" )
@ApplicationScoped
public class ISPNDBConfiguration
        implements IndyConfigInfo, SystemPropertyProvider
{

    private static final String INDY_CACHE_DB_NAME = "datasource_name";

    private static final String INDY_CACHE_DB_SERVER = "datasource_server";

    private static final String INDY_CACHE_DB_PORT = "datasource_port";

    private static final String INDY_CACHE_DB_USER = "datasource_user";

    private static final String INDY_CACHE_DB_PASS = "datasource_password";

    private static final String DEFAULT_INDY_CACHE_DB_SERVER = "localhost";

    private static final String DEFAULT_INDY_CACHE_DB_PORT = "5432";

    private static final String DEFAULT_INDY_CACHE_DB_NAME = "indy";

    private static final String DEFAULT_INDY_CACHE_DB_USER = "indy";

    private static final String DEFAULT_INDY_CACHE_DB_PASS = "indy";

    private String cacheDbServer;

    private String cacheDbPort;

    private String cacheDbName;

    private String cacheDBUser;

    private String cacheDBPassword;

    public ISPNDBConfiguration()
    {
    }

    public String getCacheDbServer()
    {
        return cacheDbServer == null ? DEFAULT_INDY_CACHE_DB_SERVER : cacheDbServer;
    }

    @ConfigName( INDY_CACHE_DB_SERVER )
    public void setCacheDbServer( String cacheDbServer )
    {
        this.cacheDbServer = cacheDbServer;
    }

    public String getCacheDbPort()
    {
        return cacheDbPort == null ? DEFAULT_INDY_CACHE_DB_PORT : cacheDbPort;
    }

    @ConfigName( INDY_CACHE_DB_PORT )
    public void setCacheDbPort( String cacheDbPort )
    {
        this.cacheDbPort = cacheDbPort;
    }

    public String getCacheDbName()
    {
        return cacheDbName == null ? DEFAULT_INDY_CACHE_DB_NAME : cacheDbName;
    }

    @ConfigName( INDY_CACHE_DB_NAME )
    public void setCacheDbName( String cacheDbName )
    {
        this.cacheDbName = cacheDbName;
    }

    public String getCacheDBUser()
    {
        return cacheDBUser == null ? DEFAULT_INDY_CACHE_DB_USER : cacheDBUser;
    }

    @ConfigName( INDY_CACHE_DB_USER )
    public void setCacheDBUser( String cacheDBUser )
    {
        this.cacheDBUser = cacheDBUser;
    }

    public String getCacheDBPassword()
    {
        return cacheDBPassword == null ? DEFAULT_INDY_CACHE_DB_PASS : cacheDBPassword;
    }

    @ConfigName( INDY_CACHE_DB_PASS )
    public void setCacheDBPassword( String cacheDBPassword )
    {
        this.cacheDBPassword = cacheDBPassword;
    }

    @Override
    public String getDefaultConfigFileName()
    {
        return IndyConfigInfo.APPEND_DEFAULTS_TO_MAIN_CONF;
    }

    @Override
    public InputStream getDefaultConfig()
    {
        return Thread.currentThread().getContextClassLoader().getResourceAsStream( "default-ispndb.conf" );
    }

    @Override
    public Properties getSystemProperties()
    {
        Properties properties = new Properties();
        preparePropertyInSysEnv( properties, INDY_CACHE_DB_SERVER, getCacheDbServer() );
        preparePropertyInSysEnv( properties, INDY_CACHE_DB_PORT, getCacheDbPort() );
        preparePropertyInSysEnv( properties, INDY_CACHE_DB_NAME, getCacheDbName() );
        preparePropertyInSysEnv( properties, INDY_CACHE_DB_USER, getCacheDBUser() );
        preparePropertyInSysEnv( properties, INDY_CACHE_DB_PASS, getCacheDBPassword() );

        return properties;
    }

    private void preparePropertyInSysEnv( Properties props, String propName, String ifNotInSysEnv )
    {
        String envVal = System.getenv( propName );
        if ( StringUtils.isBlank( envVal ) )
        {
            envVal = System.getProperty( propName );
        }
        if ( StringUtils.isNotBlank( envVal ) )
        {
            props.setProperty( propName, envVal );
        }
        else
        {
            props.setProperty( propName, ifNotInSysEnv );
        }
    }
}

@@ -4572,19 +4572,20 @@
  String 
-env
+prop
 Val = Sy
@@ -4639,27 +4639,28 @@
 ls.isBlank( 
-env
+prop
 Val ) )%0A    
@@ -4677,19 +4677,20 @@
         
-env
+prop
 Val = Sy
@@ -4760,21 +4760,19 @@
 s.is
-Not
 Blank( 
-env
+prop
 Val 
@@ -4805,41 +4805,27 @@
 prop
-s.setProperty( propName, envVal )
+Val = ifNotInSysEnv
 ;%0A  
@@ -4832,24 +4832,25 @@
       %7D%0A
+%0A
         
 else%0A   
@@ -4845,27 +4845,106 @@
     
-else%0A        %7B%0A    
+propVal = propVal.replace( %22%5C%22%22, %22&quot;%22 );%0A        propVal = propVal.replace( %22'%22, %22&apos;%22 );%0A%0A
     
@@ -4980,34 +4980,18 @@
 me, 
-ifNotInSysEnv );%0A        %7D
+propVal );
 %0A   

Fix problem of indy db secrets contain quote and double quote