refactoring of the config interface towards more reuse in the implementation and...
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / XMLRouter.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.Collection;
20 import java.util.HashSet;
21 import java.util.LinkedHashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.concurrent.atomic.AtomicLong;
26 import java.util.logging.Level;
27 import java.util.logging.Logger;
28
29 import javax.xml.transform.dom.DOMSource;
30
31 import org.wamblee.general.Clock;
32 import org.wamblee.xml.XMLDocument;
33 import org.wamblee.xmlrouter.common.Id;
34 import org.wamblee.xmlrouter.config.Config;
35 import org.wamblee.xmlrouter.config.DocumentType;
36 import org.wamblee.xmlrouter.config.Filter;
37 import org.wamblee.xmlrouter.config.RouterConfig;
38 import org.wamblee.xmlrouter.config.Transformation;
39 import org.wamblee.xmlrouter.listener.EventInfo;
40 import org.wamblee.xmlrouter.listener.EventListener;
41 import org.wamblee.xmlrouter.publish.Gateway;
42 import org.wamblee.xmlrouter.subscribe.Destination;
43 import org.wamblee.xmlrouter.subscribe.DestinationRegistry;
44
45 /**
46  * The XML Router.
47  * 
48  * @author Erik Brakkee
49  * 
50  */
51 public class XMLRouter implements RouterConfig, Gateway, DestinationRegistry {
52
53     private static final Logger LOGGER = Logger.getLogger(XMLRouter.class
54         .getName());
55
56     private AtomicLong sequenceNumbers;
57     private EventListener listener;
58     private Clock clock;
59     private AtomicLong nextEventId;
60
61     private Config<DocumentType> documentTypes;
62     private Transformations transformations;
63     private Config<Filter> filters;
64     private Map<Id<Destination>, Destination> destinations;
65
66     public XMLRouter(Clock aClock, EventListener aListener) {
67         sequenceNumbers = new AtomicLong(1);
68         listener = aListener;
69         clock = aClock;
70         nextEventId = new AtomicLong(clock.currentTimeMillis());
71         documentTypes = new ConfigImpl<DocumentType>() {
72             @Override
73             public DocumentType wrap(Id<DocumentType> aId, DocumentType aType) {
74                 return new RobustDocumentType(aId, aType);
75             }
76         };
77         transformations = new Transformations();
78         filters = new ConfigImpl<Filter>() {
79             @Override
80             public Filter wrap(Id<Filter> aId, Filter aFilter) {
81                 return new RobustFilter(aId, aFilter);
82             }
83         };
84         destinations = new LinkedHashMap<Id<Destination>, Destination>();
85     }
86
87     @Override
88     public Config<DocumentType> getDocumentTypeConfig() {
89         return documentTypes;
90     }
91
92     @Override
93     public Config<Transformation> getTransformationConfig() {
94         return transformations.getTransformationConfig();
95     }
96
97     @Override
98     public Config<Filter> getFilterConfig() {
99         return filters;
100     }
101
102     @Override
103     public void publish(String aSource, DOMSource aEvent) {
104
105         long time = clock.currentTimeMillis();
106         Id<DOMSource> id = new Id<DOMSource>(nextEventId.getAndIncrement());
107         List<String> types = determineDocumentTypes(aEvent);
108         EventInfo info = new EventInfo(time, aSource, id, types, aEvent);
109
110         boolean delivered = false;
111         try {
112
113             List<String> filteredInputTypes = determineFilteredInputTypes(
114                 types, aEvent);
115             if (filteredInputTypes.isEmpty()) {
116                 if (LOGGER.isLoggable(Level.FINE)) {
117                     String doc = new XMLDocument(aEvent).print(true);
118                     LOGGER
119                         .log(
120                             Level.FINE,
121                             "Event ''0}'' from source {1} removed because of filters.",
122                             new Object[] { doc, aSource });
123                 }
124             }
125
126             // get the reachable target types through transformations.
127
128             // It is possible that a given event belongs to multiple input
129             // types.
130             // This is however certainly not the main case.
131
132             for (String inputType : filteredInputTypes) {
133                 boolean result = deliverEvent(info, inputType);
134                 delivered = delivered || result;
135             }
136         } finally {
137             if (!delivered) {
138                 destinationNotFound(aSource, aEvent);
139                 listener.notDelivered(info);
140             }
141         }
142     }
143
144     private boolean deliverEvent(EventInfo aInfo, String aInputType) {
145
146         boolean delivered = false;
147         Set<String> possibleTargetTypes = new HashSet<String>();
148         possibleTargetTypes.addAll(transformations
149             .getPossibleTargetTypes(aInputType));
150
151         // ask each destination what target types, if any they want to have.
152         for (Map.Entry<Id<Destination>, Destination> entry : destinations
153             .entrySet()) {
154             Id<Destination> destinationId = entry.getKey();
155             Destination destination = entry.getValue();
156             Collection<String> requested = destination
157                 .chooseFromTargetTypes(possibleTargetTypes);
158             if (!requested.isEmpty()) {
159                 // Deliver to the destination.
160                 for (String targetType : requested) {
161                     TransformationPath path = transformations.getPath(
162                         aInputType, targetType);
163                     List<Transformation> ts = path.getTransformations();
164                     int i = 0;
165                     boolean allowed = true;
166                     DOMSource transformed = aInfo.getEvent();
167                     while (i < ts.size() && allowed && transformed != null) {
168                         Transformation t = ts.get(i);
169                         DOMSource orig = transformed;
170                         transformed = t.transform(transformed);
171                         if (transformed == null) {
172                             transformationReturnedNull(aInfo.getSource(),
173                                 aInfo.getEvent(), aInputType, t, orig);
174                         }
175
176                         if (!isAllowedByFilters(t.getToType(), transformed)) {
177                             allowed = false;
178                         }
179                         i++;
180                     }
181                     if (allowed && transformed != null) {
182                         // all transformations done and all filters still
183                         // allow the event.
184                         boolean result = destination.receive(transformed);
185                         listener.delivered(aInfo, ts, destinationId.getId(),
186                             destination.getName(), result);
187                         delivered = delivered || result;
188
189                     }
190                 }
191             }
192         }
193         return delivered;
194     }
195
196     private List<String> determineFilteredInputTypes(List<String> aTypes,
197         DOMSource aEvent) {
198
199         // apply filters to the input
200         List<String> filteredTypes = new ArrayList<String>();
201         for (String type : aTypes) {
202             boolean allowed = isAllowedByFilters(type, aEvent);
203             if (allowed) {
204                 filteredTypes.add(type);
205             }
206         }
207         return filteredTypes;
208     }
209
210     private boolean isAllowedByFilters(String aType, DOMSource aEvent) {
211         boolean allowed = true;
212         for (Id<Filter> id : filters.ids()) {
213             Filter filter = filters.get(id);
214             if (!filter.isAllowed(aType, aEvent)) {
215                 allowed = false;
216             }
217         }
218         return allowed;
219     }
220
221     private List<String> determineDocumentTypes(DOMSource aEvent) {
222         List<String> res = new ArrayList<String>();
223         for (Id<DocumentType> id : documentTypes.ids()) {
224             DocumentType type = documentTypes.get(id);
225             if (type.isInstance(aEvent)) {
226                 res.add(type.getName());
227             }
228         }
229         return res;
230     }
231
232     private void logEvent(String aMessage, String aSource, DOMSource aEvent,
233         Exception aException) {
234         LOGGER.log(Level.WARNING, aMessage + ": source '" + aSource +
235             "': Event: '" + new XMLDocument(aEvent).print(true) + "'",
236             aException);
237     }
238
239     private void logEvent(String aMessage, String aSource, DOMSource aEvent) {
240         LOGGER.log(Level.WARNING,
241             aMessage + ": " + eventToString(aSource, aEvent));
242     }
243
244     private String eventToString(String aSource, DOMSource aEvent) {
245         return "source '" + aSource + "': Event: '" +
246             new XMLDocument(aEvent).print(true) + "'";
247     }
248
249     private void transformationReturnedNull(String aSource, DOMSource aEvent,
250         String aInputType, Transformation aT, DOMSource aTransformed) {
251         LOGGER.log(Level.WARNING, "Transformation returned null for event " +
252             eventToString(aSource, aEvent) + " inputType '" + aInputType +
253             "', transformation '" + aT + "' document to transform " +
254             new XMLDocument(aTransformed).print(true));
255     }
256
257     private void destinationNotFound(String aSource, DOMSource aEvent) {
258         LOGGER.log(Level.WARNING, "No destination found for event: " +
259             eventToString(aSource, aEvent));
260     }
261
262     @Override
263     public Id<Destination> registerDestination(Destination aDestination) {
264         notNull("destination", aDestination);
265         long seqno = sequenceNumbers.getAndIncrement();
266         Id<Destination> id = new Id<Destination>(seqno);
267         destinations.put(id, new RobustDestination(id, aDestination));
268         return id;
269     }
270
271     @Override
272     public void unregisterDestination(Id<Destination> aId) {
273         destinations.remove(aId);
274     }
275
276     private void notNull(String aName, Object aValue) {
277         if (aValue == null) {
278             throw new IllegalArgumentException("Parameter '" + aName +
279                 "' may not be null");
280         }
281     }
282 }