KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import java.util.logging.Logger JavaDoc;
25
26 import org.columba.core.scripting.config.BeanshellConfig;
27 import org.columba.core.scripting.model.ColumbaScript;
28
29
30 /**
31  This class manages and registers all available script interpreters,
32  which must implement the ScriptInterpreter interface.
33  The registration has to be hardcoded so, if you create an new
34  ScriptInterpreter class, do not forget to add it here.
35
36  @author Celso Pinto (cpinto@yimports.com)
37 */

38 public class InterpreterManager
39 {
40
41     private static final Logger JavaDoc LOG = Logger.getLogger(InterpreterManager.class.getName());
42
43     /**
44      The interpreters Map has the format
45      key(script_extension) => value(ScriptInterpreter instance)
46      */

47     private final Map JavaDoc<String JavaDoc, ScriptInterpreter> interpreters;
48
49     public InterpreterManager()
50     {
51         
52         interpreters = new HashMap JavaDoc<String JavaDoc, ScriptInterpreter>();
53
54         try
55         {
56             registerInterpreter(new BshInterpreter());
57         }
58         catch (Exception JavaDoc e)
59         {
60             LOG.warning("Failed to register the Beanshell script interpreter");
61         }
62         
63         try
64         {
65             registerInterpreter(new JythonInterpreter());
66         }
67         catch (Exception JavaDoc e)
68         {
69             LOG.warning("Failed to register the Jython script interpreter");
70         }
71         
72         try
73         {
74             registerInterpreter(new GroovyInterpreter());
75         }
76         catch (Exception JavaDoc e)
77         {
78             LOG.warning("Failed to register the Groovy script interpreter");
79         }
80         
81     }
82
83     public void registerInterpreter(ScriptInterpreter interpreter)
84     {
85
86         LOG.entering("registerInterpreter", interpreter.getName());
87
88         /* find out if the interpreter is already registered */
89         /* if so, remove it */
90         /*XXX this is not thread-safe, though luck. It isn't supposed to be. */
91         for (Iterator JavaDoc it = interpreters.entrySet().iterator(); it.hasNext();)
92         {
93
94             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
95
96             if (entry.getValue().getClass().isInstance(interpreter)) it.remove();
97
98         }
99
100         String JavaDoc[] extensions = interpreter.getSupportedExtensions();
101         for (int i = 0; i < extensions.length; i++)
102         {
103             ScriptInterpreter previous = interpreters.put(extensions[i], interpreter);
104             if (previous != null)
105                 LOG.warning(previous.getClass().getName() + " doesn't handle " + extensions[i] + " anymore");
106         }
107
108         LOG.exiting("registerInterpreter", interpreter.getName());
109
110     }
111
112     public void executeScript(ColumbaScript script)
113     {
114         LOG.entering("executeScript", script.getPath());
115
116         ScriptInterpreter interpreter = interpreters.get(script.getExtension());
117
118         if (interpreter == null)
119         {
120             LOG.warning("No interpreter found for " + script.getPath());
121             return;
122         }
123
124         Map JavaDoc vars = new HashMap JavaDoc();
125         vars.put(ScriptInterpreter.SCRIPT_PATH, BeanshellConfig.getInstance().getPath().getPath());
126
127         vars.put(ScriptInterpreter.SCRIPT_OBJ, script);
128
129         try
130         {
131             interpreter.execute(script, vars);
132         }
133         catch (Exception JavaDoc ex)
134         {
135             /* any errors that may occur cannot stop other scripts from running */
136             LOG.log(Level.SEVERE, "Exception caught in script:" + script.getPath(), ex);
137             ex.printStackTrace();
138
139         }
140
141         LOG.exiting("executeScript", script.getPath());
142     }
143
144     public String JavaDoc[] getSupportedExtensions()
145     {
146         return (String JavaDoc[]) interpreters.keySet().toArray(new String JavaDoc[]{});
147     }
148 }
149
Popular Tags