<commit_before>/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you 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.apache.james.protocols.lib;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.configuration.CombinedConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationUtils;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.configuration.HierarchicalConfiguration;

import org.apache.james.protocols.api.handler.ExtensibleHandler;
import org.apache.james.protocols.api.handler.HandlersPackage;
import org.apache.james.protocols.api.handler.LifecycleAwareProtocolHandler;
import org.apache.james.protocols.api.handler.ProtocolHandlerChain;
import org.apache.james.protocols.api.handler.ProtocolHandlerLoader;
import org.apache.james.protocols.api.handler.WiringException;

public class ProtocolHandlerChainImpl implements ProtocolHandlerChain{

    private ProtocolHandlerLoader loader;
    private HierarchicalConfiguration handlerchainConfig;
    private String jmxName;
    private String coreHandlersPackage;
    private String jmxHandlersPackage;
    private List<Object> handlers = new LinkedList<Object>();
    
    public ProtocolHandlerChainImpl(ProtocolHandlerLoader loader, HierarchicalConfiguration handlerchainConfig, String jmxName, Class<? extends HandlersPackage> coreHandlersPackage, Class<? extends HandlersPackage> jmxHandlersPackage) {
        this.loader = loader;
        this.handlerchainConfig = handlerchainConfig;
        this.jmxName = jmxName;
        this.coreHandlersPackage = coreHandlersPackage.getName();
        this.jmxHandlersPackage = jmxHandlersPackage.getName();
    }
    
    @SuppressWarnings("unchecked")
    public void init() throws Exception {
        List<org.apache.commons.configuration.HierarchicalConfiguration> children = handlerchainConfig.configurationsAt("handler");

        // check if the coreHandlersPackage was specified inte hconfig if
        // not add the default
        if (handlerchainConfig.getString("[@coreHandlersPackage]") == null)
            handlerchainConfig.addProperty("[@coreHandlersPackage]", coreHandlersPackage);

        String coreHandlersPackage = handlerchainConfig.getString("[@coreHandlersPackage]");
        
        if (handlerchainConfig.getString("[@jmxHandlersPackage]") == null)
            handlerchainConfig.addProperty("[@jmxHandlersPackage]", jmxHandlersPackage);

        String jmxHandlersPackage = handlerchainConfig.getString("[@jmxHandlersPackage]");
 
        HandlersPackage handlersPackage = (HandlersPackage) loader.load(coreHandlersPackage, addHandler(coreHandlersPackage));
        registerHandlersPackage(handlersPackage, null, children);

        if (handlerchainConfig.getBoolean("[@enableJmx]", true)) {
            DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
            builder.addProperty("jmxName", jmxName);
            HandlersPackage jmxPackage = (HandlersPackage) loader.load(jmxHandlersPackage, addHandler(jmxHandlersPackage));
            
            registerHandlersPackage(jmxPackage, builder, children);
        }

        for (int i = 0; i < children.size(); i++) {
            HierarchicalConfiguration hConf = children.get(i);
            String className = hConf.getString("[@class]", null);
            if (className != null) {
                handlers.add(loader.load(className, hConf));
            } else {
                throw new ConfigurationException("Missing @class attribute in configuration: " + ConfigurationUtils.toString(hConf));
            }
        }
        wireExtensibleHandlers();

    }
    
