|
1 | | -/* |
2 | | - * Copyright 2002-2019 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 | | - * https://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 | | - |
17 | | -package org.springframework.integration.groovy; |
18 | | - |
19 | | -import java.util.Map; |
20 | | - |
21 | | -import org.springframework.scripting.groovy.GroovyObjectCustomizer; |
22 | | -import org.springframework.util.Assert; |
23 | | - |
24 | | -import groovy.lang.Binding; |
25 | | -import groovy.lang.GroovyObject; |
26 | | -import groovy.lang.Script; |
27 | | - |
28 | | -/** |
29 | | - * @author Dave Syer |
30 | | - * @author Oleg Zhurakousky |
31 | | - * @author Mark Fisher |
32 | | - * @since 2.0 |
33 | | - */ |
34 | | -class VariableBindingGroovyObjectCustomizerDecorator implements GroovyObjectCustomizer { |
35 | | - |
36 | | - private volatile Map<String, ?> variables; |
37 | | - |
38 | | - private volatile GroovyObjectCustomizer customizer; |
39 | | - |
40 | | - |
41 | | - public void setVariables(Map<String, ?> variables) { |
42 | | - this.variables = variables; |
43 | | - } |
44 | | - |
45 | | - public void setCustomizer(GroovyObjectCustomizer customizer) { |
46 | | - this.customizer = customizer; |
47 | | - } |
48 | | - |
49 | | - public void customize(GroovyObject goo) { |
50 | | - Assert.state(goo instanceof Script, "Expected a Script"); |
51 | | - if (this.variables != null) { |
52 | | - Binding binding = ((Script) goo).getBinding(); |
53 | | - for (Map.Entry<String, ?> entry : this.variables.entrySet()) { |
54 | | - binding.setVariable(entry.getKey(), entry.getValue()); |
55 | | - } |
56 | | - } |
57 | | - if (this.customizer != null) { |
58 | | - this.customizer.customize(goo); |
59 | | - } |
60 | | - } |
61 | | - |
62 | | -} |
| 1 | +/* |
| 2 | + * Copyright 2002-2019 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 | + * https://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 | + |
| 17 | +package org.springframework.integration.groovy; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.springframework.scripting.groovy.GroovyObjectCustomizer; |
| 22 | +import org.springframework.util.Assert; |
| 23 | + |
| 24 | +import groovy.lang.Binding; |
| 25 | +import groovy.lang.GroovyObject; |
| 26 | +import groovy.lang.Script; |
| 27 | + |
| 28 | +/** |
| 29 | + * @author Dave Syer |
| 30 | + * @author Oleg Zhurakousky |
| 31 | + * @author Mark Fisher |
| 32 | + * @since 2.0 |
| 33 | + */ |
| 34 | +class VariableBindingGroovyObjectCustomizerDecorator implements GroovyObjectCustomizer { |
| 35 | + |
| 36 | + private volatile Map<String, ?> variables; |
| 37 | + |
| 38 | + private volatile GroovyObjectCustomizer customizer; |
| 39 | + |
| 40 | + |
| 41 | + public void setVariables(Map<String, ?> variables) { |
| 42 | + this.variables = variables; |
| 43 | + } |
| 44 | + |
| 45 | + public void setCustomizer(GroovyObjectCustomizer customizer) { |
| 46 | + this.customizer = customizer; |
| 47 | + } |
| 48 | + |
| 49 | + public void customize(GroovyObject goo) { |
| 50 | + Assert.state(goo instanceof Script, "Expected a Script"); |
| 51 | + if (this.variables != null) { |
| 52 | + Binding binding = ((Script) goo).getBinding(); |
| 53 | + for (Map.Entry<String, ?> entry : this.variables.entrySet()) { |
| 54 | + binding.setVariable(entry.getKey(), entry.getValue()); |
| 55 | + } |
| 56 | + } |
| 57 | + if (this.customizer != null) { |
| 58 | + this.customizer.customize(goo); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | +} |
0 commit comments