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