    private void wireExtensibleHandlers() throws WiringException {
        LinkedList<ExtensibleHandler> eHandlers = getHandlers(ExtensibleHandler.class);
        for (int a = 0; a < eHandlers.size(); a++) {
            final ExtensibleHandler extensibleHandler = eHandlers.get(a);
            final List<Class<?>> markerInterfaces = extensibleHandler.getMarkerInterfaces();
            for (int i = 0; i < markerInterfaces.size(); i++) {
                final Class<?> markerInterface = markerInterfaces.get(i);
                final List<?> extensions = getHandlers(markerInterface);                   
                // ok now time for try the wiring
                extensibleHandler.wireExtensions(markerInterface, extensions);
            }
        }

    }
    
    
    private void registerHandlersPackage(HandlersPackage handlersPackage, HierarchicalConfiguration handlerConfig, List<HierarchicalConfiguration> children) throws ConfigurationException{
        List<String> c = handlersPackage.getHandlers();

        for (Iterator<String> i = c.iterator(); i.hasNext();) {
            String cName = i.next();

            try {
                CombinedConfiguration conf = new CombinedConfiguration();
                HierarchicalConfiguration cmdConf = addHandler(cName);
                conf.addConfiguration(cmdConf);
                if (handlerConfig != null) {
                    conf.addConfiguration(handlerConfig);
                }
                children.add(conf);
            } catch (ConfigurationException e) {
                throw new ConfigurationException("Unable to create configuration for handler " + cName, e);
            }
        }
    }
    /**
     * Return a DefaultConfiguration build on the given command name and
     * classname.
     * 
     * @param cmdName
     *            The command name
     * @param className
     *            The class name
     * @return DefaultConfiguration
     * @throws ConfigurationException
     */
    private HierarchicalConfiguration addHandler(String className) throws ConfigurationException {
        HierarchicalConfiguration hConf = new DefaultConfigurationBuilder();
        hConf.addProperty("[@class]", className);
        return hConf;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> LinkedList<T> getHandlers(Class<T> type) {
        LinkedList<T> hList = new LinkedList<T>();
        
        for (int i = 0; i < handlers.size(); i++) {
            Object h = handlers.get(i);
            if (type.isInstance(h)) {
                hList.add((T) h);

            }
        }
        return hList;
    }

    /**
     * Destroy all loaded {@link LifecycleAwareProtocolHandler}
     */
    public void destroy() {
        LinkedList<LifecycleAwareProtocolHandler> lHandlers = getHandlers(LifecycleAwareProtocolHandler.class);
        for (LifecycleAwareProtocolHandler handler: lHandlers) {
            handler.destroy();
        }
    }
}<commit_msg>Import and Class rename / usage to use the new protocols 1.6.0-RC2-SNAPSHOT (JAMES-1360)<commit_after>/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you 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.apache.james.protocols.lib;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.configuration.CombinedConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationUtils;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.protocols.api.handler.ExtensibleHandler;
import org.apache.james.protocols.api.handler.ProtocolHandlerChain;
import org.apache.james.protocols.api.handler.WiringException;
import org.apache.james.protocols.lib.handler.HandlersPackage;
import org.apache.james.protocols.lib.handler.ProtocolHandlerLoader;
import org.apache.james.protocols.lib.lifecycle.InitializingLifecycleAwareProtocolHandler;

public class ProtocolHandlerChainImpl implements ProtocolHandlerChain{

    private ProtocolHandlerLoader loader;
    private HierarchicalConfiguration handlerchainConfig;
    private String jmxName;
    private String coreHandlersPackage;
    private String jmxHandlersPackage;
    private List<Object> handlers = new LinkedList<Object>();
    
    public ProtocolHandlerChainImpl(ProtocolHandlerLoader loader, HierarchicalConfiguration handlerchainConfig, String jmxName, Class<? extends HandlersPackage> coreHandlersPackage, Class<? extends HandlersPackage> jmxHandlersPackage) {
        this.loader = loader;
        this.handlerchainConfig = handlerchainConfig;
        this.jmxName = jmxName;
        this.coreHandlersPackage = coreHandlersPackage.getName();
        this.jmxHandlersPackage = jmxHandlersPackage.getName();
    }
    
