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 */
017package org.apache.camel.impl;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.camel.Endpoint;
025import org.apache.camel.Route;
026import org.apache.camel.Service;
027import org.apache.camel.spi.RouteContext;
028import 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 */
039public 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 String getDescription() {
080        Object value = properties.get(Route.DESCRIPTION_PROPERTY);
081        return value != null ? value.toString() : null;
082    }
083
084    public void onStartingServices(List<Service> services) throws Exception {
085        addServices(services);
086    }
087
088    public List<Service> getServices() {
089        return services;
090    }
091
092    public void addService(Service service) {
093        if (!services.contains(service)) {
094            services.add(service);
095        }
096    }
097
098    public void warmUp() {
099        getServices().clear();
100    }
101
102    /**
103     * Do not invoke this method directly, use {@link org.apache.camel.CamelContext#startRoute(String)} to start a route.
104     */
105    @Override
106    public void start() throws Exception {
107        super.start();
108    }
109
110    /**
111     * Do not invoke this method directly, use {@link org.apache.camel.CamelContext#stopRoute(String)} to stop a route.
112     */
113    @Override
114    public void stop() throws Exception {
115        super.stop();
116    }
117
118    /**
119     * Strategy method to allow derived classes to lazily load services for the route
120     */
121    protected void addServices(List<Service> services) throws Exception {
122    }
123
124    protected void doStart() throws Exception {
125        // noop
126    }
127
128    protected void doStop() throws Exception {
129        // noop
130    }
131
132    @Override
133    protected void doShutdown() throws Exception {
134        // clear services when shutting down
135        services.clear();
136    }
137}