Now using RouterConfig internally inside the XML Router.
[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 ExtendedRouterConfig routerConfig;
62     private Transformations transformations;
63
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         routerConfig = new SingleRouterConfig(sequenceNumbers);
72         transformations = new Transformations();
73         destinations = new LinkedHashMap<Id<Destination>, Destination>();
74     }
75
76     @Override
77     public Config<DocumentType> documentTypeConfig() {
78         return routerConfig.documentTypeConfig();
79     }
80
81     @Override
82     public Config<Transformation> transformationConfig() {
83         return routerConfig.transformationConfig();
84     }
85
86     @Override
87     public Config<Filter> filterConfig() {
88         return routerConfig.filterConfig();
89     }
90
91     @Override
92     public void publish(String aSource, DOMSource aEvent) {
93         long time = clock.currentTimeMillis();
94
95         if (routerConfig.isDirty()) {
96             transformations.replaceTransformations(routerConfig
97                 .transformationConfig().map());
98             routerConfig.resetDirty();
99         }
100
101         Id<DOMSource> id = new Id<DOMSource>(nextEventId.getAndIncrement());
102         List<String> types = determineDocumentTypes(aEvent);
103         EventInfo info = new EventInfo(time, aSource, id, types, aEvent);
104
105         boolean delivered = false;
106         try {
107
108             List<String> filteredInputTypes = determineFilteredInputTypes(
109                 types, aEvent);
110             if (filteredInputTypes.isEmpty()) {
111                 if (LOGGER.isLoggable(Level.FINE)) {
112                     String doc = new XMLDocument(aEvent).print(true);
113                     LOGGER
114                         .log(
115                             Level.FINE,
116                             "Event ''0}'' from source {1} removed because of filters.",
117                             new Object[] { doc, aSource });
118                 }
119             }
120
121             // get the reachable target types through transformations.
122
123             // It is possible that a given event belongs to multiple input
124             // types.
125             // This is however certainly not the main case.
126
127             for (String inputType : filteredInputTypes) {
128                 boolean result = deliverEvent(info, inputType);
129                 delivered = delivered || result;
130             }
131         } finally {
132             if (!delivered) {
133                 destinationNotFound(aSource, aEvent);
134                 listener.notDelivered(info);
135             }
136         }
137     }
138
139     private boolean deliverEvent(EventInfo aInfo, String aInputType) {
140
141         boolean delivered = false;
142         Set<String> possibleTargetTypes = new HashSet<String>();
143         possibleTargetTypes.addAll(transformations
144             .getPossibleTargetTypes(aInputType));
145
146         // ask each destination what target types, if any they want to have.
147         for (Map.Entry<Id<Destination>, Destination> entry : destinations
148             .entrySet()) {
149             Id<Destination> destinationId = entry.getKey();
150             Destination destination = entry.getValue();
151             Collection<String> requested = destination
152                 .chooseFromTargetTypes(possibleTargetTypes);
153             if (!requested.isEmpty()) {
154                 // Deliver to the destination.
155                 for (String targetType : requested) {
156                     TransformationPath path = transformations.getPath(
157                         aInputType, targetType);
158                     List<Transformation> ts = path.getTransformations();
159                     int i = 0;
160                     boolean allowed = true;
161                     DOMSource transformed = aInfo.getEvent();
162                     while (i < ts.size() && allowed && transformed != null) {
163                         Transformation t = ts.get(i);
164                         DOMSource orig = transformed;
165                         transformed = t.transform(transformed);
166                         if (transformed == null) {
167                             transformationReturnedNull(aInfo.getSource(),
168                                 aInfo.getEvent(), aInputType, t, orig);
169                         }
170
171                         if (!isAllowedByFilters(t.getToType(), transformed)) {
172                             allowed = false;
173                         }
174                         i++;
175                     }
176                     if (allowed && transformed != null) {
177                         // all transformations done and all filters still
178                         // allow the event.
179                         boolean result = destination.receive(transformed);
180                         listener.delivered(aInfo, ts, destinationId.getId(),
181                             destination.getName(), result);
182                         delivered = delivered || result;
183
184                     }
185                 }
186             }
187         }
188         return delivered;
189     }
190
191     private List<String> determineFilteredInputTypes(List<String> aTypes,
192         DOMSource aEvent) {
193
194         // apply filters to the input
195         List<String> filteredTypes = new ArrayList<String>();
196         for (String type : aTypes) {
197             boolean allowed = isAllowedByFilters(type, aEvent);
198             if (allowed) {
199                 filteredTypes.add(type);
200             }
201         }
202         return filteredTypes;
203     }
204
205     private boolean isAllowedByFilters(String aType, DOMSource aEvent) {
206         boolean allowed = true;
207         for (Filter filter : routerConfig.filterConfig().map().values()) {
208             if (!filter.isAllowed(aType, aEvent)) {
209                 allowed = false;
210             }
211         }
212         return allowed;
213     }
214
215     private List<String> determineDocumentTypes(DOMSource aEvent) {
216         List<String> res = new ArrayList<String>();
217         for (DocumentType type : routerConfig.documentTypeConfig().map()
218             .values()) {
219             if (type.isInstance(aEvent)) {
220                 res.add(type.getName());
221             }
222         }
223         return res;
224     }
225
226     private void logEvent(String aMessage, String aSource, DOMSource aEvent) {
227         LOGGER.log(Level.WARNING,
228             aMessage + ": " + eventToString(aSource, aEvent));
229     }
230
231     private String eventToString(String aSource, DOMSource aEvent) {
232         return "source '" + aSource + "': Event: '" +
233             new XMLDocument(aEvent).print(true) + "'";
234     }
235
236     private void transformationReturnedNull(String aSource, DOMSource aEvent,
237         String aInputType, Transformation aT, DOMSource aTransformed) {
238         LOGGER.log(Level.WARNING, "Transformation returned null for event " +
239             eventToString(aSource, aEvent) + " inputType '" + aInputType +
240             "', transformation '" + aT + "' document to transform " +
241             new XMLDocument(aTransformed).print(true));
242     }
243
244     private void destinationNotFound(String aSource, DOMSource aEvent) {
245         LOGGER.log(Level.WARNING, "No destination found for event: " +
246             eventToString(aSource, aEvent));
247     }
248
249     @Override
250     public Id<Destination> registerDestination(Destination aDestination) {
251         notNull("destination", aDestination);
252         long seqno = sequenceNumbers.getAndIncrement();
253         Id<Destination> id = new Id<Destination>(seqno);
254         destinations.put(id, new RobustDestination(id, aDestination));
255         return id;
256     }
257
258     @Override
259     public void unregisterDestination(Id<Destination> aId) {
260         destinations.remove(aId);
261     }
262
263     private void notNull(String aName, Object aValue) {
264         if (aValue == null) {
265             throw new IllegalArgumentException("Parameter '" + aName +
266                 "' may not be null");
267         }
268     }
269 }