1 18 package org.columba.core.scripting.interpreter; 19 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Map ; 23 import java.util.logging.Level ; 24 import java.util.logging.Logger ; 25 26 import org.columba.core.scripting.config.BeanshellConfig; 27 import org.columba.core.scripting.model.ColumbaScript; 28 29 30 38 public class InterpreterManager 39 { 40 41 private static final Logger LOG = Logger.getLogger(InterpreterManager.class.getName()); 42 43 47 private final Map <String , ScriptInterpreter> interpreters; 48 49 public InterpreterManager() 50 { 51 52 interpreters = new HashMap <String , ScriptInterpreter>(); 53 54 try 55 { 56 registerInterpreter(new BshInterpreter()); 57 } 58 catch (Exception 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 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 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 89 90 91 for (Iterator it = interpreters.entrySet().iterator(); it.hasNext();) 92 { 93 94 Map.Entry entry = (Map.Entry ) it.next(); 95 96 if (entry.getValue().getClass().isInstance(interpreter)) it.remove(); 97 98 } 99 100 String [] 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 vars = new HashMap (); 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 ex) 134 { 135 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 [] getSupportedExtensions() 145 { 146 return (String []) interpreters.keySet().toArray(new String []{}); 147 } 148 } 149 | Popular Tags |