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