package org.opennms.netmgt.correlation;

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.Assert;

public class CorrelationEngineFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
    
    private List<CorrelationEngine> m_correlationEngines = Collections.EMPTY_LIST;
	private ApplicationContext m_applicationContext;

    public Object getObject() throws Exception {
        return m_correlationEngines;
    }

    public Class getObjectType() {
        return m_correlationEngines.getClass();
    }

    public boolean isSingleton() {
        return true;
    }
    
    public void afterPropertiesSet() throws Exception {
        Assert.state(m_applicationContext != null, "applicationContext must be set");
        
        Map<String, CorrelationEngine> beans = 
            BeanFactoryUtils.beansOfTypeIncludingAncestors(m_applicationContext, CorrelationEngine.class);
        
        m_correlationEngines = new LinkedList<CorrelationEngine>(beans.values());
        
        Map<String, CorrelationEngine[]> listBeans = 
            BeanFactoryUtils.beansOfTypeIncludingAncestors(m_applicationContext, CorrelationEngine[].class);
        
        for (CorrelationEngine[] engines : listBeans.values()) {
            m_correlationEngines.addAll(Arrays.asList(engines));
        }
        
    }
    

	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
	    m_applicationContext = applicationContext;
	}

}

@@ -89,16 +89,42 @@
 ctions;%0A
+import java.util.HashSet;%0A
 import j
@@ -1393,44 +1393,111 @@
     
-m_correlationEngines = new LinkedLis
+// put them in a set to deduplicate the beans%0A        HashSet%3CCorrelationEngine%3E engineSet = new HashSe
 t%3CCo
@@ -1529,16 +1529,17 @@
 lues());
+ 
 %0A       
@@ -1789,36 +1789,25 @@
         
-m_correlationE
+e
 ngine
-s
+Set
 .addAll(
@@ -1846,24 +1846,101 @@
  %7D%0A        %0A
+        m_correlationEngines = new LinkedList%3CCorrelationEngine%3E(engineSet);%0A
     %7D%0A    %0A%0A

make sure we deduplicate bean entries