package com.emc.ecs.cloudfoundry.broker.config;

import com.emc.ecs.cloudfoundry.broker.EcsManagementClientException;
import com.emc.ecs.cloudfoundry.broker.EcsManagementResourceNotFoundException;
import com.emc.ecs.cloudfoundry.broker.repository.ServiceInstanceBindingRepository;
import com.emc.ecs.cloudfoundry.broker.repository.ServiceInstanceRepository;
import com.emc.ecs.cloudfoundry.broker.service.EcsService;
import com.emc.ecs.cloudfoundry.broker.service.EcsServiceInstanceBindingService;
import com.emc.ecs.cloudfoundry.broker.service.EcsServiceInstanceService;
import com.emc.ecs.management.sdk.Connection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.servicebroker.model.BrokerApiVersion;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import java.net.URISyntaxException;
import java.net.URL;

@EnableAutoConfiguration
@ComponentScan
public class Application {

//    static {
//        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
//                new javax.net.ssl.HostnameVerifier(){
//
//                    public boolean verify(String hostname,
//                                          javax.net.ssl.SSLSession sslSession) {
//                        return true;
//                    }
//                });
//    }
//
    @Autowired
    private BrokerConfig broker;

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public Connection ecsConnection() {
	if (broker.getCertificate() != null) {
        URL certificate = Thread.currentThread().getContextClassLoader()
                .getResource(broker.getCertificate());
        return new Connection(broker.getManagementEndpoint(),
                broker.getUsername(), broker.getPassword(), certificate);
	} else {
		return new Connection(broker.getManagementEndpoint(),
                broker.getUsername(), broker.getPassword());
	}
    }

    @Bean
    public BrokerApiVersion brokerApiVersion() {
        return new BrokerApiVersion(broker.getBrokerApiVersion());
    }

    @Bean
    public EcsService ecsService() throws EcsManagementClientException,
            EcsManagementResourceNotFoundException {
        return new EcsService();
    }

    @Bean
    public EcsServiceInstanceBindingService ecsServiceInstanceBindingService()
            throws EcsManagementClientException,
            EcsManagementResourceNotFoundException, URISyntaxException {
        return new EcsServiceInstanceBindingService();
    }

    @Bean
    public ServiceInstanceRepository serviceInstanceRepository()
            throws EcsManagementClientException,
            EcsManagementResourceNotFoundException, URISyntaxException {
        return new ServiceInstanceRepository();
    }

    @Bean
    public EcsServiceInstanceService ecsServiceInstanceService()
            throws EcsManagementClientException,
            EcsManagementResourceNotFoundException, URISyntaxException {
        return new EcsServiceInstanceService();
    }

    @Bean
    public ServiceInstanceBindingRepository serviceInstanceBindingRepository()
            throws EcsManagementClientException,
            EcsManagementResourceNotFoundException, URISyntaxException {
        return new ServiceInstanceBindingRepository();
    }

    public BrokerConfig getBroker() {
        return broker;
    }

    public void setBroker(BrokerConfig broker) {
        this.broker = broker;
    }
}
@@ -607,24 +607,81 @@
 Connection;%0A
+import org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;%0A
 import org.s
@@ -731,16 +731,16 @@
 owired;%0A
-
 import o
@@ -1164,16 +1164,102 @@
 tion %7B%0A%0A
+    private static final Logger logger = LoggerFactory.getLogger(Application.class);%0A%0A
 //    st
@@ -1878,24 +1878,91 @@
  != null) %7B%0A
+%09    logger.info(%22Instantiating esc connection with certificate%22);%0A
         URL 
@@ -2213,16 +2213,16 @@
 icate);%0A
-
 %09%7D else 
@@ -2223,16 +2223,81 @@
  else %7B%0A
+        logger.info(%22Instantiating unencrypted esc connection%22);%0A
 %09%09return

add logging