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;
019
020 import java.util.HashMap;
021 import java.util.Map;
022 import java.util.concurrent.Callable;
023
024 import org.apache.camel.impl.DefaultEndpointResolver;
025 import org.apache.camel.impl.DefaultExchangeConverter;
026
027 /**
028 * Represents the container used to configure routes and the policies to use.
029 *
030 * @version $Revision: 520124 $
031 */
032 public class CamelContainer<E> {
033
034 private EndpointResolver<E> endpointResolver;
035 private ExchangeConverter exchangeConverter;
036 private Map<String, Component> components = new HashMap<String, Component>();
037
038 public EndpointResolver<E> getEndpointResolver() {
039 if (endpointResolver == null) {
040 endpointResolver = createEndpointResolver();
041 }
042 return endpointResolver;
043 }
044
045 public void setEndpointResolver(EndpointResolver<E> endpointResolver) {
046 this.endpointResolver = endpointResolver;
047 }
048
049 public ExchangeConverter getExchangeConverter() {
050 if (exchangeConverter == null) {
051 exchangeConverter = createExchangeConverter();
052 }
053 return exchangeConverter;
054 }
055
056 public void setExchangeConverter(ExchangeConverter exchangeConverter) {
057 this.exchangeConverter = exchangeConverter;
058 }
059
060 // Implementation methods
061 //-----------------------------------------------------------------------
062 protected EndpointResolver<E> createEndpointResolver() {
063 return new DefaultEndpointResolver<E>();
064 }
065
066 /**
067 * Lazily create a default exchange converter implementation
068 */
069 protected ExchangeConverter createExchangeConverter() {
070 return new DefaultExchangeConverter();
071 }
072
073 public Component getOrCreateComponent(String componentName, Callable<Component<E,? extends Endpoint<E>>> factory) {
074 synchronized(components) {
075 Component component = components.get(componentName);
076 if( component == null ) {
077 try {
078 component = factory.call();
079 if( component == null )
080 throw new IllegalArgumentException("Factory failed to create the "+componentName+" component, it returned null.");
081 } catch (Exception e) {
082 throw new IllegalArgumentException("Factory failed to create the "+componentName+" component", e);
083 }
084 }
085 return component;
086 }
087 }
088
089 public Component getComponent(String componentName) {
090 synchronized(components) {
091 Component component = components.get(componentName);
092 return component;
093 }
094 }
095 }