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.javascript;
016    
017    import com.liferay.mozilla.javascript.Context;
018    import com.liferay.mozilla.javascript.Script;
019    import com.liferay.mozilla.javascript.Scriptable;
020    import com.liferay.mozilla.javascript.ScriptableObject;
021    import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
022    import com.liferay.portal.kernel.scripting.ScriptingException;
023    import com.liferay.portal.kernel.scripting.ScriptingExecutor;
024    
025    import java.util.HashMap;
026    import java.util.Map;
027    import java.util.Set;
028    
029    /**
030     * @author Alberto Montero
031     */
032    public class JavaScriptExecutor implements ScriptingExecutor {
033    
034            public static final String CACHE_NAME = JavaScriptExecutor.class.getName();
035    
036            public static final String LANGUAGE = "javascript";
037    
038            public void clearCache() {
039                    SingleVMPoolUtil.clear(CACHE_NAME);
040            }
041    
042            public String getLanguage() {
043                    return LANGUAGE;
044            }
045    
046            public Map<String, Object> eval(
047                            Set<String> allowedClasses, Map<String, Object> inputObjects,
048                            Set<String> outputNames, String script)
049                    throws ScriptingException {
050    
051                    Script compiledScript = getCompiledScript(script);
052    
053                    try {
054                            Context context = Context.enter();
055    
056                            Scriptable scriptable = context.initStandardObjects();
057    
058                            for (String varName: inputObjects.keySet()) {
059                                    ScriptableObject.putProperty(
060                                            scriptable, varName,
061                                            Context.javaToJS(inputObjects.get(varName), scriptable));
062                            }
063    
064                            if (allowedClasses != null) {
065                                    context.setClassShutter(
066                                            new JavaScriptClassVisibilityChecker(allowedClasses));
067                            }
068    
069                            compiledScript.exec(context, scriptable);
070    
071                            if (outputNames == null) {
072                                    return null;
073                            }
074    
075                            Map<String, Object> outputObjects = new HashMap<String, Object>();
076    
077                            for (String outputName : outputNames) {
078                                    outputObjects.put(
079                                            outputName,
080                                            ScriptableObject.getProperty(scriptable, outputName));
081                            }
082    
083                            return outputObjects;
084                    }
085                    catch (Exception e) {
086                            throw new ScriptingException(e.getMessage() + "\n\n", e);
087                    }
088                    finally {
089                            Context.exit();
090                    }
091            }
092    
093            protected Script getCompiledScript(String script) {
094                    String key = String.valueOf(script.hashCode());
095    
096                    Script compiledScript = (Script)SingleVMPoolUtil.get(CACHE_NAME, key);
097    
098                    if (compiledScript == null) {
099                            try {
100                                    Context context = Context.enter();
101    
102                                    compiledScript = context.compileString(
103                                            script, "script", 0, null);
104                            }
105                            finally {
106                                    Context.exit();
107                            }
108    
109                            SingleVMPoolUtil.put(CACHE_NAME, key, compiledScript);
110                    }
111    
112                    return compiledScript;
113            }
114    
115    }