    @SuppressWarnings("unchecked")
    public void init() throws Exception {
        List<org.apache.commons.configuration.HierarchicalConfiguration> children = handlerchainConfig.configurationsAt("handler");

        // check if the coreHandlersPackage was specified inte hconfig if
        // not add the default
        if (handlerchainConfig.getString("[@coreHandlersPackage]") == null)
            handlerchainConfig.addProperty("[@coreHandlersPackage]", coreHandlersPackage);

        String coreHandlersPackage = handlerchainConfig.getString("[@coreHandlersPackage]");
        
        if (handlerchainConfig.getString("[@jmxHandlersPackage]") == null)
            handlerchainConfig.addProperty("[@jmxHandlersPackage]", jmxHandlersPackage);

        String jmxHandlersPackage = handlerchainConfig.getString("[@jmxHandlersPackage]");
 
        HandlersPackage handlersPackage = (HandlersPackage) loader.load(coreHandlersPackage, addHandler(coreHandlersPackage));
        registerHandlersPackage(handlersPackage, null, children);

        if (handlerchainConfig.getBoolean("[@enableJmx]", true)) {
            DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
            builder.addProperty("jmxName", jmxName);
            HandlersPackage jmxPackage = (HandlersPackage) loader.load(jmxHandlersPackage, addHandler(jmxHandlersPackage));
            
            registerHandlersPackage(jmxPackage, builder, children);
        }

        for (int i = 0; i < children.size(); i++) {
            HierarchicalConfiguration hConf = children.get(i);
            String className = hConf.getString("[@class]", null);
            if (className != null) {
                handlers.add(loader.load(className, hConf));
            } else {
                throw new ConfigurationException("Missing @class attribute in configuration: " + ConfigurationUtils.toString(hConf));
            }
        }
        wireExtensibleHandlers();

    }
    
    private void wireExtensibleHandlers() throws WiringException {
        LinkedList<ExtensibleHandler> eHandlers = getHandlers(ExtensibleHandler.class);
        for (int a = 0; a < eHandlers.size(); a++) {
            final ExtensibleHandler extensibleHandler = eHandlers.get(a);
            final List<Class<?>> markerInterfaces = extensibleHandler.getMarkerInterfaces();
            for (int i = 0; i < markerInterfaces.size(); i++) {
                final Class<?> markerInterface = markerInterfaces.get(i);
                final List<?> extensions = getHandlers(markerInterface);                   
                // ok now time for try the wiring
                extensibleHandler.wireExtensions(markerInterface, extensions);
            }
        }

    }
    
    
    private void registerHandlersPackage(HandlersPackage handlersPackage, HierarchicalConfiguration handlerConfig, List<HierarchicalConfiguration> children) throws ConfigurationException{
        List<String> c = handlersPackage.getHandlers();

        for (Iterator<String> i = c.iterator(); i.hasNext();) {
            String cName = i.next();

            try {
                CombinedConfiguration conf = new CombinedConfiguration();
                HierarchicalConfiguration cmdConf = addHandler(cName);
                conf.addConfiguration(cmdConf);
                if (handlerConfig != null) {
                    conf.addConfiguration(handlerConfig);
                }
                children.add(conf);
            } catch (ConfigurationException e) {
                throw new ConfigurationException("Unable to create configuration for handler " + cName, e);
            }
        }
    }
    /**
     * Return a DefaultConfiguration build on the given command name and
     * classname.
     * 
     * @param cmdName
     *            The command name
     * @param className
     *            The class name
     * @return DefaultConfiguration
     * @throws ConfigurationException
     */
    private HierarchicalConfiguration addHandler(String className) throws ConfigurationException {
        HierarchicalConfiguration hConf = new DefaultConfigurationBuilder();
        hConf.addProperty("[@class]", className);
        return hConf;
    }

    @SuppressWarnings("unchecked")
    @Override
    public <T> LinkedList<T> getHandlers(Class<T> type) {
        LinkedList<T> hList = new LinkedList<T>();
        
        for (int i = 0; i < handlers.size(); i++) {
            Object h = handlers.get(i);
            if (type.isInstance(h)) {
                hList.add((T) h);

            }
        }
        return hList;
    }

    /**
     * Destroy all loaded {@link InitializingLifecycleAwareProtocolHandler}
     */
    public void destroy() {
        LinkedList<InitializingLifecycleAwareProtocolHandler> lHandlers = getHandlers(InitializingLifecycleAwareProtocolHandler.class);
        for (InitializingLifecycleAwareProtocolHandler handler: lHandlers) {
            handler.destroy();
        }
    }
}
{'fraction_non_alphanumeric': 0.10164765525982256, 'fraction_numerical': 0.001520912547528517, 'mean_word_length': 2.8136781053649105, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3233415', 'n_tokens_mistral': 3826, 'n_tokens_neox': 3721, 'n_words': 935}