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.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.BaseScriptingExecutor;
023    import com.liferay.portal.kernel.scripting.ScriptingException;
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 extends BaseScriptingExecutor {
033    
034            @Override
035            public void clearCache() {
036                    SingleVMPoolUtil.clear(_CACHE_NAME);
037            }
038    
039            public Map<String, Object> eval(
040                            Set<String> allowedClasses, Map<String, Object> inputObjects,
041                            Set<String> outputNames, String script)
042                    throws ScriptingException {
043    
044                    Script compiledScript = getCompiledScript(script);
045    
046                    try {
047                            Context context = Context.enter();
048    
049                            Scriptable scriptable = context.initStandardObjects();
050    
051                            for (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
052                                    String key = entry.getKey();
053                                    Object value = entry.getValue();
054    
055                                    ScriptableObject.putProperty(
056                                            scriptable, key, Context.javaToJS(value, scriptable));
057                            }
058    
059                            if (allowedClasses != null) {
060                                    context.setClassShutter(
061                                            new JavaScriptClassVisibilityChecker(allowedClasses));
062                            }
063    
064                            compiledScript.exec(context, scriptable);
065    
066                            if (outputNames == null) {
067                                    return null;
068                            }
069    
070                            Map<String, Object> outputObjects = new HashMap<String, Object>();
071    
072                            for (String outputName : outputNames) {
073                                    outputObjects.put(
074                                            outputName,
075                                            ScriptableObject.getProperty(scriptable, outputName));
076                            }
077    
078                            return outputObjects;
079                    }
080                    catch (Exception e) {
081                            throw new ScriptingException(e.getMessage() + "\n\n", e);
082                    }
083                    finally {
084                            Context.exit();
085                    }
086            }
087    
088            public String getLanguage() {
089                    return _LANGUAGE;
090            }
091    
092            protected Script getCompiledScript(String script) {
093                    String key = String.valueOf(script.hashCode());
094    
095                    Script compiledScript = (Script)SingleVMPoolUtil.get(_CACHE_NAME, key);
096    
097                    if (compiledScript == null) {
098                            try {
099                                    Context context = Context.enter();
100    
101                                    compiledScript = context.compileString(
102                                            script, "script", 0, null);
103                            }
104                            finally {
105                                    Context.exit();
106                            }
107    
108                            SingleVMPoolUtil.put(_CACHE_NAME, key, compiledScript);
109                    }
110    
111                    return compiledScript;
112            }
113    
114            private static final String _CACHE_NAME =
115                    JavaScriptExecutor.class.getName();
116    
117            private static final String _LANGUAGE = "javascript";
118    
119    }