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.component.ref;
018
019import org.apache.camel.Component;
020import org.apache.camel.Consumer;
021import org.apache.camel.DelegateEndpoint;
022import org.apache.camel.Endpoint;
023import org.apache.camel.Processor;
024import org.apache.camel.Producer;
025import org.apache.camel.impl.DefaultEndpoint;
026import org.apache.camel.spi.Metadata;
027import org.apache.camel.spi.UriEndpoint;
028import org.apache.camel.spi.UriPath;
029import org.apache.camel.util.CamelContextHelper;
030
031/**
032 * The ref component is used for lookup of existing endpoints bound in the Registry.
033 */
034@UriEndpoint(firstVersion = "1.2.0", scheme = "ref", title = "Ref", syntax = "ref:name", label = "core,endpoint")
035public class RefEndpoint extends DefaultEndpoint implements DelegateEndpoint {
036
037    private volatile Endpoint endpoint;
038
039    @UriPath @Metadata(required = "true")
040    private String name;
041
042    public RefEndpoint(String endpointUri, Component component) {
043        super(endpointUri, component);
044    }
045
046    public String getName() {
047        return name;
048    }
049
050    /**
051     * Name of endpoint to lookup in the registry.
052     */
053    public void setName(String name) {
054        this.name = name;
055    }
056
057    @Override
058    public Producer createProducer() throws Exception {
059        return endpoint.createProducer();
060    }
061
062    @Override
063    public Consumer createConsumer(Processor processor) throws Exception {
064        return endpoint.createConsumer(processor);
065    }
066
067    @Override
068    public boolean isSingleton() {
069        return true;
070    }
071
072    @Override
073    public Endpoint getEndpoint() {
074        return endpoint;
075    }
076
077    @Override
078    protected void doStart() throws Exception {
079        endpoint = CamelContextHelper.mandatoryLookup(getCamelContext(), name, Endpoint.class);
080        // add the endpoint to the endpoint registry
081        getCamelContext().addEndpoint(endpoint.getEndpointUri(), endpoint);
082        super.doStart();
083    }
084
085    @Override
086    protected void doStop() throws Exception {
087        super.doStop();
088        // noop
089    }
090}