001
014
015 package com.liferay.portal.scripting.beanshell;
016
017 import bsh.Interpreter;
018
019 import com.liferay.portal.kernel.scripting.BaseScriptingExecutor;
020 import com.liferay.portal.kernel.scripting.ExecutionException;
021 import com.liferay.portal.kernel.scripting.ScriptingException;
022
023 import java.util.HashMap;
024 import java.util.Map;
025 import java.util.Set;
026
027
030 public class BeanShellExecutor extends BaseScriptingExecutor {
031
032 public static final String LANGUAGE = "beanshell";
033
034 public Map<String, Object> eval(
035 Set<String> allowedClasses, Map<String, Object> inputObjects,
036 Set<String> outputNames, String script)
037 throws ScriptingException {
038
039 if (allowedClasses != null) {
040 throw new ExecutionException(
041 "Constrained execution not supported for BeanShell");
042 }
043
044 try {
045 Interpreter interpreter = new Interpreter();
046
047 for (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
048 interpreter.set(entry.getKey(), entry.getValue());
049 }
050
051 interpreter.eval(script);
052
053 if (outputNames == null) {
054 return null;
055 }
056
057 Map<String, Object> outputObjects = new HashMap<String, Object>();
058
059 for (String outputName : outputNames) {
060 outputObjects.put(outputName, interpreter.get(outputName));
061 }
062
063 return outputObjects;
064 }
065 catch (Exception e) {
066 throw new ScriptingException(e.getMessage(), e);
067 }
068 }
069
070 public String getLanguage() {
071 return LANGUAGE;
072 }
073
074 }