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    private boolean block;
043    @Metadata(defaultValue = "30000")
044    private long timeout = 30000L;
045    private HeaderFilterStrategy headerFilterStrategy;
046    @Metadata(defaultValue = "true")
047    private Boolean propagateProperties = Boolean.TRUE;
048
049    public DirectVmComponent() {
050        super(DirectVmEndpoint.class);
051    }
052
053    /**
054     * Gets all the consumer endpoints.
055     *
056     * @return consumer endpoints
057     */
058    public static Collection<Endpoint> getConsumerEndpoints() {
059        Collection<Endpoint> endpoints = new ArrayList<Endpoint>(CONSUMERS.size());
060        for (DirectVmConsumer consumer : CONSUMERS.values()) {
061            endpoints.add(consumer.getEndpoint());
062        }
063        return endpoints;
064    }
065
066    @Override
067    protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
068        DirectVmEndpoint answer = new DirectVmEndpoint(uri, this);
069        answer.setBlock(block);
070        answer.setTimeout(timeout);
071        answer.configureProperties(parameters);
072        setProperties(answer, parameters);
073        return answer;
074    }
075
076    public DirectVmConsumer getConsumer(DirectVmEndpoint endpoint) {
077        String key = getConsumerKey(endpoint.getEndpointUri());
078        return CONSUMERS.get(key);
079    }
080
081    public void addConsumer(DirectVmEndpoint endpoint, DirectVmConsumer consumer) {
082        String key = getConsumerKey(endpoint.getEndpointUri());
083        DirectVmConsumer existing = CONSUMERS.putIfAbsent(key, consumer);
084        if (existing != null) {
085            String contextId = existing.getEndpoint().getCamelContext().getName();
086            throw new IllegalStateException("A consumer " + existing + " already exists from CamelContext: " + contextId + ". Multiple consumers not supported");
087        }
088    }
089
090    public void removeConsumer(DirectVmEndpoint endpoint, DirectVmConsumer consumer) {
091        String key = getConsumerKey(endpoint.getEndpointUri());
092        CONSUMERS.remove(key);
093    }
094
095    private static String getConsumerKey(String uri) {
096        if (uri.contains("?")) {
097            // strip parameters
098            uri = uri.substring(0, uri.indexOf('?'));
099        }
100        return uri;
101    }
102
103    @Override
104    protected void doStart() throws Exception {
105        super.doStart();
106        START_COUNTER.incrementAndGet();
107    }
108
109    @Override
110    protected void doStop() throws Exception {
111        if (START_COUNTER.decrementAndGet() <= 0) {
112            // clear queues when no more direct-vm components in use
113            CONSUMERS.clear();
114        }
115        super.doStop();
116    }
117
118    public boolean isBlock() {
119        return block;
120    }
121
122    /**
123     * If sending a message to a direct endpoint which has no active consumer,
124     * then we can tell the producer to block and wait for the consumer to become active.
125     */
126    public void setBlock(boolean block) {
127        this.block = block;
128    }
129
130    public long getTimeout() {
131        return timeout;
132    }
133
134    /**
135     * The timeout value to use if block is enabled.
136     */
137    public void setTimeout(long timeout) {
138        this.timeout = timeout;
139    }
140
141    public HeaderFilterStrategy getHeaderFilterStrategy() {
142        return headerFilterStrategy;
143    }
144
145    /**
146     * Sets a {@link HeaderFilterStrategy} that will only be applied on producer endpoints (on both directions: request and response).
147     * <p>Default value: none.</p>
148     * @param headerFilterStrategy
149     */
150    public void setHeaderFilterStrategy(HeaderFilterStrategy headerFilterStrategy) {
151        this.headerFilterStrategy = headerFilterStrategy;
152    }
153
154    public boolean isPropagateProperties() {
155        return propagateProperties;
156    }
157
158    /**
159     * Whether to propagate or not properties from the producer side to the consumer side, and viceversa.
160     * <p>Default value: true.</p>
161     * @param propagateProperties
162     */
163    public void setPropagateProperties(boolean propagateProperties) {
164        this.propagateProperties = propagateProperties;
165    }
166
167}