1 package org.wamblee.xmlrouter.impl;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.logging.Level;
7 import java.util.logging.Logger;
9 import javax.xml.transform.dom.DOMSource;
11 import org.wamblee.xml.XMLDocument;
12 import org.wamblee.xmlrouter.common.Id;
13 import org.wamblee.xmlrouter.subscribe.Destination;
16 * Provides robustness for the use of externally provided destinations by
17 * catching null return values and exceptions and providing usable default
20 * @author Erik Brakkee
23 public class RobustDestination implements Destination {
25 private static final Logger LOGGER = Logger
26 .getLogger(RobustDestination.class.getName());
28 private Id<Destination> id;
29 private Destination destination;
31 public RobustDestination(Id<Destination> aId, Destination aDestination) {
33 destination = aDestination;
37 public String getName() {
39 String res = destination.getName();
41 logNameReturnedNull();
42 return Constants.UNKNOWN_DESTINATION_NAME.toString();
45 } catch (Exception e) {
46 logNameThrewException(e);
47 return Constants.UNKNOWN_DESTINATION_NAME.toString();
52 public Collection<String> chooseFromTargetTypes(
53 Collection<String> aPossibleTargetTypes) {
55 Collection<String> res = destination
56 .chooseFromTargetTypes(aPossibleTargetTypes);
58 logChooseFromTargetTypesReturnedNull(aPossibleTargetTypes);
59 return Collections.EMPTY_LIST;
61 Collection<String> finalRes = new ArrayList<String>();
62 for (String type : res) {
63 if (!aPossibleTargetTypes.contains(type)) {
64 logChooseFromTargetTypesReturnedInvalidType(
65 aPossibleTargetTypes, type);
71 } catch (Exception e) {
72 logChooseFromTargetTypesThrewException(aPossibleTargetTypes, e);
73 return Collections.EMPTY_LIST;
78 public boolean receive(DOMSource aEvent) {
80 return destination.receive(aEvent);
81 } catch (Exception e) {
82 logReceiveThrewException(aEvent, e);
87 private void logNameThrewException(Exception aE) {
88 LOGGER.log(Level.WARNING,
89 "getName() threw exception for destination id " + id +
90 " returning name " + Constants.UNKNOWN_DESTINATION_NAME +
94 private void logNameReturnedNull() {
95 LOGGER.log(Level.WARNING,
96 "getName() returned null for destination id " + id +
97 " returning name " + Constants.UNKNOWN_DESTINATION_NAME +
101 private void logChooseFromTargetTypesReturnedInvalidType(
102 Collection<String> aPossibleTargetTypes, String aType) {
106 "chooseFromTargetTypes() for destination {0} returned invalid type ''{1}'' which is not in the offered set of types {2}",
107 new Object[] { id, aType, aPossibleTargetTypes.toString() });
110 private void logChooseFromTargetTypesReturnedNull(
111 Collection<String> aPossibleTargetTypes) {
112 LOGGER.log(Level.WARNING,
113 "chooseFromTargetTypes() returned null for destination " + id);
116 private void logChooseFromTargetTypesThrewException(
117 Collection<String> aPossibleTargetTypes, Exception aE) {
121 "chooseFromTargetTypes() threw exception for destination " + id,
125 private void logReceiveThrewException(DOMSource aEvent, Exception aE) {
126 LOGGER.log(Level.WARNING, "Receive threw exception for destination " +
127 id + " for event " + new XMLDocument(aEvent).print(true), aE);