unit tests for composite event listener.
[xmlrouter] / listener / src / main / java / org / wamblee / xmlrouter / listener / LoggingEventListener.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xmlrouter.listener;
17
18 import java.util.List;
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
21
22 import javax.print.attribute.standard.Destination;
23 import javax.xml.transform.dom.DOMSource;
24
25 import org.wamblee.xml.XMLDocument;
26 import org.wamblee.xmlrouter.common.Id;
27 import org.wamblee.xmlrouter.config.Transformation;
28
29 /**
30  * Event logger that simply logs to java.util logging.
31  * 
32  * @author Erik Brakkee
33  * 
34  */
35 public class LoggingEventListener implements EventListener {
36     private static final Logger LOGGER = Logger
37         .getLogger(LoggingEventListener.class.getName());
38
39     private Level level;
40
41     public LoggingEventListener(Level aLevel) {
42         level = aLevel;
43     }
44
45     @Override
46     public void delivered(String aDocType, Id<DOMSource> aEventId,
47         DOMSource aEvent, List<Transformation> aSequence,
48         Id<Destination> aDestination, String aDestinationName,
49         boolean aSuccessFlag) {
50         if (LOGGER.isLoggable(level)) {
51             LOGGER.log(level, "event delivered: document type '" + aDocType +
52                 "', eventId " + aEventId + ", event '" +
53                 new XMLDocument(aEvent).print(true) + "', sequence '" +
54                 getSequenceString(aSequence) + "', destionationId " +
55                 aDestination + ", destinationName '" + aDestinationName + "'");
56         }
57     }
58
59     private String getSequenceString(List<Transformation> aSequence) {
60         StringBuffer buf = new StringBuffer();
61         for (Transformation transformation : aSequence) {
62             buf.append(transformation.getName());
63             buf.append("(");
64             buf.append(transformation.getFromType());
65             buf.append("->");
66             buf.append(transformation.getToType());
67             buf.append(")");
68         }
69         return buf.toString();
70     }
71
72     @Override
73     public void notDelivered(String aDocumentType, Id<DOMSource> aEventId,
74         DOMSource aEvent) {
75         if (LOGGER.isLoggable(level)) {
76             LOGGER.log(level, "event not delivered: document type '" +
77                 aDocumentType + "', eventId " + aEventId + ", event '" +
78                 new XMLDocument(aEvent).print(true) + "'");
79         }
80     }
81 }