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