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.model;
018    
019    import org.apache.camel.CamelContext;
020    import org.apache.camel.CamelContextAware;
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.Processor;
023    import org.apache.camel.Route;
024    import org.apache.camel.builder.ProcessorFactory;
025    import org.apache.camel.impl.EventDrivenConsumerRoute;
026    
027    import javax.xml.bind.annotation.XmlRootElement;
028    
029    /**
030     * Represents an XML <route/> element
031     *
032     * @version $Revision: $
033     */
034    @XmlRootElement(name = "root")
035    public class RouteElement implements CamelContextAware, ProcessorFactory {
036        private String uri;
037        private Endpoint endpoint;
038        private CamelContext camelContext;
039    
040        public Route createRoute() throws Exception {
041            return new EventDrivenConsumerRoute(getEndpoint(), createProcessor());
042        }
043    
044        // Properties
045        //-----------------------------------------------------------------------
046        public String getUri() {
047            return uri;
048        }
049    
050        public void setUri(String uri) {
051            this.uri = uri;
052        }
053    
054        public CamelContext getCamelContext() {
055            return camelContext;
056        }
057    
058        public void setCamelContext(CamelContext camelContext) {
059            this.camelContext = camelContext;
060        }
061    
062        public Endpoint getEndpoint() {
063            if (endpoint == null) {
064                endpoint = resolveEndpoint();
065            }
066            return endpoint;
067        }
068    
069        public void setEndpoint(Endpoint endpoint) {
070            this.endpoint = endpoint;
071        }
072       
073        public Processor createProcessor() throws Exception {
074            return null;  // TODO
075        }
076    
077        protected Endpoint resolveEndpoint() {
078            CamelContext context = getCamelContext();
079            if (context == null) {
080                throw new IllegalArgumentException("No CamelContext has been injected!");
081            }
082            Endpoint answer = context.getEndpoint(getUri());
083            if (answer == null) {
084                throw new IllegalArgumentException("No Endpoint found for uri: " + getUri());
085            }
086            return answer;
087        }
088    }