/*
 * oxAuth is available under the MIT License (2008). See http://opensource.org/licenses/MIT for full text.
 *
 * Copyright (c) 2014, Gluu
 */

package org.xdi.oxauth.service;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;

import org.slf4j.Logger;
import org.xdi.model.SmtpConfiguration;
import org.xdi.oxauth.crypto.signature.SHA256withECDSASignatureVerification;
import org.xdi.oxauth.model.appliance.GluuAppliance;
import org.xdi.service.cache.CacheConfiguration;
import org.xdi.service.cache.InMemoryConfiguration;
import org.xdi.util.StringHelper;
import org.xdi.util.security.StringEncrypter.EncryptionException;

/**
 * Holds factory methods to create services
 *
 * @author Yuriy Movchan Date: 05/22/2015
 */
@ApplicationScoped
@Named
public class ApplicationFactory {

    @Inject
    private Logger log;
    
    @Inject
    private ApplianceService applianceService;
    
    @Inject
    private EncryptionService encryptionService;

    @Produces @ApplicationScoped @Named("sha256withECDSASignatureVerification")
    public SHA256withECDSASignatureVerification getBouncyCastleSignatureVerification() {
        return new SHA256withECDSASignatureVerification();
    }

	@Produces @ApplicationScoped
	public CacheConfiguration getCacheConfiguration() {
		CacheConfiguration cacheConfiguration = applianceService.getAppliance().getCacheConfiguration();
		if (cacheConfiguration == null || cacheConfiguration.getCacheProviderType() == null) {
			log.error("Failed to read cache configuration from LDAP. Please check appliance oxCacheConfiguration attribute " +
					"that must contain cache configuration JSON represented by CacheConfiguration.class. Applieance DN: " + applianceService.getAppliance().getDn());
			log.info("Creating fallback IN-MEMORY cache configuration ... ");

			cacheConfiguration = new CacheConfiguration();
			cacheConfiguration.setInMemoryConfiguration(new InMemoryConfiguration());

			log.info("IN-MEMORY cache configuration is created.");
		}
		log.info("Cache configuration: " + cacheConfiguration);
		return cacheConfiguration;
	}

	@Produces @ApplicationScoped
	public SmtpConfiguration getSmtpConfiguration() {
		GluuAppliance appliance = applianceService.getAppliance();
		SmtpConfiguration smtpConfiguration = appliance.getSmtpConfiguration();
		
		if (smtpConfiguration == null) {
			return null;
		}

		String password = smtpConfiguration.getPassword();
		if (StringHelper.isNotEmpty(password)) {
			try {
				smtpConfiguration.setPasswordDecrypted(encryptionService.decrypt(password));
			} catch (EncryptionException ex) {
				log.error("Failed to decript SMTP user password", ex);
			}
		}
		
		return smtpConfiguration;
	}

}
@@ -519,109 +519,8 @@
 ce;%0A
-import org.xdi.service.cache.CacheConfiguration;%0Aimport org.xdi.service.cache.InMemoryConfiguration;%0A
 impo
@@ -1180,899 +1180,8 @@
  %7D%0A%0A
-%09@Produces @ApplicationScoped%0A%09public CacheConfiguration getCacheConfiguration() %7B%0A%09%09CacheConfiguration cacheConfiguration = applianceService.getAppliance().getCacheConfiguration();%0A%09%09if (cacheConfiguration == null %7C%7C cacheConfiguration.getCacheProviderType() == null) %7B%0A%09%09%09log.error(%22Failed to read cache configuration from LDAP. Please check appliance oxCacheConfiguration attribute %22 +%0A%09%09%09%09%09%22that must contain cache configuration JSON represented by CacheConfiguration.class. Applieance DN: %22 + applianceService.getAppliance().getDn());%0A%09%09%09log.info(%22Creating fallback IN-MEMORY cache configuration ... %22);%0A%0A%09%09%09cacheConfiguration = new CacheConfiguration();%0A%09%09%09cacheConfiguration.setInMemoryConfiguration(new InMemoryConfiguration());%0A%0A%09%09%09log.info(%22IN-MEMORY cache configuration is created.%22);%0A%09%09%7D%0A%09%09log.info(%22Cache configuration: %22 + cacheConfiguration);%0A%09%09return cacheConfiguration;%0A%09%7D%0A%0A
 %09@Pr

Remove unused CR config producer