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 org.apache.camel.CamelContext;
020    import org.apache.camel.Component;
021    import org.apache.camel.Endpoint;
022    import org.apache.camel.EndpointResolver;
023    import org.apache.camel.util.FactoryFinder;
024    import org.apache.camel.util.ObjectHelper;
025    
026    /**
027     * An implementation of {@link org.apache.camel.EndpointResolver} that delegates to 
028     * other {@link EndpointResolver} which are selected based on the uri prefix.
029     * 
030     * The delegate {@link EndpointResolver} are associated with uri prefixes by 
031     * adding a property file with the same uri prefix in the
032     * META-INF/services/org/apache/camel/EndpointResolver/
033     * directory on the classpath.
034     *
035     * @version $Revision: 520985 $
036     */
037    public class DefaultEndpointResolver<E> implements EndpointResolver<E> {
038        static final private FactoryFinder endpointResolverFactory = new FactoryFinder("META-INF/services/org/apache/camel/EndpointResolver/");
039        
040        public Endpoint<E> resolveEndpoint(CamelContext container, String uri) throws Exception {
041            EndpointResolver resolver = getDelegate(uri);
042                    return resolver.resolveEndpoint(container, uri);
043        }
044    
045            public Component resolveComponent(CamelContext container, String uri) throws Exception {
046            EndpointResolver resolver = getDelegate(uri);
047                    return resolver.resolveComponent(container, uri);
048            }
049    
050            private EndpointResolver getDelegate(String uri) {
051                    String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 2);
052            if( splitURI[1] == null )
053                    throw new IllegalArgumentException("Invalid URI, it did not contain a scheme: "+uri);
054            EndpointResolver resolver;
055                    try {
056                            resolver = (EndpointResolver) endpointResolverFactory.newInstance(splitURI[0]);
057                    } catch (Throwable e) {
058                            throw new IllegalArgumentException("Invalid URI, no EndpointResolver registered for scheme : "+splitURI[0], e);
059                    }
060                    return resolver;
061            }
062    
063    }