001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
030     * @author Shuyang Zhou
031     */
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                                                    ClassLoaderUtil.getPortalClassLoader(), classLoaders);
057    
058                                    interpreter.setClassLoader(aggregateClassLoader);
059                            }
060    
061                            interpreter.eval(script);
062    
063                            if (outputNames == null) {
064                                    return null;
065                            }
066    
067                            Map<String, Object> outputObjects = new HashMap<String, Object>();
068    
069                            for (String outputName : outputNames) {
070                                    outputObjects.put(outputName, interpreter.get(outputName));
071                            }
072    
073                            return outputObjects;
074                    }
075                    catch (Exception e) {
076                            throw new ScriptingException(e.getMessage(), e);
077                    }
078            }
079    
080            public String getLanguage() {
081                    return LANGUAGE;
082            }
083    
084    }