All Downloads are FREE. Search and download functionalities are using the official Maven repository.

metridoc.camel.builder.GroovyRouteBuilder.groovy Maven / Gradle / Ivy

There is a newer version: 0.12
Show newest version
/*
 * Copyright 2010 Trustees of the University of Pennsylvania Licensed under the
 * Educational Community 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.osedu.org/licenses/ECL-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 metridoc.camel.builder

import org.apache.camel.Exchange
import org.apache.camel.builder.RouteBuilder
import org.apache.camel.model.RouteDefinition
import org.slf4j.Logger

/**
 *
 * @author Tommy Barker
 */
@Mixin(metridoc.dsl.PropertyMissing.class)
class GroovyRouteBuilder extends RouteBuilder {

    def Closure route
    def Boolean usePolling
    def Logger log
    def List routeExceptions

    @Override
    RouteDefinition from(String uri) {
        def newUri = uri

        if (usePolling(uri)) {
            newUri = "poll:${newUri}"
        }

        super.from newUri
    }

    def void configure() {
        //TODO: so if it is handled below, can we just use the default?
        //TODO: can someone provide their own global error handler which will override this?

        onException(Throwable.class).process {Exchange exchange ->
            def exception = exchange.getException()
            if(exception) {
                handleException(exception)
            } else {
                exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT)
                if(exception) {
                    handleException(exception)
                } else {
                    exception = new RuntimeException("An unknown exception occurred, if the log is not " +
                            "sufficient to determine what happened consider setting logging level to DEBUG")
                    handleException(exception)
                }
            }
        }

        route.delegate = this
        route.setDirective(Closure.DELEGATE_ONLY)
        route()
    }

    private void handleException(Throwable throwable) {
        if(routeExceptions) {
            log.error("an additional exception occurred", throwable)
            return //only catch the first one
        }

        routeExceptions.add(throwable)
    }

    private boolean usePolling(String uri) {

        def m = uri =~ /usePolling=(true|false)/
        def boolean usePollingInUri

        //uri based
        if (m.find()) {
            return Boolean.valueOf(m[0][1])
        }

        //global
        if (usePolling != null) {
            return usePolling
        }

        //defaults
        if (uri.startsWith("seda") || uri.startsWith("direct")) {
            return false
        } else {
            return true
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy