View Javadoc

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.commons.jelly.JellyException;
19  import org.apache.commons.jelly.expression.Expression;
20  import org.apache.commons.jelly.expression.ExpressionFactory;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import org.apache.bsf.BSFEngine;
25  import org.apache.bsf.BSFException;
26  import org.apache.bsf.BSFManager;
27  
28  /*** Represents a factory of BSF expressions
29    *
30    * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
31    * @version $Revision: 155420 $
32    */
33  public class BSFExpressionFactory implements ExpressionFactory {
34  
35      /*** The logger of messages */
36      private Log log = LogFactory.getLog( getClass() );
37  
38      private String language = "javascript";
39      private BSFManager manager;
40      private BSFEngine engine;
41      private JellyContextRegistry registry = new JellyContextRegistry();
42  
43      public BSFExpressionFactory() {
44      }
45  
46      // Properties
47      //-------------------------------------------------------------------------
48  
49      /*** @return the BSF language to be used */
50      public String getLanguage() {
51          return language;
52      }
53  
54      public void setLanguage(String language) {
55          this.language = language;
56      }
57  
58      /*** @return the BSF Engine to be used by this expression factory */
59      public BSFEngine getBSFEngine() throws BSFException {
60          if ( engine == null ) {
61              engine = createBSFEngine();
62          }
63          return engine;
64      }
65  
66      public void setBSFEngine(BSFEngine engine) {
67          this.engine = engine;
68      }
69  
70      public BSFManager getBSFManager() {
71          if ( manager == null ) {
72              manager = createBSFManager();
73              manager.setObjectRegistry( registry );
74          }
75          return manager;
76      }
77  
78      public void setBSFManager(BSFManager manager) {
79          this.manager = manager;
80          manager.setObjectRegistry( registry );
81      }
82  
83      // ExpressionFactory interface
84      //-------------------------------------------------------------------------
85      public Expression createExpression(String text) throws JellyException {
86          try {
87              return new BSFExpression( text, getBSFEngine(), getBSFManager(), registry );
88          } catch (BSFException e) {
89              throw new JellyException("Could not obtain BSF engine",e);
90          }
91      }
92  
93      // Implementation methods
94      //-------------------------------------------------------------------------
95  
96      /*** Factory method */
97      protected BSFEngine createBSFEngine() throws BSFException {
98          return getBSFManager().loadScriptingEngine( getLanguage() );
99      }
100 
101     /*** Factory method */
102     protected BSFManager createBSFManager() {
103         BSFManager answer = new BSFManager();
104         return answer;
105     }
106 }