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.ScheduledExecutorService;
023 import java.util.concurrent.ScheduledThreadPoolExecutor;
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
034 /**
035 * A default endpoint useful for implementation inheritance
036 *
037 * @version $Revision: 41314 $
038 */
039 public abstract class DefaultEndpoint<E extends Exchange> implements Endpoint<E>, CamelContextAware {
040 private String endpointUri;
041 private CamelContext camelContext;
042 private Component component;
043 private ScheduledExecutorService executorService;
044 private ExchangePattern exchangePattern = ExchangePattern.InOnly;
045
046 protected DefaultEndpoint(String endpointUri, Component component) {
047 this(endpointUri, component.getCamelContext());
048 this.component = component;
049 }
050
051 protected DefaultEndpoint(String endpointUri, CamelContext camelContext) {
052 this(endpointUri);
053 this.camelContext = camelContext;
054 }
055
056 protected DefaultEndpoint(String endpointUri) {
057 this.endpointUri = endpointUri;
058 }
059
060 public int hashCode() {
061 return endpointUri.hashCode() * 37 + 1;
062 }
063
064 @Override
065 public boolean equals(Object object) {
066 if (object instanceof DefaultEndpoint) {
067 DefaultEndpoint that = (DefaultEndpoint) object;
068 return ObjectHelper.equal(this.endpointUri, that.endpointUri);
069 }
070 return false;
071 }
072
073 @Override
074 public String toString() {
075 return "Endpoint[" + endpointUri + "]";
076 }
077
078 public String getEndpointUri() {
079 return endpointUri;
080 }
081
082 public CamelContext getCamelContext() {
083 return camelContext;
084 }
085
086 public Component getComponent() {
087 return component;
088 }
089
090 public void setCamelContext(CamelContext camelContext) {
091 this.camelContext = camelContext;
092 }
093
094 /**
095 * @return the executor
096 */
097 public synchronized ScheduledExecutorService getExecutorService() {
098 if (executorService == null) {
099 Component c = getComponent();
100 if (c != null && c instanceof DefaultComponent) {
101 DefaultComponent dc = (DefaultComponent) c;
102 executorService = dc.getExecutorService();
103 }
104 if (executorService == null) {
105 executorService = createExecutorService();
106 }
107 }
108 return executorService;
109 }
110
111 /**
112 * @param executorService the executor to set
113 */
114 public synchronized void setExecutorService(ScheduledExecutorService executorService) {
115 this.executorService = executorService;
116 }
117
118 public PollingConsumer<E> createPollingConsumer() throws Exception {
119 return new EventDrivenPollingConsumer<E>(this);
120 }
121
122 /**
123 * Converts the given exchange to the specified exchange type
124 */
125 public E convertTo(Class<E> type, Exchange exchange) {
126 // TODO we could infer type parameter
127 if (type.isInstance(exchange)) {
128 return type.cast(exchange);
129 }
130 return getCamelContext().getExchangeConverter().convertTo(type, exchange);
131 }
132
133 public E createExchange(Exchange exchange) {
134 Class<E> exchangeType = getExchangeType();
135 if (exchangeType != null) {
136 if (exchangeType.isInstance(exchange)) {
137 return exchangeType.cast(exchange);
138 }
139 }
140 E answer = createExchange();
141 answer.copyFrom(exchange);
142 return answer;
143 }
144
145 /**
146 * Returns the type of the exchange which is generated by this component
147 */
148 public Class<E> getExchangeType() {
149 Type type = getClass().getGenericSuperclass();
150 if (type instanceof ParameterizedType) {
151 ParameterizedType parameterizedType = (ParameterizedType) type;
152 Type[] arguments = parameterizedType.getActualTypeArguments();
153 if (arguments.length > 0) {
154 Type argumentType = arguments[0];
155 if (argumentType instanceof Class) {
156 return (Class<E>) argumentType;
157 }
158 }
159 }
160 return null;
161 }
162
163 public E createExchange() {
164 return createExchange(getExchangePattern());
165 }
166
167 public E createExchange(ExchangePattern pattern) {
168 return (E) new DefaultExchange(getCamelContext(), pattern);
169 }
170
171 public ExchangePattern getExchangePattern() {
172 return exchangePattern;
173 }
174
175 public void setExchangePattern(ExchangePattern exchangePattern) {
176 this.exchangePattern = exchangePattern;
177 }
178
179 protected ScheduledThreadPoolExecutor createExecutorService() {
180 return new ScheduledThreadPoolExecutor(10);
181 }
182
183 public void configureProperties(Map options) {
184 }
185 }