001    /**
002     * Copyright (c) 2000-2012 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    
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    }