first version of the logging interface with some basic implementations.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / CompositeEventLogger.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.impl;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.logging.Level;
22 import java.util.logging.Logger;
23
24 import javax.print.attribute.standard.Destination;
25 import javax.xml.transform.dom.DOMSource;
26
27 import org.wamblee.concurrency.ReadLock;
28 import org.wamblee.concurrency.WriteLock;
29 import org.wamblee.xmlrouter.common.Id;
30 import org.wamblee.xmlrouter.config.Transformation;
31 import org.wamblee.xmlrouter.logging.EventLogger;
32
33 public class CompositeEventLogger implements EventLogger {
34
35     private static final Logger LOGGER = Logger
36         .getLogger(CompositeEventLogger.class.getName());
37     private List<EventLogger> loggers;
38
39     public CompositeEventLogger() {
40         loggers = new ArrayList<EventLogger>();
41     }
42
43     /**
44      * Adds a logger.
45      * 
46      * @param aLogger
47      *            Logger
48      */
49     @WriteLock
50     public void addLogger(EventLogger aLogger) {
51         loggers.add(aLogger);
52     }
53
54     /**
55      * Removes a logger.
56      * 
57      * @param aLogger
58      *            Logger
59      * @return True iff the logger was removed.
60      */
61     @WriteLock
62     public boolean removeLogger(EventLogger aLogger) {
63         Iterator<EventLogger> logger = loggers.iterator();
64         while (logger.hasNext()) {
65             EventLogger value = logger.next();
66             if (value == aLogger) {
67                 logger.remove();
68                 return true;
69             }
70         }
71         return false;
72     }
73
74     @Override
75     @ReadLock
76     public void delivered(String aDocumentType, Id<DOMSource> aEventId,
77         DOMSource aEvent, List<Transformation> aSequence,
78         Id<Destination> aDestination, String aDestinationName,
79         boolean aSuccessFlag) {
80         for (EventLogger logger : loggers) {
81             try {
82                 logger.delivered(aDocumentType, aEventId, aEvent, aSequence,
83                     aDestination, aDestinationName, aSuccessFlag);
84             } catch (Exception e) {
85                 LOGGER.log(Level.WARNING, "Logger threw exception", e);
86             }
87         }
88     }
89
90     @Override
91     @ReadLock
92     public void notDelivered(String aDocumentType, Id<DOMSource> aEventId,
93         DOMSource aEvent) {
94         for (EventLogger logger : loggers) {
95             try {
96                 logger.notDelivered(aDocumentType, aEventId, aEvent);
97             } catch (Exception e) {
98                 LOGGER.log(Level.WARNING, "Logger threw exception", e);
99             }
100         }
101     }
102
103 }