6de2c19c65877d4403e57b8b10b3a26b1a565542
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / RobustDestination.java
1 package org.wamblee.xmlrouter.impl;
2
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;
8
9 import javax.xml.transform.dom.DOMSource;
10
11 import org.wamblee.xml.XMLDocument;
12 import org.wamblee.xmlrouter.common.Id;
13 import org.wamblee.xmlrouter.subscribe.Destination;
14
15 /**
16  * Provides robustness for the use of externally provided destinations by
17  * catching null return values and exceptions and providing usable default
18  * return values.
19  * 
20  * @author Erik Brakkee
21  * 
22  */
23 public class RobustDestination implements Destination {
24
25     private static final Logger LOGGER = Logger
26         .getLogger(RobustDestination.class.getName());
27
28     private Id<Destination> id;
29     private Destination destination;
30
31     public RobustDestination(Id<Destination> aId, Destination aDestination) {
32         id = aId;
33         destination = aDestination;
34     }
35
36     @Override
37     public String getName() {
38         try {
39             String res = destination.getName();
40             if (res == null) {
41                 logNameReturnedNull();
42                 return Constants.UNKNOWN_DESTINATION_NAME.toString();
43             }
44             return res;
45         } catch (Exception e) {
46             logNameThrewException(e);
47             return Constants.UNKNOWN_DESTINATION_NAME.toString();
48         }
49     }
50
51     @Override
52     public Collection<String> chooseFromTargetTypes(
53         Collection<String> aPossibleTargetTypes) {
54         try {
55             Collection<String> res = destination
56                 .chooseFromTargetTypes(aPossibleTargetTypes);
57             if (res == null) {
58                 logChooseFromTargetTypesReturnedNull(aPossibleTargetTypes);
59                 return Collections.EMPTY_LIST;
60             }
61             Collection<String> finalRes = new ArrayList<String>();
62             for (String type : res) {
63                 if (!aPossibleTargetTypes.contains(type)) {
64                     logChooseFromTargetTypesReturnedInvalidType(
65                         aPossibleTargetTypes, type);
66                 } else {
67                     finalRes.add(type);
68                 }
69             }
70             return finalRes;
71         } catch (Exception e) {
72             logChooseFromTargetTypesThrewException(aPossibleTargetTypes, e);
73             return Collections.EMPTY_LIST;
74         }
75     }
76
77     @Override
78     public boolean receive(DOMSource aEvent) {
79         try {
80             return destination.receive(aEvent);
81         } catch (Exception e) {
82             logReceiveThrewException(aEvent, e);
83             return false;
84         }
85     }
86
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 +
91                 " instead", aE);
92     }
93
94     private void logNameReturnedNull() {
95         LOGGER.log(Level.WARNING,
96             "getName() returned null for destination id " + id +
97                 " returning name " + Constants.UNKNOWN_DESTINATION_NAME +
98                 " instead");
99     }
100
101     private void logChooseFromTargetTypesReturnedInvalidType(
102         Collection<String> aPossibleTargetTypes, String aType) {
103         LOGGER
104             .log(
105                 Level.WARNING,
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() });
108     }
109
110     private void logChooseFromTargetTypesReturnedNull(
111         Collection<String> aPossibleTargetTypes) {
112         LOGGER.log(Level.WARNING,
113             "chooseFromTargetTypes() returned null for destination " + id);
114     }
115
116     private void logChooseFromTargetTypesThrewException(
117         Collection<String> aPossibleTargetTypes, Exception aE) {
118         LOGGER
119             .log(
120                 Level.WARNING,
121                 "chooseFromTargetTypes() threw exception for destination " + id,
122                 aE);
123     }
124
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);
128     }
129
130 }