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 (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
059                                    String key = entry.getKey();
060                                    Object value = entry.getValue();
061    
062                                    ScriptableObject.putProperty(
063                                            scriptable, key, Context.javaToJS(value, scriptable));
064                            }
065    
066                            if (allowedClasses != null) {
067                                    context.setClassShutter(
068                                            new JavaScriptClassVisibilityChecker(allowedClasses));
069                            }
070    
071                            compiledScript.exec(context, scriptable);
072    
073                            if (outputNames == null) {
074                                    return null;
075                            }
076    
077                            Map<String, Object> outputObjects = new HashMap<String, Object>();
078    
079                            for (String outputName : outputNames) {
080                                    outputObjects.put(
081                                            outputName,
082                                            ScriptableObject.getProperty(scriptable, outputName));
083                            }
084    
085                            return outputObjects;
086                    }
087                    catch (Exception e) {
088                            throw new ScriptingException(e.getMessage() + "\n\n", e);
089                    }
090                    finally {
091                            Context.exit();
092                    }
093            }
094    
095            protected Script getCompiledScript(String script) {
096                    String key = String.valueOf(script.hashCode());
097    
098                    Script compiledScript = (Script)SingleVMPoolUtil.get(CACHE_NAME, key);
099    
100                    if (compiledScript == null) {
101                            try {
102                                    Context context = Context.enter();
103    
104                                    compiledScript = context.compileString(
105                                            script, "script", 0, null);
106                            }
107                            finally {
108                                    Context.exit();
109                            }
110    
111                            SingleVMPoolUtil.put(CACHE_NAME, key, compiledScript);
112                    }
113    
114                    return compiledScript;
115            }
116    
117    }