first version of the logging interface with some basic implementations.
authorErik Brakkee <erik@brakkee.org>
Sun, 24 Jul 2011 13:29:46 +0000 (15:29 +0200)
committerErik Brakkee <erik@brakkee.org>
Sun, 24 Jul 2011 13:29:46 +0000 (15:29 +0200)
impl/pom.xml
impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeEventLogger.java [new file with mode: 0644]
impl/src/main/java/org/wamblee/xmlrouter/impl/LoggingEventLogger.java [new file with mode: 0644]
impl/src/main/java/org/wamblee/xmlrouter/impl/XMLRouter.java
logging/src/main/java/org/wamblee/xmlrouter/logging/EventLogger.java [new file with mode: 0644]

index a20b1e4676e039d72c98b411bab9955e7e2452ec..8d5ec97f5961eb4cb2c38bc7db69c10cd893dd1c 100644 (file)
             <artifactId>xmlrouter-subscribe</artifactId>
             <version>0.1-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>org.wamblee.xmlrouter</groupId>
+            <artifactId>xmlrouter-logging</artifactId>
+            <version>0.1-SNAPSHOT</version>
+        </dependency>
         <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeEventLogger.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/CompositeEventLogger.java
new file mode 100644 (file)
index 0000000..424bd80
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed 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.wamblee.xmlrouter.impl;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.print.attribute.standard.Destination;
+import javax.xml.transform.dom.DOMSource;
+
+import org.wamblee.concurrency.ReadLock;
+import org.wamblee.concurrency.WriteLock;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Transformation;
+import org.wamblee.xmlrouter.logging.EventLogger;
+
+public class CompositeEventLogger implements EventLogger {
+
+    private static final Logger LOGGER = Logger
+        .getLogger(CompositeEventLogger.class.getName());
+    private List<EventLogger> loggers;
+
+    public CompositeEventLogger() {
+        loggers = new ArrayList<EventLogger>();
+    }
+
+    /**
+     * Adds a logger.
+     * 
+     * @param aLogger
+     *            Logger
+     */
+    @WriteLock
+    public void addLogger(EventLogger aLogger) {
+        loggers.add(aLogger);
+    }
+
+    /**
+     * Removes a logger.
+     * 
+     * @param aLogger
+     *            Logger
+     * @return True iff the logger was removed.
+     */
+    @WriteLock
+    public boolean removeLogger(EventLogger aLogger) {
+        Iterator<EventLogger> logger = loggers.iterator();
+        while (logger.hasNext()) {
+            EventLogger value = logger.next();
+            if (value == aLogger) {
+                logger.remove();
+                return true;
+            }
+        }
+        return false;
+    }
+
+    @Override
+    @ReadLock
+    public void delivered(String aDocumentType, Id<DOMSource> aEventId,
+        DOMSource aEvent, List<Transformation> aSequence,
+        Id<Destination> aDestination, String aDestinationName,
+        boolean aSuccessFlag) {
+        for (EventLogger logger : loggers) {
+            try {
+                logger.delivered(aDocumentType, aEventId, aEvent, aSequence,
+                    aDestination, aDestinationName, aSuccessFlag);
+            } catch (Exception e) {
+                LOGGER.log(Level.WARNING, "Logger threw exception", e);
+            }
+        }
+    }
+
+    @Override
+    @ReadLock
+    public void notDelivered(String aDocumentType, Id<DOMSource> aEventId,
+        DOMSource aEvent) {
+        for (EventLogger logger : loggers) {
+            try {
+                logger.notDelivered(aDocumentType, aEventId, aEvent);
+            } catch (Exception e) {
+                LOGGER.log(Level.WARNING, "Logger threw exception", e);
+            }
+        }
+    }
+
+}
diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/LoggingEventLogger.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/LoggingEventLogger.java
new file mode 100644 (file)
index 0000000..9f4e809
--- /dev/null
@@ -0,0 +1,82 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed 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.wamblee.xmlrouter.impl;
+
+import java.util.List;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import javax.print.attribute.standard.Destination;
+import javax.xml.transform.dom.DOMSource;
+
+import org.wamblee.xml.XMLDocument;
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Transformation;
+import org.wamblee.xmlrouter.logging.EventLogger;
+
+/**
+ * Event logger that simply logs to java.util logging.
+ * 
+ * @author Erik Brakkee
+ * 
+ */
+public class LoggingEventLogger implements EventLogger {
+    private static final Logger LOGGER = Logger
+        .getLogger(LoggingEventLogger.class.getName());
+
+    private Level level;
+
+    public LoggingEventLogger(Level aLevel) {
+        level = aLevel;
+    }
+
+    @Override
+    public void delivered(String aDocType, Id<DOMSource> aEventId,
+        DOMSource aEvent, List<Transformation> aSequence,
+        Id<Destination> aDestination, String aDestinationName,
+        boolean aSuccessFlag) {
+        if (LOGGER.isLoggable(level)) {
+            LOGGER.log(level, "event delivered: document type '" + aDocType +
+                "', eventId " + aEventId + ", event '" +
+                new XMLDocument(aEvent).print(true) + "', sequence '" +
+                getSequenceString(aSequence) + "', destionationId " +
+                aDestination + ", destinationName '" + aDestinationName + "'");
+        }
+    }
+
+    private String getSequenceString(List<Transformation> aSequence) {
+        StringBuffer buf = new StringBuffer();
+        for (Transformation transformation : aSequence) {
+            buf.append(transformation.getName());
+            buf.append("(");
+            buf.append(transformation.getFromType());
+            buf.append("->");
+            buf.append(transformation.getToType());
+            buf.append(")");
+        }
+        return buf.toString();
+    }
+
+    @Override
+    public void notDelivered(String aDocumentType, Id<DOMSource> aEventId,
+        DOMSource aEvent) {
+        if (LOGGER.isLoggable(level)) {
+            LOGGER.log(level, "event not delivered: document type '" +
+                aDocumentType + "', eventId " + aEventId + ", event '" +
+                new XMLDocument(aEvent).print(true) + "'");
+        }
+    }
+}
index 652a2ff8a037376bf53e1a026a968120850e8416..679c7e38682f0ddf5560f0b875dc3bb397d20309 100644 (file)
@@ -12,7 +12,7 @@
  * 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.wamblee.xmlrouter.impl;
 
 import java.util.ArrayList;
