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 */
017package org.apache.camel.impl;
018
019import org.apache.camel.NoFactoryAvailableException;
020import org.apache.camel.Processor;
021import org.apache.camel.model.ProcessorDefinition;
022import org.apache.camel.spi.FactoryFinder;
023import org.apache.camel.spi.ProcessorFactory;
024import org.apache.camel.spi.RouteContext;
025
026/**
027 * Default {@link ProcessorFactory} that supports using 3rd party Camel components to implement the EIP {@link Processor}.
028 * <p/>
029 * The component should use the {@link FactoryFinder} SPI to specify a file with the name of the EIP model in the
030 * directory of {@link #RESOURCE_PATH}. The file should contain a property with key <tt>class</tt> that refers
031 * to the name of the {@link ProcessorFactory} the Camel component implement, which gets called for creating
032 * the {@link Processor}s for the EIP.
033 * <p/>
034 * The Hystrix EIP is such an example where {@link org.apache.camel.model.HystrixDefinition} is implemented
035 * in the <tt>camel-hystrix</tt> component.
036 */
037public class DefaultProcessorFactory implements ProcessorFactory {
038
039    public static final String RESOURCE_PATH = "META-INF/services/org/apache/camel/model/";
040
041    @Override
042    public Processor createChildProcessor(RouteContext routeContext, ProcessorDefinition<?> definition, boolean mandatory) throws Exception {
043        String name = definition.getClass().getSimpleName();
044        FactoryFinder finder = routeContext.getCamelContext().getFactoryFinder(RESOURCE_PATH);
045        try {
046            if (finder != null) {
047                Object object = finder.newInstance(name);
048                if (object != null && object instanceof ProcessorFactory) {
049                    ProcessorFactory pc = (ProcessorFactory) object;
050                    return pc.createChildProcessor(routeContext, definition, mandatory);
051                }
052            }
053        } catch (NoFactoryAvailableException e) {
054            // ignore there is no custom factory
055        }
056
057        return null;
058    }
059
060    @Override
061    public Processor createProcessor(RouteContext routeContext, ProcessorDefinition<?> definition) throws Exception {
062        String name = definition.getClass().getSimpleName();
063        FactoryFinder finder = routeContext.getCamelContext().getFactoryFinder(RESOURCE_PATH);
064        try {
065            if (finder != null) {
066                Object object = finder.newInstance(name);
067                if (object != null && object instanceof ProcessorFactory) {
068                    ProcessorFactory pc = (ProcessorFactory) object;
069                    return pc.createProcessor(routeContext, definition);
070                }
071            }
072        } catch (NoFactoryAvailableException e) {
073            // ignore there is no custom factory
074        }
075
076        return null;
077    }
078}