First version after introduction of meaningful ids and Identifiable interface.
[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 org.wamblee.xmlrouter.config.Transformation;
23
24 /**
25  * Event logger that simply logs to java.util logging.
26  * 
27  * @author Erik Brakkee
28  * 
29  */
30 public class LoggingEventListener implements EventListener {
31     private static final Logger LOGGER = Logger
32         .getLogger(LoggingEventListener.class.getName());
33
34     private Level level;
35
36     public LoggingEventListener(Level aLevel) {
37         level = aLevel;
38     }
39
40     @Override
41     public void delivered(EventInfo aEvent, List<Transformation> aSequence,
42         String aDestinationId, boolean aSuccessFlag) {
43         if (LOGGER.isLoggable(level)) {
44             LOGGER.log(level, "event delivered: " + aEvent + ", sequence '" +
45                 getSequenceString(aSequence) + "', destinationId " +
46                 aDestinationId + "'");
47         }
48     }
49
50     private String getSequenceString(List<Transformation> aSequence) {
51         StringBuffer buf = new StringBuffer();
52         for (int i = 0; i < aSequence.size(); i++) {
53             if (i > 0) {
54                 buf.append(", ");
55             }
56             Transformation transformation = aSequence.get(i);
57             buf.append(transformation.getId());
58             buf.append("(");
59             buf.append(transformation.getFromType());
60             buf.append("->");
61             buf.append(transformation.getToType());
62             buf.append(")");
63         }
64         return buf.toString();
65     }
66
67     @Override
68     public void notDelivered(EventInfo aInfo) {
69         if (LOGGER.isLoggable(level)) {
70             LOGGER.log(level, "event not delivered: " + aInfo);
71         }
72     }
73 }