001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.impl;
018
019 import java.util.concurrent.TimeUnit;
020
021 import org.apache.camel.Consumer;
022 import org.apache.camel.Exchange;
023 import org.apache.camel.Route;
024 import org.apache.camel.spi.ExceptionHandler;
025 import org.apache.camel.spi.RoutePolicy;
026 import org.apache.camel.support.ServiceSupport;
027 import org.apache.camel.util.ServiceHelper;
028 import org.slf4j.Logger;
029 import org.slf4j.LoggerFactory;
030
031 /**
032 * A base class for developing custom {@link RoutePolicy} implementations.
033 *
034 * @version
035 */
036 public abstract class RoutePolicySupport extends ServiceSupport implements RoutePolicy {
037
038 // TODO: Move to support package
039
040 protected final transient Logger log = LoggerFactory.getLogger(getClass());
041 private ExceptionHandler exceptionHandler;
042
043 public void onInit(Route route) {
044 // noop
045 }
046
047 public void onRemove(Route route) {
048 // noop
049 }
050
051 @Override
052 public void onStart(Route route) {
053 // noop
054 }
055
056 @Override
057 public void onStop(Route route) {
058 // noop
059 }
060
061 @Override
062 public void onSuspend(Route route) {
063 // noop
064 }
065
066 @Override
067 public void onResume(Route route) {
068 // noop
069 }
070
071 public void onExchangeBegin(Route route, Exchange exchange) {
072 // noop
073 }
074
075 public void onExchangeDone(Route route, Exchange exchange) {
076 // noop
077 }
078
079 protected boolean startConsumer(Consumer consumer) throws Exception {
080 boolean resumed = ServiceHelper.resumeService(consumer);
081 if (resumed) {
082 log.debug("Resuming consumer {}", consumer);
083 }
084 return resumed;
085 }
086
087 protected boolean stopConsumer(Consumer consumer) throws Exception {
088 boolean suspended = ServiceHelper.suspendService(consumer);
089 if (suspended) {
090 log.debug("Suspended consumer {}", consumer);
091 }
092 return suspended;
093 }
094
095 protected void startRoute(Route route) throws Exception {
096 route.getRouteContext().getCamelContext().startRoute(route.getId());
097 }
098
099 protected void stopRoute(Route route) throws Exception {
100 route.getRouteContext().getCamelContext().stopRoute(route.getId());
101 }
102
103 protected void stopRoute(Route route, long timeout, TimeUnit timeUnit) throws Exception {
104 route.getRouteContext().getCamelContext().stopRoute(route.getId(), timeout, timeUnit);
105 }
106
107 /**
108 * Handles the given exception using the {@link #getExceptionHandler()}
109 *
110 * @param t the exception to handle
111 */
112 protected void handleException(Throwable t) {
113 getExceptionHandler().handleException(t);
114 }
115
116 @Override
117 protected void doStart() throws Exception {
118 // noop
119 }
120
121 @Override
122 protected void doStop() throws Exception {
123 // noop
124 }
125
126 public ExceptionHandler getExceptionHandler() {
127 if (exceptionHandler == null) {
128 exceptionHandler = new LoggingExceptionHandler(getClass());
129 }
130 return exceptionHandler;
131 }
132
133 public void setExceptionHandler(ExceptionHandler exceptionHandler) {
134 this.exceptionHandler = exceptionHandler;
135 }
136
137 }