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 */
017 package org.apache.camel.impl;
018
019 import java.lang.reflect.ParameterizedType;
020 import java.lang.reflect.Type;
021 import java.util.Map;
022 import java.util.concurrent.ExecutorService;
023 import java.util.concurrent.ScheduledExecutorService;
024
025 import org.apache.camel.CamelContext;
026 import org.apache.camel.CamelContextAware;
027 import org.apache.camel.Component;
028 import org.apache.camel.Endpoint;
029 import org.apache.camel.Exchange;
030 import org.apache.camel.ExchangePattern;
031 import org.apache.camel.PollingConsumer;
032 import org.apache.camel.util.ObjectHelper;
033 import org.apache.camel.util.concurrent.ExecutorServiceHelper;
034
035 /**
036 * A default endpoint useful for implementation inheritance
037 *
038 * @version $Revision: 772501 $
039 */
040 public abstract class DefaultEndpoint implements Endpoint, CamelContextAware {
041 private static final int DEFAULT_THREADPOOL_SIZE = 5;
042
043 private String endpointUri;
044 private CamelContext camelContext;
045 private Component component;
046 private ExecutorService executorService;
047 private ExchangePattern exchangePattern = ExchangePattern.InOnly;
048
049 protected DefaultEndpoint(String endpointUri, Component component) {
050 this(endpointUri, component.getCamelContext());
051 this.component = component;
052 }
053
054 protected DefaultEndpoint(String endpointUri, CamelContext camelContext) {
055 this(endpointUri);
056 this.camelContext = camelContext;
057 }
058
059 protected DefaultEndpoint(String endpointUri) {
060 this.setEndpointUri(endpointUri);
061 }
062
063 protected DefaultEndpoint() {
064 }
065
066 public int hashCode() {
067 return getEndpointUri().hashCode() * 37 + 1;
068 }
069
070 @Override
071 public boolean equals(Object object) {
072 if (object instanceof DefaultEndpoint) {
073 DefaultEndpoint that = (DefaultEndpoint) object;
074 return ObjectHelper.equal(this.getEndpointUri(), that.getEndpointUri());
075 }
076 return false;
077 }
078
079 @Override
080 public String toString() {
081 return "Endpoint[" + getEndpointUri() + "]";
082 }
083
084 public String getEndpointUri() {
085 if (endpointUri == null) {
086 endpointUri = createEndpointUri();
087 if (endpointUri == null) {
088 throw new IllegalArgumentException("endpointUri is not specified and " + getClass().getName()
089 + " does not implement createEndpointUri() to create a default value");
090 }
091 }
092 return endpointUri;
093 }
094
095 public CamelContext getCamelContext() {
096 return camelContext;
097 }
098
099 public Component getComponent() {
100 return component;
101 }
102
103 public void setCamelContext(CamelContext camelContext) {
104 this.camelContext = camelContext;
105 }
106
107 public synchronized ExecutorService getExecutorService() {
108 if (executorService == null) {
109 Component c = getComponent();
110 if (c instanceof DefaultComponent) {
111 DefaultComponent dc = (DefaultComponent) c;
112 executorService = dc.getExecutorService();
113 }
114 if (executorService == null) {
115 executorService = createScheduledExecutorService();
116 }
117 }
118 return executorService;
119 }
120
121 public synchronized ScheduledExecutorService getScheduledExecutorService() {
122 ExecutorService executor = getExecutorService();
123 if (executor instanceof ScheduledExecutorService) {
124 return (ScheduledExecutorService) executor;
125 } else {
126 return createScheduledExecutorService();
127 }
128 }
129
130 public synchronized void setExecutorService(ExecutorService executorService) {
131 this.executorService = executorService;
132 }
133
134 public PollingConsumer createPollingConsumer() throws Exception {
135 return new EventDrivenPollingConsumer(this);
136 }
137
138 /**
139 * Converts the given exchange to the specified exchange type
140 */
141 public Exchange convertTo(Class<Exchange> type, Exchange exchange) {
142 if (type.isInstance(exchange)) {
143 return type.cast(exchange);
144 }
145 return getCamelContext().getExchangeConverter().convertTo(type, exchange);
146 }
147
148 public Exchange createExchange(Exchange exchange) {
149 Class<Exchange> exchangeType = getExchangeType();
150 if (exchangeType != null) {
151 if (exchangeType.isInstance(exchange)) {
152 return exchangeType.cast(exchange);
153 }
154 }
155 Exchange answer = createExchange();
156 answer.copyFrom(exchange);
157 return answer;
158 }
159
160 /**
161 * Returns the type of the exchange which is generated by this component
162 */
163 @SuppressWarnings("unchecked")
164 public Class<Exchange> getExchangeType() {
165 Type type = getClass().getGenericSuperclass();
166 if (type instanceof ParameterizedType) {
167 ParameterizedType parameterizedType = (ParameterizedType) type;
168 Type[] arguments = parameterizedType.getActualTypeArguments();
169 if (arguments.length > 0) {
170 Type argumentType = arguments[0];
171 if (argumentType instanceof Class) {
172 return (Class<Exchange>) argumentType;
173 }
174 }
175 }
176 return null;
177 }
178
179 public Exchange createExchange() {
180 return createExchange(getExchangePattern());
181 }
182
183 public Exchange createExchange(ExchangePattern pattern) {
184 return new DefaultExchange(this, pattern);
185 }
186
187 public ExchangePattern getExchangePattern() {
188 return exchangePattern;
189 }
190
191 public void setExchangePattern(ExchangePattern exchangePattern) {
192 this.exchangePattern = exchangePattern;
193 }
194
195 protected ScheduledExecutorService createScheduledExecutorService() {
196 return ExecutorServiceHelper.newScheduledThreadPool(DEFAULT_THREADPOOL_SIZE, getEndpointUri(), true);
197 }
198
199 public void configureProperties(Map options) {
200 }
201
202 /**
203 * A factory method to lazily create the endpointUri if none is specified
204 */
205 protected String createEndpointUri() {
206 return null;
207 }
208
209 /**
210 * Sets the endpointUri if it has not been specified yet via some kind of dependency injection mechanism.
211 * This allows dependency injection frameworks such as Spring or Guice to set the default endpoint URI in cases
212 * where it has not been explicitly configured using the name/context in which an Endpoint is created.
213 */
214 public void setEndpointUriIfNotSpecified(String value) {
215 if (endpointUri == null) {
216 setEndpointUri(value);
217 }
218 }
219 protected void setEndpointUri(String endpointUri) {
220 this.endpointUri = endpointUri;
221 }
222
223 public boolean isLenientProperties() {
224 // default should be false for most components
225 return false;
226 }
227
228 }