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;
018
019import java.util.Collection;
020import java.util.Collections;
021import java.util.Optional;
022
023import org.apache.camel.component.extension.ComponentExtension;
024
025/**
026 * A <a href="http://camel.apache.org/component.html">component</a> is
027 * a factory of {@link Endpoint} objects.
028 */
029public interface Component extends CamelContextAware {
030
031    /**
032     * Attempt to resolve an endpoint for the given URI if the component is
033     * capable of handling the URI.
034     * <p/>
035     * See {@link #useRawUri()} for controlling whether the passed in uri
036     * should be as-is (raw), or encoded (default).
037     * 
038     * @param uri the URI to create; either raw or encoded (default)
039     * @return a newly created {@link Endpoint} or null if this component cannot create
040     *         {@link Endpoint} instances using the given uri
041     * @throws Exception is thrown if error creating the endpoint
042     * @see #useRawUri()
043     */
044    Endpoint createEndpoint(String uri) throws Exception;
045
046    /**
047     * Whether to use raw or encoded uri, when creating endpoints.
048     * <p/>
049     * <b>Notice:</b> When using raw uris, then the parameter values is raw as well.
050     *
051     * @return <tt>true</tt> to use raw uris, <tt>false</tt> to use encoded uris (default).
052     *
053     * @since Camel 2.11.0
054     */
055    boolean useRawUri();
056
057    /**
058     * Gets a list of supported extensions.
059     *
060     * @return the list of extensions.
061     */
062    default Collection<Class<? extends ComponentExtension>> getSupportedExtensions() {
063        return Collections.emptyList();
064    }
065
066    /**
067     * Gets the extension of the given type.
068     *
069     * @param extensionType tye type of the extensions
070     * @return an optional extension
071     */
072    default <T extends ComponentExtension> Optional<T> getExtension(Class<T> extensionType) {
073        return Optional.empty();
074    }
075
076    /**
077     * Set the {@link Component} context if the component is an instance of {@link ComponentAware}.
078     */
079    static <T> T trySetComponent(T object, Component component) {
080        if (object instanceof ComponentAware) {
081            ((ComponentAware) object).setComponent(component);
082        }
083
084        return object;
085    }
086
087}