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.Collection;
020 import java.util.List;
021
022 import org.apache.camel.CamelContext;
023 import org.apache.camel.Route;
024 import org.apache.camel.Service;
025 import org.apache.camel.model.RouteDefinition;
026 import org.apache.camel.spi.LifecycleStrategy;
027 import org.apache.camel.spi.RouteContext;
028
029 /**
030 * Represents the runtime objects for a given {@link RouteDefinition} so that it can be stopped independently
031 * of other routes
032 *
033 * @version $Revision: 1.1 $
034 */
035 public class RouteService extends ServiceSupport {
036
037 private final DefaultCamelContext camelContext;
038 private final RouteDefinition routeType;
039 private final List<RouteContext> routeContexts;
040 private final Collection<Route> routes;
041 private String id;
042
043 public RouteService(DefaultCamelContext camelContext, RouteDefinition routeType, List<RouteContext> routeContexts, Collection<Route> routes) {
044 this.camelContext = camelContext;
045 this.routeType = routeType;
046 this.routeContexts = routeContexts;
047 this.routes = routes;
048 this.id = routeType.idOrCreate();
049 }
050
051 public String getId() {
052 return id;
053 }
054
055 public CamelContext getCamelContext() {
056 return camelContext;
057 }
058
059 public List<RouteContext> getRouteContexts() {
060 return routeContexts;
061 }
062
063 public RouteDefinition getRouteType() {
064 return routeType;
065 }
066
067 public Collection<Route> getRoutes() {
068 return routes;
069 }
070
071 protected void doStart() throws Exception {
072 camelContext.addRouteCollection(routes);
073
074 getLifecycleStrategy().onRoutesAdd(routes);
075
076 for (Route route : routes) {
077 List<Service> services = route.getServicesForRoute();
078 for (Service service : services) {
079 startChildService(service);
080 }
081 }
082 }
083
084 protected void doStop() throws Exception {
085 camelContext.removeRouteCollection(routes);
086 // TODO should we stop the actual Route objects??
087 }
088
089 protected LifecycleStrategy getLifecycleStrategy() {
090 return camelContext.getLifecycleStrategy();
091 }
092
093 protected void startChildService(Service service) throws Exception {
094 getLifecycleStrategy().onServiceAdd(camelContext, service);
095 service.start();
096 addChildService(service);
097 }
098 }