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