KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > scripting > interpreter > BshInterpreter


1 /*
2   The contents of this file are subject to the Mozilla Public License Version 1.1
3   (the "License"); you may not use this file except in compliance with the
4   License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
5   
6   Software distributed under the License is distributed on an "AS IS" basis,
7   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8   for the specific language governing rights and
9   limitations under the License.
10
11   The Original Code is "The Columba Project"
12   
13   The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
14   Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
15   
16   All Rights Reserved.
17 */

18 package org.columba.core.scripting.interpreter;
19
20 import java.io.FileNotFoundException JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.columba.core.scripting.model.ColumbaScript;
26
27 import bsh.EvalError;
28 import bsh.Interpreter;
29
30 /**
31     @author Celso Pinto (cpinto@yimports.com)
32  */

33 public class BshInterpreter
34     extends ScriptInterpreter
35 {
36     private final static String JavaDoc[] EXTENSIONS = new String JavaDoc[]{"bsh", "beanshell"};
37
38
39     public String JavaDoc getName()
40     {
41         return "Beanshell Interpreter";
42     }
43
44     public String JavaDoc[] getSupportedExtensions()
45     {
46         return EXTENSIONS;
47     }
48
49     public void execute(ColumbaScript script, Map JavaDoc vars)
50     {
51         /*
52         * it's the script responsability to define the "metadata" by invoking
53         * .setName(), .setAuthor() and .setDescription()
54         */

55
56         logger.append("Executing bsh: " + script.getPath());
57
58         Interpreter bsh = new Interpreter();
59         bsh.setClassLoader(getClass().getClassLoader());
60
61         try
62         {
63             for (Iterator JavaDoc it = vars.entrySet().iterator(); it.hasNext();)
64             {
65                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
66                 bsh.set(entry.getKey().toString(), entry.getValue());
67             }
68
69             bsh.source(script.getPath());
70         }
71         catch (FileNotFoundException JavaDoc ex)
72         {
73             logger.append(String.format("File %s not found", script.getPath()), ex);
74         }
75         catch (IOException JavaDoc ex)
76         {
77             logger.append(String.format("IOException in %s", script.getPath()), ex);
78         }
79         catch (EvalError ex)
80         {
81             logger.append(String.format("Failed to process script %s", script.getPath()), ex);
82         }
83     }
84
85 }
86
Popular Tags