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.util.ClassLoaderUtil;
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 @Override
037 public Map<String, Object> eval(
038 Set<String> allowedClasses, Map<String, Object> inputObjects,
039 Set<String> outputNames, String script, ClassLoader... classLoaders)
040 throws ScriptingException {
041
042 if (allowedClasses != null) {
043 throw new ExecutionException(
044 "Constrained execution not supported for BeanShell");
045 }
046
047 try {
048 Interpreter interpreter = new Interpreter();
049
050 for (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
051 interpreter.set(entry.getKey(), entry.getValue());
052 }
053
054 if ((classLoaders != null) && (classLoaders.length > 0)) {
055 ClassLoader aggregateClassLoader =
056 AggregateClassLoader.getAggregateClassLoader(
057 ClassLoaderUtil.getPortalClassLoader(), 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 @Override
082 public String getLanguage() {
083 return LANGUAGE;
084 }
085
086 }