package org.openas2.processor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.openas2.OpenAS2Exception;
import org.openas2.message.Message;


public class DefaultProcessor extends BaseProcessor {
    private List<ProcessorModule> modules;

    public List<ProcessorModule> getActiveModules() {
        List<ProcessorModule> activeMods = new ArrayList<ProcessorModule>();
        Iterator<ProcessorModule> moduleIt = getModules().iterator();
        ProcessorModule procMod;

        while (moduleIt.hasNext()) {
            procMod = (ProcessorModule) moduleIt.next();

            if (procMod instanceof ActiveModule) {
                activeMods.add(procMod);
            }
        }

        return activeMods;
    }

    public void setModules(List<ProcessorModule> modules) {
        this.modules = modules;
    }

    public List<ProcessorModule> getModules() {
        if (modules == null) {
            modules = new ArrayList<ProcessorModule>();
        }

        return modules;
    }

    public void handle(String action, Message msg, Map<Object, Object> options)
        throws OpenAS2Exception {
        Iterator<ProcessorModule> moduleIt = getModules().iterator();
        ProcessorModule module;
        ProcessorException pex = null;
        boolean moduleFound = false;

        while (moduleIt.hasNext()) {
            module = (ProcessorModule) moduleIt.next();

            if (module.canHandle(action, msg, options)) {
                try {
                    moduleFound = true;
                    module.handle(action, msg, options);
                } catch (OpenAS2Exception oae) {
                    if (pex == null) {
                        pex = new ProcessorException(this);
                        pex.getCauses().add(oae);
                    }
                }
            }
        }

        if (pex != null) {
            throw pex;
        } else if (!moduleFound) {
            throw new NoModuleException(action, msg, options);
        }
    }

    public void startActiveModules() {
        Iterator<ProcessorModule> activeIt = getActiveModules().iterator();

        while (activeIt.hasNext()) {
            try {
                ((ActiveModule) activeIt.next()).start();
            } catch (OpenAS2Exception e) {
                e.terminate();
            }
        }
    }

    public void stopActiveModules() {
        Iterator<ProcessorModule> activeIt = getActiveModules().iterator();

        while (activeIt.hasNext()) {
            try {
                ((ActiveModule) activeIt.next()).stop();
            } catch (OpenAS2Exception e) {
                e.terminate();
            }
        }
    }
}

@@ -129,24 +129,111 @@
 til.Map;%0D%0A%0D%0A
+import org.apache.commons.logging.Log;%0D%0Aimport org.apache.commons.logging.LogFactory;%0D%0A
 import org.o
@@ -258,16 +258,16 @@
 ption;%0D%0A
-
 import o
@@ -297,18 +297,16 @@
 age;%0D%0A%0D%0A
-%0D%0A
 public c
@@ -395,16 +395,98 @@
 odules;%0D
+%0A%09private Log logger = LogFactory.getLog(DefaultProcessor.class.getSimpleName());%0D
 %0A%0D%0A    p
@@ -1561,24 +1561,152 @@
 = false;%0D%0A%0D%0A
+%09%09if (logger.isDebugEnabled())%0D%0A%09%09%7B%0D%0A%09%09%09logger.debug(%22Processor searching for module handler for action: %22 + action);%0D%0A%09%09%7D%0D%0A%09%09%0D%0A
         whil
@@ -2295,16 +2295,16 @@
 w pex;%0D%0A
-
         
@@ -2327,24 +2327,93 @@
 leFound) %7B%0D%0A
+            logger.error(%22No handler found for action: %22 + action);%0D%0A
             

Improve logging for debugging