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 029 /** 030 * A <a href="http://camel.apache.org/routes.html">Route</a> 031 * defines the processing used on an inbound message exchange 032 * from a specific {@link org.apache.camel.Endpoint} within a {@link org.apache.camel.CamelContext} 033 * 034 * @version $Revision: 808777 $ 035 */ 036 public abstract class DefaultRoute extends ServiceSupport implements Route { 037 038 private final Endpoint endpoint; 039 private final Map<String, Object> properties = new HashMap<String, Object>(); 040 private final List<Service> services = new ArrayList<Service>(); 041 private final RouteContext routeContext; 042 043 public DefaultRoute(RouteContext routeContext, Endpoint endpoint) { 044 this.routeContext = routeContext; 045 this.endpoint = endpoint; 046 } 047 048 public DefaultRoute(RouteContext routeContext, Endpoint endpoint, Service... services) { 049 this(routeContext, endpoint); 050 for (Service service : services) { 051 addService(service); 052 } 053 } 054 055 @Override 056 public String toString() { 057 return "Route " + getId(); 058 } 059 060 public String getId() { 061 return (String) properties.get(Route.ID_PROPERTY); 062 } 063 064 public Endpoint getEndpoint() { 065 return endpoint; 066 } 067 068 public RouteContext getRouteContext() { 069 return routeContext; 070 } 071 072 public Map<String, Object> getProperties() { 073 return properties; 074 } 075 076 public List<Service> getServicesForRoute() throws Exception { 077 List<Service> servicesForRoute = new ArrayList<Service>(getServices()); 078 addServices(servicesForRoute); 079 return servicesForRoute; 080 } 081 082 public void onStartingServices(List<Service> services) throws Exception { 083 addServices(services); 084 } 085 086 public List<Service> getServices() { 087 return services; 088 } 089 090 public void addService(Service service) { 091 getServices().add(service); 092 } 093 094 /** 095 * Strategy method to allow derived classes to lazily load services for the route 096 */ 097 protected void addServices(List<Service> services) throws Exception { 098 } 099 100 protected void doStart() throws Exception { 101 } 102 103 protected void doStop() throws Exception { 104 // clear services when stopping 105 services.clear(); 106 } 107 108 }