001    /**
002     * Copyright (c) 2000-2010 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.ExecutionException;
020    import com.liferay.portal.kernel.scripting.ScriptingException;
021    import com.liferay.portal.kernel.scripting.ScriptingExecutor;
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 implements ScriptingExecutor {
031    
032            public static final String LANGUAGE = "beanshell";
033    
034            public void clearCache() {
035            }
036    
037            public String getLanguage() {
038                    return LANGUAGE;
039            }
040    
041            public Map<String, Object> eval(
042                            Set<String> allowedClasses, Map<String, Object> inputObjects,
043                            Set<String> outputNames, String script)
044                    throws ScriptingException {
045    
046                    if (allowedClasses != null) {
047                            throw new ExecutionException(
048                                    "Constrained execution not supported for BeanShell");
049                    }
050    
051                    try {
052                            Interpreter interpreter = new Interpreter();
053    
054                            for (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
055                                    interpreter.set(entry.getKey(), entry.getValue());
056                            }
057    
058                            interpreter.eval(script);
059    
060                            if (outputNames == null) {
061                                    return null;
062                            }
063    
064                            Map<String, Object> outputObjects = new HashMap<String, Object>();
065    
066                            for (String outputName : outputNames) {
067                                    outputObjects.put(outputName, interpreter.get(outputName));
068                            }
069    
070                            return outputObjects;
071                    }
072                    catch (Exception e) {
073                            throw new ScriptingException(e.getMessage(), e);
074                    }
075            }
076    
077    }