X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=router%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FRobustDestination.java;fp=router%2Fimpl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FRobustDestination.java;h=3fc48d73c44cca1e163df3402c2ae886efed6ace;hb=8e41cb29f75362a792292d21b481bd06a9506296;hp=0000000000000000000000000000000000000000;hpb=9dbc2844950b55ae552fe74840954ea71b754c7a;p=xmlrouter diff --git a/router/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustDestination.java b/router/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustDestination.java new file mode 100644 index 0000000..3fc48d7 --- /dev/null +++ b/router/impl/src/main/java/org/wamblee/xmlrouter/impl/RobustDestination.java @@ -0,0 +1,145 @@ +/* + * Copyright 2005-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.xmlrouter.impl; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.logging.Level; +import java.util.logging.Logger; + +import javax.xml.transform.dom.DOMSource; + +import org.wamblee.xml.XMLDocument; +import org.wamblee.xmlrouter.common.Id; +import org.wamblee.xmlrouter.subscribe.Destination; + +/** + * Provides robustness for the use of externally provided destinations by + * catching null return values and exceptions and providing usable default + * return values. + * + * @author Erik Brakkee + * + */ +public class RobustDestination implements Destination { + + private static final Logger LOGGER = Logger + .getLogger(RobustDestination.class.getName()); + + private Id id; + private Destination destination; + + public RobustDestination(Id aId, Destination aDestination) { + id = aId; + destination = aDestination; + } + + @Override + public String getName() { + try { + String res = destination.getName(); + if (res == null) { + logNameReturnedNull(); + return Constants.UNKNOWN_DESTINATION_NAME.toString(); + } + return res; + } catch (Exception e) { + logNameThrewException(e); + return Constants.UNKNOWN_DESTINATION_NAME.toString(); + } + } + + @Override + public Collection chooseFromTargetTypes( + Collection aPossibleTargetTypes) { + try { + Collection res = destination + .chooseFromTargetTypes(aPossibleTargetTypes); + if (res == null) { + logChooseFromTargetTypesReturnedNull(aPossibleTargetTypes); + return Collections.EMPTY_LIST; + } + Collection finalRes = new ArrayList(); + for (String type : res) { + if (!aPossibleTargetTypes.contains(type)) { + logChooseFromTargetTypesReturnedInvalidType( + aPossibleTargetTypes, type); + } else { + finalRes.add(type); + } + } + return finalRes; + } catch (Exception e) { + logChooseFromTargetTypesThrewException(aPossibleTargetTypes, e); + return Collections.EMPTY_LIST; + } + } + + @Override + public boolean receive(DOMSource aEvent) { + try { + return destination.receive(aEvent); + } catch (Exception e) { + logReceiveThrewException(aEvent, e); + return false; + } + } + + private void logNameThrewException(Exception aE) { + LOGGER.log(Level.WARNING, + "getName() threw exception for destination id " + id + + " returning name " + Constants.UNKNOWN_DESTINATION_NAME + + " instead", aE); + } + + private void logNameReturnedNull() { + LOGGER.log(Level.WARNING, + "getName() returned null for destination id " + id + + " returning name " + Constants.UNKNOWN_DESTINATION_NAME + + " instead"); + } + + private void logChooseFromTargetTypesReturnedInvalidType( + Collection aPossibleTargetTypes, String aType) { + LOGGER + .log( + Level.WARNING, + "chooseFromTargetTypes() for destination {0} returned invalid type ''{1}'' which is not in the offered set of types {2}", + new Object[] { id, aType, aPossibleTargetTypes.toString() }); + } + + private void logChooseFromTargetTypesReturnedNull( + Collection aPossibleTargetTypes) { + LOGGER.log(Level.WARNING, + "chooseFromTargetTypes() returned null for destination " + id); + } + + private void logChooseFromTargetTypesThrewException( + Collection aPossibleTargetTypes, Exception aE) { + LOGGER + .log( + Level.WARNING, + "chooseFromTargetTypes() threw exception for destination " + id, + aE); + } + + private void logReceiveThrewException(DOMSource aEvent, Exception aE) { + LOGGER.log(Level.WARNING, "Receive threw exception for destination " + + id + " for event " + new XMLDocument(aEvent).print(true), aE); + } + +}