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.directvm;
018
019import java.util.ArrayList;
020import java.util.Collection;
021import java.util.Map;
022import java.util.concurrent.ConcurrentHashMap;
023import java.util.concurrent.ConcurrentMap;
024import java.util.concurrent.atomic.AtomicInteger;
025
026import org.apache.camel.Endpoint;
027import org.apache.camel.impl.UriEndpointComponent;
028import org.apache.camel.spi.HeaderFilterStrategy;
029import org.apache.camel.spi.Metadata;
030
031/**
032 * The <a href="http://camel.apache.org/direct-vm.html">Direct VM Component</a> manages {@link DirectVmEndpoint} and holds the list of named direct-vm endpoints.
033 */
034public class DirectVmComponent extends UriEndpointComponent {
035
036    private static final AtomicInteger START_COUNTER = new AtomicInteger();
037
038    // must keep a map of consumers on the component to ensure endpoints can lookup old consumers
039    // later in case the DirectVmEndpoint was re-created due the old was evicted from the endpoints LRUCache
040    // on DefaultCamelContext
041    private static final ConcurrentMap<String, DirectVmConsumer> CONSUMERS = new ConcurrentHashMap<String, DirectVmConsumer>();
042    @Metadata(label = "producer")
043    private boolean block;
044    @Metadata(label = "producer", defaultValue = "30000")
045    private long timeout = 30000L;
046    @Metadata(label = "advanced")
047    private HeaderFilterStrategy headerFilterStrategy;
048    @Metadata(label = "advanced", defaultValue = "true")
049    private boolean propagateProperties = true;
050
051    public DirectVmComponent() {
052        super(DirectVmEndpoint.class);
053    }
054
055    /**
056     * Gets all the consumer endpoints.
057     *
058     * @return consumer endpoints
059     */
060    public static Collection<Endpoint> getConsumerEndpoints() {
061        Collection<Endpoint> endpoints = new ArrayList<Endpoint>(CONSUMERS.size());
062        for (DirectVmConsumer consumer : CONSUMERS.values()) {
063            endpoints.add(consumer.getEndpoint());
064        }
065        return endpoints;
066    }
067
068    @Override
069    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
070        DirectVmEndpoint answer = new DirectVmEndpoint(uri, this);
071        answer.setBlock(block);
072        answer.setTimeout(timeout);
073        answer.setPropagateProperties(propagateProperties);
074        answer.configureProperties(parameters);
075        setProperties(answer, parameters);
076        return answer;
077    }
078
079    public DirectVmConsumer getConsumer(DirectVmEndpoint endpoint) {
080        String key = getConsumerKey(endpoint.getEndpointUri());
081        return CONSUMERS.get(key);
082    }
083
084    public void addConsumer(DirectVmEndpoint endpoint, DirectVmConsumer consumer) {
085        String key = getConsumerKey(endpoint.getEndpointUri());
086        DirectVmConsumer existing = CONSUMERS.putIfAbsent(key, consumer);
087        if (existing != null) {
088            String contextId = existing.getEndpoint().getCamelContext().getName();
089            throw new IllegalStateException("A consumer " + existing + " already exists from CamelContext: " + contextId + ". Multiple consumers not supported");
090        }
091    }
092
093    public void removeConsumer(DirectVmEndpoint endpoint, DirectVmConsumer consumer) {
094        String key = getConsumerKey(endpoint.getEndpointUri());
095        CONSUMERS.remove(key);
096    }
097
098    private static String getConsumerKey(String uri) {
099        if (uri.contains("?")) {
100            // strip parameters
101            uri = uri.substring(0, uri.indexOf('?'));
102        }
103        return uri;
104    }
105
106    @Override
107    protected void doStart() throws Exception {
108        super.doStart();
109        START_COUNTER.incrementAndGet();
110    }
111
112    @Override
113    protected void doStop() throws Exception {
114        if (START_COUNTER.decrementAndGet() <= 0) {
115            // clear queues when no more direct-vm components in use
116            CONSUMERS.clear();
117        }
118        super.doStop();
119    }
120
121    public boolean isBlock() {
122        return block;
123    }
124
125    /**
126     * If sending a message to a direct endpoint which has no active consumer,
127     * then we can tell the producer to block and wait for the consumer to become active.
128     */
129    public void setBlock(boolean block) {
130        this.block = block;
131    }
132
133    public long getTimeout() {
134        return timeout;
135    }
136
137    /**
138     * The timeout value to use if block is enabled.
139     */
140    public void setTimeout(long timeout) {
141        this.timeout = timeout;
142    }
143
144    public HeaderFilterStrategy getHeaderFilterStrategy() {
145        return headerFilterStrategy;
146    }
147
148    /**
149     * Sets a {@link HeaderFilterStrategy} that will only be applied on producer endpoints (on both directions: request and response).
150     * <p>Default value: none.</p>
151     */
152    public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
153        this.headerFilterStrategy = headerFilterStrategy;
154    }
155
156    public boolean isPropagateProperties() {
157        return propagateProperties;
158    }
159
160    /**
161     * Whether to propagate or not properties from the producer side to the consumer side, and vice versa.
162     * <p>Default value: true.</p>
163     */
164    public void setPropagateProperties(boolean propagateProperties) {
165        this.propagateProperties = propagateProperties;
166    }
167
168}