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
019/**
020 * An interface to represent an object which wishes to be injected with
021 * a {@link Component}.
022 */
023public interface ComponentAware {
024
025    /**
026     * Injects the {@link Component}
027     *
028     * @param component the component
029     */
030    void setComponent(Component component);
031
032    /**
033     * Get the {@link Component}
034     *
035     * @return the component
036     */
037    Component getComponent();
038
039    /**
040     * Get the {@link Component} as the specified type.
041     *
042     * @param type the proprietary class or interface of the underlying concrete Component.
043     * @return an instance of the underlying concrete Component as the required type.
044     * @throws IllegalArgumentException if the component class can't be cast to required type,
045     */
046    default <T extends Component> T getComponent(Class<T> type) {
047        final Component component = getComponent();
048
049        if (component == null) {
050            return null;
051        }
052
053        if (Component.class.isAssignableFrom(type)) {
054            return type.cast(component);
055        }
056
057        throw new IllegalArgumentException(
058            "Unable to unwrap the Component type (" + component.getClass() + ") to the required type (" + type + ")"
059        );
060    }
061}