001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.Set;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class BeanShellExecutor extends BaseScriptingExecutor {
031    
032            public static final String LANGUAGE = "beanshell";
033    
034            public Map<String, Object> eval(
035                            Set<String> allowedClasses, Map<String, Object> inputObjects,
036                            Set<String> outputNames, String script)
037                    throws ScriptingException {
038    
039                    if (allowedClasses != null) {
040                            throw new ExecutionException(
041                                    "Constrained execution not supported for BeanShell");
042                    }
043    
044                    try {
045                            Interpreter interpreter = new Interpreter();
046    
047                            for (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
048                                    interpreter.set(entry.getKey(), entry.getValue());
049                            }
050    
051                            interpreter.eval(script);
052    
053                            if (outputNames == null) {
054                                    return null;
055                            }
056    
057                            Map<String, Object> outputObjects = new HashMap<String, Object>();
058    
059                            for (String outputName : outputNames) {
060                                    outputObjects.put(outputName, interpreter.get(outputName));
061                            }
062    
063                            return outputObjects;
064                    }
065                    catch (Exception e) {
066                            throw new ScriptingException(e.getMessage(), e);
067                    }
068            }
069    
070            public String getLanguage() {
071                    return LANGUAGE;
072            }
073    
074    }