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.ArrayList;
020 import java.util.HashMap;
021 import java.util.List;
022 import java.util.Map;
023
024 import org.apache.camel.Endpoint;
025 import org.apache.camel.Route;
026 import org.apache.camel.Service;
027 import org.apache.camel.spi.RouteContext;
028 import org.apache.camel.support.ServiceSupport;
029
030 /**
031 * Default implementation of {@link Route}.
032 * <p/>
033 * Use the API from {@link org.apache.camel.CamelContext} to control the lifecycle of a route,
034 * such as starting and stopping using the {@link org.apache.camel.CamelContext#startRoute(String)}
035 * and {@link org.apache.camel.CamelContext#stopRoute(String)} methods.
036 *
037 * @version
038 */
039 public abstract class DefaultRoute extends ServiceSupport implements Route {
040
041 private final Endpoint endpoint;
042 private final Map<String, Object> properties = new HashMap<String, Object>();
043 private final List<Service> services = new ArrayList<Service>();
044 private final RouteContext routeContext;
045
046 public DefaultRoute(RouteContext routeContext, Endpoint endpoint) {
047 this.routeContext = routeContext;
048 this.endpoint = endpoint;
049 }
050
051 public DefaultRoute(RouteContext routeContext, Endpoint endpoint, Service... services) {
052 this(routeContext, endpoint);
053 for (Service service : services) {
054 addService(service);
055 }
056 }
057
058 @Override
059 public String toString() {
060 return "Route " + getId();
061 }
062
063 public String getId() {
064 return (String) properties.get(Route.ID_PROPERTY);
065 }
066
067 public Endpoint getEndpoint() {
068 return endpoint;
069 }
070
071 public RouteContext getRouteContext() {
072 return routeContext;
073 }
074
075 public Map<String, Object> getProperties() {
076 return properties;
077 }
078
079 public void onStartingServices(List<Service> services) throws Exception {
080 addServices(services);
081 }
082
083 public List<Service> getServices() {
084 return services;
085 }
086
087 public void addService(Service service) {
088 if (!services.contains(service)) {
089 services.add(service);
090 }
091 }
092
093 public void warmUp() {
094 getServices().clear();
095 }
096
097 /**
098 * Do not invoke this method directly, use {@link org.apache.camel.CamelContext#startRoute(String)} to start a route.
099 */
100 @Override
101 public void start() throws Exception {
102 super.start();
103 }
104
105 /**
106 * Do not invoke this method directly, use {@link org.apache.camel.CamelContext#stopRoute(String)} to stop a route.
107 */
108 @Override
109 public void stop() throws Exception {
110 super.stop();
111 }
112
113 /**
114 * Strategy method to allow derived classes to lazily load services for the route
115 */
116 protected void addServices(List<Service> services) throws Exception {
117 }
118
119 protected void doStart() throws Exception {
120 // noop
121 }
122
123 protected void doStop() throws Exception {
124 // noop
125 }
126
127 @Override
128 protected void doShutdown() throws Exception {
129 // clear services when shutting down
130 services.clear();
131 }
132 }