simplified the XMLRouterConfiguration API. It is now much simpler and still atomic.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / XMLRouter.java
index 2f4d45bae3a449e3118c50b8949a885b585a1071..09bbdc1dc50cc78c89e1900b879360c1d32e1dab 100644 (file)
@@ -29,6 +29,7 @@ import java.util.logging.Logger;
 import javax.xml.transform.dom.DOMSource;
 
 import org.wamblee.general.Clock;
+import org.wamblee.general.Pair;
 import org.wamblee.xml.XMLDocument;
 import org.wamblee.xmlrouter.common.Id;
 import org.wamblee.xmlrouter.config.DocumentType;
@@ -74,26 +75,22 @@ public class XMLRouter implements Gateway, DestinationRegistry {
 
     @Override
     public void publish(String aSource, DOMSource aEvent) {
-        config.startPublishEvent();
-        try {
-            publishImpl(aSource, aEvent);
-        } finally {
-            config.endPublishEvent();
-        }
-    }
-
-    private void publishImpl(String aSource, DOMSource aEvent) {
         long time = clock.currentTimeMillis();
 
+        Pair<ExtendedRouterConfig, TransformationPaths> snapshotconfig = config
+            .getConfig();
+
         Id<DOMSource> id = new Id<DOMSource>(nextEventId.getAndIncrement() + "");
-        List<String> types = determineDocumentTypes(aEvent);
+        List<String> types = determineDocumentTypes(snapshotconfig.getFirst()
+            .documentTypeConfig().values(), aEvent);
         EventInfo info = new EventInfo(time, aSource, id, types, aEvent);
 
         boolean delivered = false;
         try {
 
             List<String> filteredInputTypes = determineFilteredInputTypes(
-                types, aEvent);
+                snapshotconfig.getFirst().filterConfig().values(), types,
+                aEvent);
             if (filteredInputTypes.isEmpty()) {
                 if (LOGGER.isLoggable(Level.FINE)) {
                     String doc = new XMLDocument(aEvent).print(true);
@@ -112,7 +109,9 @@ public class XMLRouter implements Gateway, DestinationRegistry {
             // This is however certainly not the main case.
 
             for (String inputType : filteredInputTypes) {
-                boolean result = deliverEvent(info, inputType);
+                boolean result = deliverEvent(snapshotconfig.getFirst()
+                    .filterConfig().values(), snapshotconfig.getSecond(), info,
+                    inputType);
                 delivered = delivered || result;
             }
         } finally {
@@ -123,11 +122,12 @@ public class XMLRouter implements Gateway, DestinationRegistry {
         }
     }
 
-    private boolean deliverEvent(EventInfo aInfo, String aInputType) {
+    private boolean deliverEvent(Collection<Filter> aFilters,
+        TransformationPaths aTransformations, EventInfo aInfo, String aInputType) {
 
         boolean delivered = false;
         Set<String> possibleTargetTypes = new HashSet<String>();
-        possibleTargetTypes.addAll(config.getTransformations()
+        possibleTargetTypes.addAll(aTransformations
             .getPossibleTargetTypes(aInputType));
 
         // ask each destination what target types, if any they want to have.
@@ -140,8 +140,8 @@ public class XMLRouter implements Gateway, DestinationRegistry {
             if (!requested.isEmpty()) {
                 // Deliver to the destination.
                 for (String targetType : requested) {
-                    TransformationPath path = config.getTransformations()
-                        .getPath(aInputType, targetType);
+                    TransformationPath path = aTransformations.getPath(
+                        aInputType, targetType);
                     List<Transformation> ts = path.getTransformations();
                     int i = 0;
                     boolean allowed = true;
@@ -155,7 +155,8 @@ public class XMLRouter implements Gateway, DestinationRegistry {
                                 aInfo.getEvent(), aInputType, t, orig);
                         }
 
-                        if (!isAllowedByFilters(t.getToType(), transformed)) {
+                        if (!isAllowedByFilters(aFilters, t.getToType(),
+                            transformed)) {
                             allowed = false;
                         }
                         i++;
@@ -175,13 +176,13 @@ public class XMLRouter implements Gateway, DestinationRegistry {
         return delivered;
     }
 
-    private List<String> determineFilteredInputTypes(List<String> aTypes,
-        DOMSource aEvent) {
+    private List<String> determineFilteredInputTypes(
+        Collection<Filter> aFilters, List<String> aTypes, DOMSource aEvent) {
 
         // apply filters to the input
         List<String> filteredTypes = new ArrayList<String>();
         for (String type : aTypes) {
-            boolean allowed = isAllowedByFilters(type, aEvent);
+            boolean allowed = isAllowedByFilters(aFilters, type, aEvent);
             if (allowed) {
                 filteredTypes.add(type);
             }
@@ -189,9 +190,10 @@ public class XMLRouter implements Gateway, DestinationRegistry {
         return filteredTypes;
     }
 
-    private boolean isAllowedByFilters(String aType, DOMSource aEvent) {
+    private boolean isAllowedByFilters(Collection<Filter> aFilters,
+        String aType, DOMSource aEvent) {
         boolean allowed = true;
-        for (Filter filter : config.getRouterConfig().filterConfig().values()) {
+        for (Filter filter : aFilters) {
             if (!filter.isAllowed(aType, aEvent)) {
                 allowed = false;
             }
@@ -199,10 +201,10 @@ public class XMLRouter implements Gateway, DestinationRegistry {
         return allowed;
     }
 
-    private List<String> determineDocumentTypes(DOMSource aEvent) {
+    private List<String> determineDocumentTypes(
+        Collection<DocumentType> aTypes, DOMSource aEvent) {
         List<String> res = new ArrayList<String>();
-        for (DocumentType type : config.getRouterConfig().documentTypeConfig()
-            .values()) {
+        for (DocumentType type : aTypes) {
             if (type.isInstance(aEvent)) {
                 res.add(type.getName());
             }