1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.commons.jelly.tags.bsf;
17
18 import org.apache.bsf.BSFEngine;
19 import org.apache.bsf.BSFManager;
20
21 import java.util.Iterator;
22
23 import org.apache.commons.jelly.JellyContext;
24 import org.apache.commons.jelly.expression.ExpressionSupport;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28
29
30 /*** Represents a BSF expression
31 *
32 * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
33 * @version $Revision: 155420 $
34 */
35 public class BSFExpression extends ExpressionSupport {
36
37 /*** The Log to which logging calls will be made. */
38 private static final Log log = LogFactory.getLog( BSFExpression.class );
39
40 /*** The expression */
41 private String text;
42
43 /*** The BSF Engine to evaluate expressions */
44 private BSFEngine engine;
45 /*** The BSF Manager to evaluate expressions */
46 private BSFManager manager;
47
48 /*** The adapter to BSF's ObjectRegistry that uses the JellyContext */
49 private JellyContextRegistry registry;
50
51 public BSFExpression(String text, BSFEngine engine, BSFManager manager, JellyContextRegistry registry) {
52 this.text = text;
53 this.engine = engine;
54 this.manager = manager;
55 this.registry = registry;
56 }
57
58
59
60 public String getExpressionText() {
61 return "${" + text + "}";
62 }
63
64 public Object evaluate(JellyContext context) {
65
66
67
68
69 synchronized (registry) {
70 registry.setJellyContext(context);
71
72 try {
73
74 for ( Iterator iter = context.getVariableNames(); iter.hasNext(); ) {
75 String name = (String) iter.next();
76 Object value = context.getVariable( name );
77 manager.declareBean( name, value, value.getClass() );
78 }
79 return engine.eval( text, -1, -1, text );
80 }
81 catch (Exception e) {
82 log.warn( "Caught exception evaluating: " + text + ". Reason: " + e, e );
83 return null;
84 }
85 }
86 }
87 }