%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.bsf.BSFExpression |
|
|
1 | /* |
|
2 | * Copyright 2002,2004 The Apache Software Foundation. |
|
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.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: 1.5 $ |
|
34 | */ |
|
35 | 0 | public class BSFExpression extends ExpressionSupport { |
36 | ||
37 | /** The Log to which logging calls will be made. */ |
|
38 | 0 | 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 | 0 | public BSFExpression(String text, BSFEngine engine, BSFManager manager, JellyContextRegistry registry) { |
52 | 0 | this.text = text; |
53 | 0 | this.engine = engine; |
54 | 0 | this.manager = manager; |
55 | 0 | this.registry = registry; |
56 | 0 | } |
57 | ||
58 | // Expression interface |
|
59 | //------------------------------------------------------------------------- |
|
60 | public String getExpressionText() { |
|
61 | 0 | return "${" + text + "}"; |
62 | } |
|
63 | ||
64 | public Object evaluate(JellyContext context) { |
|
65 | // XXXX: unfortunately we must sychronize evaluations |
|
66 | // so that we can swizzle in the context. |
|
67 | // maybe we could create an expression from a context |
|
68 | // (and so create a BSFManager for a context) |
|
69 | 0 | synchronized (registry) { |
70 | 0 | registry.setJellyContext(context); |
71 | ||
72 | try { |
|
73 | // XXXX: hack - there must be a better way!!! |
|
74 | 0 | for ( Iterator iter = context.getVariableNames(); iter.hasNext(); ) { |
75 | 0 | String name = (String) iter.next(); |
76 | 0 | Object value = context.getVariable( name ); |
77 | 0 | manager.declareBean( name, value, value.getClass() ); |
78 | } |
|
79 | 0 | return engine.eval( text, -1, -1, text ); |
80 | } |
|
81 | catch (Exception e) { |
|
82 | 0 | log.warn( "Caught exception evaluating: " + text + ". Reason: " + e, e ); |
83 | 0 | return null; |
84 | } |
|
85 | 0 | } |
86 | } |
|
87 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |