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.logging.Level;
22 import java.util.logging.Logger;
24 import javax.xml.transform.dom.DOMSource;
26 import org.wamblee.xml.XMLDocument;
27 import org.wamblee.xmlrouter.common.Id;
28 import org.wamblee.xmlrouter.subscribe.Destination;
31 * Provides robustness for the use of externally provided destinations by
32 * catching null return values and exceptions and providing usable default
35 * @author Erik Brakkee
38 public class RobustDestination implements Destination {
40 private static final Logger LOGGER = Logger
41 .getLogger(RobustDestination.class.getName());
43 private Id<Destination> id;
44 private Destination destination;
46 public RobustDestination(Id<Destination> aId, Destination aDestination) {
48 destination = aDestination;
52 public String getName() {
54 String res = destination.getName();
56 logNameReturnedNull();
57 return Constants.UNKNOWN_DESTINATION_NAME.toString();
60 } catch (Exception e) {
61 logNameThrewException(e);
62 return Constants.UNKNOWN_DESTINATION_NAME.toString();
67 public Collection<String> chooseFromTargetTypes(
68 Collection<String> aPossibleTargetTypes) {
70 Collection<String> res = destination
71 .chooseFromTargetTypes(aPossibleTargetTypes);
73 logChooseFromTargetTypesReturnedNull(aPossibleTargetTypes);
74 return Collections.EMPTY_LIST;
76 Collection<String> finalRes = new ArrayList<String>();
77 for (String type : res) {
78 if (!aPossibleTargetTypes.contains(type)) {
79 logChooseFromTargetTypesReturnedInvalidType(
80 aPossibleTargetTypes, type);
86 } catch (Exception e) {
87 logChooseFromTargetTypesThrewException(aPossibleTargetTypes, e);
88 return Collections.EMPTY_LIST;
93 public boolean receive(DOMSource aEvent) {
95 return destination.receive(aEvent);
96 } catch (Exception e) {
97 logReceiveThrewException(aEvent, e);
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 +
109 private void logNameReturnedNull() {
110 LOGGER.log(Level.WARNING,
111 "getName() returned null for destination id " + id +
112 " returning name " + Constants.UNKNOWN_DESTINATION_NAME +
116 private void logChooseFromTargetTypesReturnedInvalidType(
117 Collection<String> aPossibleTargetTypes, String aType) {
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() });
125 private void logChooseFromTargetTypesReturnedNull(
126 Collection<String> aPossibleTargetTypes) {
127 LOGGER.log(Level.WARNING,
128 "chooseFromTargetTypes() returned null for destination " + id);
131 private void logChooseFromTargetTypesThrewException(
132 Collection<String> aPossibleTargetTypes, Exception aE) {
136 "chooseFromTargetTypes() threw exception for destination " + id,
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);