diff --git a/logging/src/main/java/org/wamblee/xmlrouter/logging/EventLogger.java b/logging/src/main/java/org/wamblee/xmlrouter/logging/EventLogger.java
new file mode 100644 (file)
index 0000000..3c6e325
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed 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.wamblee.xmlrouter.logging;
+
+import java.util.List;
+
+import javax.print.attribute.standard.Destination;
+import javax.xml.transform.dom.DOMSource;
+
+import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Transformation;
+
+/**
+ * Event logger which is notified of the activities done by the xml router.
+ * 
+ * @author Erik Brakkee
+ * 
+ */
+public interface EventLogger {
+
+    /**
+     * Called when an event is delivered to a destination.
+     * 
+     * @param aDocumentType
+     *            Document type.
+     * @param aEventId
+     *            Unique id for the original event.
+     * @param aEvent
+     *            The event.
+     * @param aSequence
+     *            Sequence of transformations performed.
+     * @param aDestination
+     *            Id of the destination the event was delivered to.
+     * @param aDestinationName
+     *            Destination name.
+     * @param aSuccessFlag
+     *            Whether or not event delivery succeeded.
+     */
+    void delivered(String aDocumentType, Id<DOMSource> aEventId,
+        DOMSource aEvent, List<Transformation> aSequence,
+        Id<Destination> aDestination, String aDestinationName,
+        boolean aSuccessFlag);
+
+    /**
+     * Called when an event could not be delivered to any destination.
+     * 
+     * @param aEventId
+     *            Unique id for the original event.
+     * @param aEvent
+     *            Event.
+     */
+    void notDelivered(String aDocumentType, Id<DOMSource> aEventId,
+        DOMSource aEvent);
+}