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.closures;
17  
18  import groovy.lang.Closure;
19  
20  import org.codehaus.groovy.runtime.InvokerInvocationException;
21  
22  /**
23   * Base class for concrete closure adapters.
24   * 
25   * @author Martin Krasser
26   */
27  public abstract class ClosureAdapter {
28      
29      private final Closure closure;
30      
31      public ClosureAdapter(Closure closure) {
32          this.closure = closure;
33      }
34      
35      public Closure getClosure() {
36          return closure;
37      }
38      
39      public Object call(Object... args) {
40          return callClosure(closure, args);
41      }
42  
43      public static Object callClosure(Closure closure, Object... args) {
44          try {
45              return closure.call(args);
46          } catch (InvokerInvocationException e) {
47              if (e.getCause() instanceof RuntimeException) {
48                  throw (RuntimeException)e.getCause();
49              } else {
50                  throw e;
51              }
52          }
53      }
54      
55  }