import org.wamblee.xmlrouter.config.DocumentType;
import org.wamblee.xmlrouter.config.Filter;
import org.wamblee.xmlrouter.config.Transformation;
+import org.wamblee.xmlrouter.listener.EventListener;
import org.wamblee.xmlrouter.publish.Gateway;
import org.wamblee.xmlrouter.subscribe.Destination;
import org.wamblee.xmlrouter.subscribe.DestinationRegistry;
private static final Logger LOGGER = Logger.getLogger(XMLRouter.class
.getName());
+ private EventListener listener;
private AtomicInteger sequenceNumbers;
private Map<Integer, DocumentType> documentTypes;
private Transformations transformations;
private Map<Integer, Filter> filters;
private Map<Integer, Destination> destinations;
- public XMLRouter() {
+ public XMLRouter(EventListener aListener) {
+ listener = aListener;
sequenceNumbers = new AtomicInteger(1);
documentTypes = new LinkedHashMap<Integer, DocumentType>();
transformations = new Transformations();
* 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 static junit.framework.Assert.*;
import java.util.Arrays;
import java.util.Collection;
+import java.util.logging.Level;
import javax.xml.transform.dom.DOMSource;
import org.wamblee.xmlrouter.common.Id;
import org.wamblee.xmlrouter.config.DocumentType;
import org.wamblee.xmlrouter.config.Transformation;
+import org.wamblee.xmlrouter.listener.EventListener;
+import org.wamblee.xmlrouter.listener.LoggingEventListener;
import org.wamblee.xmlrouter.subscribe.Destination;
public class XMLRouterTest {
private Destination destinationSpy;
private Id<Destination> destinationId;
+ private EventListener listener;
@Before
public void setUp() {
- router = new XMLRouter();
+ EventListener logListener = new LoggingEventListener(Level.INFO);
+ listener = spy(logListener);
+ router = new XMLRouter(listener);
source1 = mock(DOMSource.class);
source2 = mock(DOMSource.class);
source3 = mock(DOMSource.class);
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;
@Override
@ReadLock
- public void delivered(String aDocumentType, Id<DOMSource> aEventId,
- DOMSource aEvent, List<Transformation> aSequence,
+ public void delivered(EventInfo aInfo, List<Transformation> aSequence,
Id<Destination> aDestination, String aDestinationName,
boolean aSuccessFlag) {
for (EventListener logger : loggers) {
try {
- logger.delivered(aDocumentType, aEventId, aEvent, aSequence,
- aDestination, aDestinationName, aSuccessFlag);
+ logger.delivered(aInfo, 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) {
+ public void notDelivered(EventInfo aInfo) {
for (EventListener logger : loggers) {
try {
- logger.notDelivered(aDocumentType, aEventId, aEvent);
+ logger.notDelivered(aInfo);
} catch (Exception e) {
LOGGER.log(Level.WARNING, "Logger threw exception", e);
}
--- /dev/null
+/*
+ * 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.listener;
+
+import javax.xml.transform.dom.DOMSource;
+
+import org.wamblee.xml.XMLDocument;
+import org.wamblee.xmlrouter.common.Id;
+
+/**
+ * Combines information about the event.
+ *
+ * @author Erik Brakkee
+ *
+ */
+public class EventInfo {
+
+ private long time;
+ private String source;
+ private Id<DOMSource> id;
+ private String type;
+ private DOMSource event;
+
+ public EventInfo(long aTime, String aSource, Id<DOMSource> aId,
+ String aType, DOMSource aEvent) {
+ time = aTime;
+ source = aSource;
+ id = aId;
+ type = aType;
+ event = aEvent;
+ }
+
+ public long getTime() {
+ return time;
+ }
+
+ public String getSource() {
+ return source;
+ }
+
+ public Id<DOMSource> getId() {
+ return id;
+ }
+
+ public String getType() {
+ return type;
+ }
+
+ public void setType(String aType) {
+ type = aType;
+ }
+
+ public DOMSource getEvent() {
+ return event;
+ }
+
+ @Override
+ public String toString() {
+ StringBuffer buf = new StringBuffer();
+ buf.append("Event(");
+ buf.append("time " + time);
+ buf.append(", source " + source);
+ buf.append(", id " + id);
+ buf.append(", type " + type);
+ buf.append(", event " + new XMLDocument(event).print(true));
+ buf.append(")");
+ return buf.toString();
+ }
+}
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;
/**
* 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 aInfo
+ * Event information.
* @param aSequence
* Sequence of transformations performed.
* @param aDestination
* @param aSuccessFlag
* Whether or not event delivery succeeded.
*/
- void delivered(String aDocumentType, Id<DOMSource> aEventId,
- DOMSource aEvent, List<Transformation> aSequence,
+ void delivered(EventInfo aInfo, 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.
+ * @param aInfo
+ * Event info.
*/
- void notDelivered(String aDocumentType, Id<DOMSource> aEventId,
- DOMSource aEvent);
+ void notDelivered(EventInfo aInfo);
}
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;
}
@Override
- public void delivered(String aDocType, Id<DOMSource> aEventId,
- DOMSource aEvent, List<Transformation> aSequence,
+ public void delivered(EventInfo 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 '" +
+ LOGGER.log(level, "event delivered: " + aEvent + ", sequence '" +
getSequenceString(aSequence) + "', destionationId " +
aDestination + ", destinationName '" + aDestinationName + "'");
}
}
@Override
- public void notDelivered(String aDocumentType, Id<DOMSource> aEventId,
- DOMSource aEvent) {
+ public void notDelivered(EventInfo aInfo) {
if (LOGGER.isLoggable(level)) {
- LOGGER.log(level, "event not delivered: document type '" +
- aDocumentType + "', eventId " + aEventId + ", event '" +
- new XMLDocument(aEvent).print(true) + "'");
+ LOGGER.log(level, "event not delivered: " + aInfo);
}
}
}
import java.util.ArrayList;
import javax.print.attribute.standard.Destination;
-import javax.xml.transform.dom.DOMSource;
import org.junit.Before;
import org.junit.Test;
private static final String DESTINATION_NAME = "dest";
private static final Id<Destination> DESTINATION_ID = new Id<Destination>(
12);
- private static final Id<DOMSource> EVENT_ID = new Id<DOMSource>(1);
- private static final String DOCTYPE = "doc";
private CompositeEventListener composite;
- private DOMSource source;
+ private EventInfo source;
@Before
public void setUp() {
composite = new CompositeEventListener();
- source = mock(DOMSource.class);
+ source = mock(EventInfo.class);
}
@Test
public void testNoListeners() {
// verify no exceptions occur.
- composite.delivered(DOCTYPE, EVENT_ID, mock(DOMSource.class),
- getTransformations(), DESTINATION_ID, DESTINATION_NAME, true);
- composite.notDelivered(DOCTYPE, EVENT_ID, mock(DOMSource.class));
+ composite.delivered(mock(EventInfo.class), getTransformations(),
+ DESTINATION_ID, DESTINATION_NAME, true);
+ composite.notDelivered(mock(EventInfo.class));
}
@Test
invokeNotDelivered();
for (EventListener listener : listeners) {
- verify(listener).notDelivered(eq(DOCTYPE), eq(EVENT_ID),
- same(source));
+ verify(listener).notDelivered(same(source));
verifyNoMoreInteractions(listener);
reset(listener);
}
}
private void invokeDelivered() {
- composite.delivered(DOCTYPE, EVENT_ID, source, getTransformations(),
- DESTINATION_ID, DESTINATION_NAME, true);
+ composite.delivered(source, getTransformations(), DESTINATION_ID,
+ DESTINATION_NAME, true);
}
private void invokeNotDelivered() {
- composite.notDelivered(DOCTYPE, EVENT_ID, source);
+ composite.notDelivered(source);
}
private void checkInvokeDelivered(EventListener listener) {
- verify(listener).delivered(eq(DOCTYPE), eq(EVENT_ID), same(source),
- eq(getTransformations()), eq(DESTINATION_ID), eq(DESTINATION_NAME),
- eq(true));
+ verify(listener).delivered(same(source), eq(getTransformations()),
+ eq(DESTINATION_ID), eq(DESTINATION_NAME), eq(true));
}
@Test