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 import com.liferay.portal.kernel.util.AggregateClassLoader;
023 import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
024
025 import java.util.HashMap;
026 import java.util.Map;
027 import java.util.Set;
028
029
032 public class BeanShellExecutor extends BaseScriptingExecutor {
033
034 public static final String LANGUAGE = "beanshell";
035
036 public Map<String, Object> eval(
037 Set<String> allowedClasses, Map<String, Object> inputObjects,
038 Set<String> outputNames, String script, ClassLoader... classLoaders)
039 throws ScriptingException {
040
041 if (allowedClasses != null) {
042 throw new ExecutionException(
043 "Constrained execution not supported for BeanShell");
044 }
045
046 try {
047 Interpreter interpreter = new Interpreter();
048
049 for (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
050 interpreter.set(entry.getKey(), entry.getValue());
051 }
052
053 if ((classLoaders != null) && (classLoaders.length > 0)) {
054 ClassLoader aggregateClassLoader =
055 AggregateClassLoader.getAggregateClassLoader(
056 PACLClassLoaderUtil.getPortalClassLoader(),
057 classLoaders);
058
059 interpreter.setClassLoader(aggregateClassLoader);
060 }
061
062 interpreter.eval(script);
063
064 if (outputNames == null) {
065 return null;
066 }
067
068 Map<String, Object> outputObjects = new HashMap<String, Object>();
069
070 for (String outputName : outputNames) {
071 outputObjects.put(outputName, interpreter.get(outputName));
072 }
073
074 return outputObjects;
075 }
076 catch (Exception e) {
077 throw new ScriptingException(e.getMessage(), e);
078 }
079 }
080
081 public String getLanguage() {
082 return LANGUAGE;
083 }
084
085 }