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