001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }