// Path: apple-config-core/src/main/java/com/appleframework/config/core/spi/NamespaceLoadSpi.java
// public interface NamespaceLoadSpi {
// 
// 	public Set<String> load();
// 	
// }


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import com.appleframework.config.core.spi.NamespaceLoadSpi;
import com.ctrip.framework.apollo.core.spi.Ordered;
import com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor;
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValueProcessor;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.SpringValueProcessor;
import com.ctrip.framework.apollo.spring.property.SpringValueDefinitionProcessor;
import com.ctrip.framework.apollo.spring.spi.ApolloConfigRegistrarHelper;
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil;
import com.google.common.collect.Lists;
package com.appleframework.config;




public class AppleApolloConfigRegistrarHelper implements ApolloConfigRegistrarHelper {

	  @Override
	  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	    AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableApolloConfig.class.getName()));
	    String[] namespaces = attributes.getStringArray("value");
	    int order = attributes.getNumber("order");
	    ApplePropertySourcesProcessor.addNamespaces(Lists.newArrayList(namespaces), order++);
	    
	    ServiceLoader<NamespaceLoadSpi> serviceLoader = ServiceLoader.load(NamespaceLoadSpi.class);
		Iterator<NamespaceLoadSpi> iterator = serviceLoader.iterator();
		while (iterator.hasNext()) {
			NamespaceLoadSpi namespaceLoadSpi = iterator.next();
			Set<String> namespaceSet = namespaceLoadSpi.load();
			ApplePropertySourcesProcessor.addNamespaces(namespaceSet, order++);
		}

	    Map<String, Object> propertySourcesPlaceholderPropertyValues = new HashMap<>();
	    // to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer
	    propertySourcesPlaceholderPropertyValues.put("order", 0);

	    BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(), PropertySourcesPlaceholderConfigurer.class, propertySourcesPlaceholderPropertyValues);
	    
// Path: apple-config-core/src/main/java/com/appleframework/config/core/spi/NamespaceLoadSpi.java
// public interface NamespaceLoadSpi {
// 
// 	public Set<String> load();
// 	
// }


// Path: apple-config-apollo/src/main/java/com/appleframework/config/AppleApolloConfigRegistrarHelper.java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.type.AnnotationMetadata;
import com.appleframework.config.core.spi.NamespaceLoadSpi;
import com.ctrip.framework.apollo.core.spi.Ordered;
import com.ctrip.framework.apollo.spring.annotation.ApolloAnnotationProcessor;
import com.ctrip.framework.apollo.spring.annotation.ApolloJsonValueProcessor;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.SpringValueProcessor;
import com.ctrip.framework.apollo.spring.property.SpringValueDefinitionProcessor;
import com.ctrip.framework.apollo.spring.spi.ApolloConfigRegistrarHelper;
import com.ctrip.framework.apollo.spring.util.BeanRegistrationUtil;
import com.google.common.collect.Lists;

package com.appleframework.config;




public class AppleApolloConfigRegistrarHelper implements ApolloConfigRegistrarHelper {

	  @Override
	  public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
	    AnnotationAttributes attributes = AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(EnableApolloConfig.class.getName()));
	    String[] namespaces = attributes.getStringArray("value");
	    int order = attributes.getNumber("order");
	    ApplePropertySourcesProcessor.addNamespaces(Lists.newArrayList(namespaces), order++);
	    
	    ServiceLoader<NamespaceLoadSpi> serviceLoader = ServiceLoader.load(NamespaceLoadSpi.class);
		Iterator<NamespaceLoadSpi> iterator = serviceLoader.iterator();
		while (iterator.hasNext()) {
			NamespaceLoadSpi namespaceLoadSpi = iterator.next();
			Set<String> namespaceSet = namespaceLoadSpi.load();
			ApplePropertySourcesProcessor.addNamespaces(namespaceSet, order++);
		}

	    Map<String, Object> propertySourcesPlaceholderPropertyValues = new HashMap<>();
	    // to make sure the default PropertySourcesPlaceholderConfigurer's priority is higher than PropertyPlaceholderConfigurer
	    propertySourcesPlaceholderPropertyValues.put("order", 0);

	    BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, PropertySourcesPlaceholderConfigurer.class.getName(), PropertySourcesPlaceholderConfigurer.class, propertySourcesPlaceholderPropertyValues);
	    
	    BeanRegistrationUtil.registerBeanDefinitionIfNotExists(registry, AppleLocalResourceProcessor.class.getName(), AppleLocalResourceProcessor.class);