View Javadoc
1   /*
2    * Copyright 2009 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *     
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.openehealth.ipf.platform.camel.core.model;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.apache.camel.Processor;
22  import org.apache.camel.model.OutputDefinition;
23  import org.apache.camel.model.RouteDefinition;
24  import org.apache.camel.processor.DelegateProcessor;
25  import org.apache.camel.processor.Pipeline;
26  import org.apache.camel.spi.RouteContext;
27  
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  
31  /**
32   * An {@link OutputDefinition} that combines the {@link Processor} created by
33   * {@link #doCreateDelegate(RouteContext)} and the child processor created by
34   * {@link #createChildProcessor} into a {@link Pipeline}.
35   * This base class supports the implementation of parameterizable DSL extensions
36   * without forcing implementors to create {@link DelegateProcessor} instances.
37   * Instead, plain {@link Processor} instances can be returned by
38   * {@link #doCreateDelegate(RouteContext)} implementations.
39   * 
40   * @author Martin Krasser
41   */
42  @XmlAccessorType(XmlAccessType.FIELD)
43  public abstract class DelegateDefinition extends OutputDefinition<RouteDefinition> {
44  
45      @Override
46      public Processor createProcessor(RouteContext routeContext) throws Exception {
47          Processor delegate = doCreateDelegate(routeContext);
48          Processor next = createChildProcessor(routeContext, false);
49          
50          List<Processor> processors = new ArrayList<>();
51          processors.add(delegate);
52          if (next != null) {
53              processors.add(next);
54          }
55          return Pipeline.newInstance(routeContext.getCamelContext(), processors);
56      }
57  
58      /**
59       * Creates a {@link Processor} for this DSL element.
60       * 
61       * @param routeContext
62       *            the current route context.
63       * @return a {@link Processor} instance.
64       * @throws Exception
65       */
66      protected abstract Processor doCreateDelegate(RouteContext routeContext);
67      
68  }