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