001 /** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one or more 004 * contributor license agreements. See the NOTICE file distributed with 005 * this work for additional information regarding copyright ownership. 006 * The ASF licenses this file to You under the Apache License, Version 2.0 007 * (the "License"); you may not use this file except in compliance with 008 * the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018 package org.apache.camel.impl; 019 020 import org.apache.camel.CamelContext; 021 import org.apache.camel.Component; 022 import org.apache.camel.Endpoint; 023 import org.apache.camel.Exchange; 024 import org.apache.camel.util.IntrospectionSupport; 025 import org.apache.camel.util.URISupport; 026 import org.apache.camel.util.ObjectHelper; 027 028 import java.net.URI; 029 import java.util.Map; 030 import java.util.concurrent.ScheduledExecutorService; 031 import java.util.concurrent.ScheduledThreadPoolExecutor; 032 import java.util.concurrent.ThreadFactory; 033 034 /** 035 * @version $Revision: 541335 $ 036 */ 037 public abstract class DefaultComponent<E extends Exchange> extends ServiceSupport implements Component<E> { 038 039 private int defaultThreadPoolSize = 5; 040 private CamelContext camelContext; 041 private ScheduledExecutorService executorService; 042 043 public DefaultComponent() { 044 } 045 046 public DefaultComponent(CamelContext context) { 047 this.camelContext = context; 048 } 049 050 051 public Endpoint<E> createEndpoint(String uri) throws Exception { 052 ObjectHelper.notNull(getCamelContext(), "camelContext"); 053 URI u = new URI(uri); 054 String path = u.getHost(); 055 if (path == null) { 056 path = u.getSchemeSpecificPart(); 057 } 058 Map parameters = URISupport.parseParamters(u); 059 060 Endpoint<E> endpoint = createEndpoint(uri, path, parameters); 061 if (endpoint == null) { 062 return null; 063 } 064 if (parameters != null) { 065 if (endpoint instanceof ScheduledPollEndpoint) { 066 ScheduledPollEndpoint scheduledPollEndpoint = (ScheduledPollEndpoint) endpoint; 067 scheduledPollEndpoint.configureProperties(parameters); 068 } 069 IntrospectionSupport.setProperties(endpoint, parameters); 070 } 071 return endpoint; 072 } 073 074 public CamelContext getCamelContext() { 075 return camelContext; 076 } 077 078 public void setCamelContext(CamelContext context) { 079 this.camelContext = context; 080 } 081 082 public ScheduledExecutorService getExecutorService() { 083 if (executorService == null) { 084 executorService = createExecutorService(); 085 } 086 return executorService; 087 } 088 089 public void setExecutorService(ScheduledExecutorService executorService) { 090 this.executorService = executorService; 091 } 092 093 /** 094 * A factory method to create a default thread pool and executor 095 */ 096 protected ScheduledExecutorService createExecutorService() { 097 return new ScheduledThreadPoolExecutor(defaultThreadPoolSize, new ThreadFactory() { 098 int counter; 099 100 public synchronized Thread newThread(Runnable runnable) { 101 Thread thread = new Thread(runnable); 102 thread.setName("Thread" + (++counter) + " " + DefaultComponent.this.toString()); 103 return thread; 104 } 105 }); 106 } 107 108 protected void doStart() throws Exception { 109 } 110 111 protected void doStop() throws Exception { 112 if (executorService != null) { 113 executorService.shutdown(); 114 } 115 } 116 117 118 /** 119 * A factory method allowing derived components to create a new endpoint from the given URI, 120 * remaining path and optional parameters 121 * 122 * @param uri the full URI of the endpoint 123 * @param remaining the remaining part of the URI without the query parameters or component prefix 124 * @param parameters the optional parameters passed in 125 * @return a newly created endpoint or null if the endpoint cannot be created based on the inputs 126 */ 127 abstract protected Endpoint<E> createEndpoint(String uri, String remaining, Map parameters) throws Exception; 128 }