View Javadoc
1   /*
2    * Copyright 2008 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 groovy.lang.Closure;
19  import groovy.transform.stc.ClosureParams;
20  import groovy.transform.stc.SimpleType;
21  import org.apache.camel.Expression;
22  import org.apache.camel.Processor;
23  import org.apache.camel.spi.RouteContext;
24  import org.openehealth.ipf.platform.camel.core.adapter.ProcessorAdapter;
25  import org.openehealth.ipf.platform.camel.core.adapter.StaticParams;
26  import org.openehealth.ipf.platform.camel.core.closures.DelegatingExpression;
27  
28  import javax.xml.bind.annotation.XmlAccessType;
29  import javax.xml.bind.annotation.XmlAccessorType;
30  import javax.xml.bind.annotation.XmlTransient;
31  
32  /**
33   * @author Martin Krasser
34   */
35  @XmlAccessorType(XmlAccessType.FIELD)
36  public abstract class ProcessorAdapterDefinition extends DelegateDefinition {
37  
38      @XmlTransient
39      private Expression inputExpression;
40      @XmlTransient
41      private Expression paramsExpression;
42      
43      /**
44       * Defines the input to the adapter via the given expression 
45       * @param inputExpression
46       *          the expression logic
47       */
48      public ProcessorAdapterDefinition input(Expression inputExpression) {
49          this.inputExpression = inputExpression;
50          return this;
51      }
52      
53      /**
54       * Defines the input to the adapter via the closure expression 
55       * @param inputExpression
56       *          a closure implementing the expression logic
57       */
58      public ProcessorAdapterDefinition input(@ClosureParams(value = SimpleType.class, options = { "org.apache.camel.Expression"})
59                                                      Closure inputExpression) {
60          this.inputExpression = new DelegatingExpression(inputExpression);
61          return this;
62      }
63      
64      /**
65       * Defines the parameters for the adapter via the given expression 
66       * @param paramsExpression
67       *          the expression logic
68       */
69      public ProcessorAdapterDefinition params(Expression paramsExpression) {
70          this.paramsExpression = paramsExpression;
71          return this;
72      }
73      
74      /**
75       * Defines the parameters for the adapter via the closure expression 
76       * @param paramsExpression
77       *          a closure implementing the expression logic
78       */
79      public ProcessorAdapterDefinition params(@ClosureParams(value = SimpleType.class, options = { "org.apache.camel.Expression"})
80                                                       Closure paramsExpression) {
81          this.paramsExpression = new DelegatingExpression(paramsExpression);
82          return this;
83      }
84      
85      /**
86       * Defines the static parameters for the adapter 
87       * @param params
88       *          the parameters
89       */
90      public ProcessorAdapterDefinition staticParams(Object... params) {
91          paramsExpression = new StaticParams(params);
92          return this;
93      }
94      
95      public ParamsDefinition params() {
96          return new ParamsDefinition(this);
97      }
98      
99      @Override
100     protected Processor doCreateDelegate(RouteContext routeContext) {
101         ProcessorAdapter adapter = doCreateProcessor(routeContext);
102         if (inputExpression != null) {
103             adapter.input(inputExpression);
104         }
105         if (paramsExpression != null) {
106             adapter.params(paramsExpression);
107         }
108         return adapter;
109     }
110 
111     protected abstract ProcessorAdapter doCreateProcessor(RouteContext routeContext);
112     
113 }