2 * Copyright 2005-2011 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.xmlrouter.impl;
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;
26 import java.util.concurrent.atomic.AtomicInteger;
27 import java.util.logging.Level;
28 import java.util.logging.Logger;
30 import javax.xml.transform.dom.DOMSource;
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;
44 public class XMLRouter implements Config, Gateway, DestinationRegistry {
46 private static final Logger LOGGER = Logger.getLogger(XMLRouter.class
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;
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>();
64 public Id<DocumentType> addDocumentType(DocumentType aType) {
65 int seqno = sequenceNumbers.getAndIncrement();
66 documentTypes.put(seqno, aType);
67 return new Id<DocumentType>(seqno);
71 public void removeDocumentType(Id<DocumentType> aId) {
72 documentTypes.remove(aId);
76 public Collection<DocumentType> getDocumentTypes() {
77 return Collections.unmodifiableCollection(documentTypes.values());
81 public Id<Transformation> addTransformation(Transformation aTransformation) {
82 return transformations.addTransformation(aTransformation);
86 public void removeTransformation(Id<Transformation> aId) {
87 transformations.removeTransformation(aId);
91 public Collection<Transformation> getTransformations() {
92 return transformations.getTransformations();
96 public Id<Filter> addFilter(Filter aFilter) {
97 int seqno = sequenceNumbers.getAndIncrement();
98 filters.put(seqno, aFilter);
99 return new Id<Filter>(seqno);
103 public void removeFilter(Id<Filter> aId) {
108 public Collection<Filter> getFilters() {
109 return Collections.unmodifiableCollection(filters.values());
113 public boolean publish(String aSource, DOMSource aEvent) {
115 boolean delivered = false;
118 List<String> filteredInputTypes = determineFilteredInputTypes(aEvent);
119 if (filteredInputTypes.isEmpty()) {
120 if (LOGGER.isLoggable(Level.FINE)) {
121 String doc = new XMLDocument(aEvent).print(true);
125 "Event ''0}'' from source {1} removed because of filters.",
126 new Object[] { doc, aSource });
131 // get the reachable target types through transformations.
133 // It is possible that a given event belongs to multiple input
135 // This is however certainly not the main case.
137 for (String inputType : filteredInputTypes) {
138 boolean result = deliverEvent(aSource, aEvent, inputType);
139 delivered = delivered || result;
143 destinationNotFound(aSource, aEvent);
149 private boolean deliverEvent(String aSource, DOMSource aEvent,
152 boolean delivered = false;
153 Set<String> possibleTargetTypes = new HashSet<String>();
154 possibleTargetTypes.addAll(transformations
155 .getPossibleTargetTypes(aInputType));
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();
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);
179 if (!isAllowedByFilters(t.getToType(), transformed)) {
184 if (allowed && transformed != null) {
185 // all transformations done and all filters still
187 boolean result = destination.receive(transformed);
188 delivered = delivered || result;
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);
204 filteredTypes.add(type);
207 return filteredTypes;
210 private boolean isAllowedByFilters(String aType, DOMSource aEvent) {
211 boolean allowed = true;
212 for (Filter filter : filters.values()) {
213 if (!filter.isAllowed(aType, aEvent)) {
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());
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) + "'",
237 private void logEvent(String aMessage, String aSource, DOMSource aEvent) {
238 LOGGER.log(Level.WARNING,
239 aMessage + ": " + eventToString(aSource, aEvent));
242 private String eventToString(String aSource, DOMSource aEvent) {
243 return "source '" + aSource + "': Event: '" +
244 new XMLDocument(aEvent).print(true) + "'";
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));
255 private void destinationNotFound(String aSource, DOMSource aEvent) {
256 LOGGER.log(Level.WARNING, "No destination found for event: " +
257 eventToString(aSource, aEvent));
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));
270 public void unregisterDestination(Id<Destination> aId) {
271 destinations.remove(aId.getId());
274 private void notNull(String aName, Object aValue) {
275 if (aValue == null) {
276 throw new IllegalArgumentException("Parameter '" + aName +
277 "' may not be null");