Turned the API packages into bundles.
[xmlrouter] / router / impl / src / main / java / org / wamblee / xmlrouter / impl / RobustDestination.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.logging.Level;
22 import java.util.logging.Logger;
23
24 import javax.xml.transform.dom.DOMSource;
25
26 import org.wamblee.xml.XMLDocument;
27 import org.wamblee.xmlrouter.common.Id;
28 import org.wamblee.xmlrouter.subscribe.Destination;
29
30 /**
31  * Provides robustness for the use of externally provided destinations by
32  * catching null return values and exceptions and providing usable default
33  * return values.
34  * 
35  * @author Erik Brakkee
36  * 
37  */
38 public class RobustDestination implements Destination {
39
40     private static final Logger LOGGER = Logger
41         .getLogger(RobustDestination.class.getName());
42
43     private Id<Destination> id;
44     private Destination destination;
45
46     public RobustDestination(Id<Destination> aId, Destination aDestination) {
47         id = aId;
48         destination = aDestination;
49     }
50
51     @Override
52     public String getName() {
53         try {
54             String res = destination.getName();
55             if (res == null) {
56                 logNameReturnedNull();
57                 return Constants.UNKNOWN_DESTINATION_NAME.toString();
58             }
59             return res;
60         } catch (Exception e) {
61             logNameThrewException(e);
62             return Constants.UNKNOWN_DESTINATION_NAME.toString();
63         }
64     }
65
66     @Override
67     public Collection<String> chooseFromTargetTypes(
68         Collection<String> aPossibleTargetTypes) {
69         try {
70             Collection<String> res = destination
71                 .chooseFromTargetTypes(aPossibleTargetTypes);
72             if (res == null) {
73                 logChooseFromTargetTypesReturnedNull(aPossibleTargetTypes);
74                 return Collections.EMPTY_LIST;
75             }
76             Collection<String> finalRes = new ArrayList<String>();
77             for (String type : res) {
78                 if (!aPossibleTargetTypes.contains(type)) {
79                     logChooseFromTargetTypesReturnedInvalidType(
80                         aPossibleTargetTypes, type);
81                 } else {
82                     finalRes.add(type);
83                 }
84             }
85             return finalRes;
86         } catch (Exception e) {
87             logChooseFromTargetTypesThrewException(aPossibleTargetTypes, e);
88             return Collections.EMPTY_LIST;
89         }
90     }
91
92     @Override
93     public boolean receive(DOMSource aEvent) {
94         try {
95             return destination.receive(aEvent);
96         } catch (Exception e) {
97             logReceiveThrewException(aEvent, e);
98             return false;
99         }
100     }
101
102     private void logNameThrewException(Exception aE) {
103         LOGGER.log(Level.WARNING,
104             "getName() threw exception for destination id " + id +
105                 " returning name " + Constants.UNKNOWN_DESTINATION_NAME +
106                 " instead", aE);
107     }
108
109     private void logNameReturnedNull() {
110         LOGGER.log(Level.WARNING,
111             "getName() returned null for destination id " + id +
112                 " returning name " + Constants.UNKNOWN_DESTINATION_NAME +
113                 " instead");
114     }
115
116     private void logChooseFromTargetTypesReturnedInvalidType(
117         Collection<String> aPossibleTargetTypes, String aType) {
118         LOGGER
119             .log(
120                 Level.WARNING,
121                 "chooseFromTargetTypes() for destination {0} returned invalid type ''{1}'' which is not in the offered set of types {2}",
122                 new Object[] { id, aType, aPossibleTargetTypes.toString() });
123     }
124
125     private void logChooseFromTargetTypesReturnedNull(
126         Collection<String> aPossibleTargetTypes) {
127         LOGGER.log(Level.WARNING,
128             "chooseFromTargetTypes() returned null for destination " + id);
129     }
130
131     private void logChooseFromTargetTypesThrewException(
132         Collection<String> aPossibleTargetTypes, Exception aE) {
133         LOGGER
134             .log(
135                 Level.WARNING,
136                 "chooseFromTargetTypes() threw exception for destination " + id,
137                 aE);
138     }
139
140     private void logReceiveThrewException(DOMSource aEvent, Exception aE) {
141         LOGGER.log(Level.WARNING, "Receive threw exception for destination " +
142             id + " for event " + new XMLDocument(aEvent).print(true), aE);
143     }
144
145 }