001    /**
002     * Copyright (c) 2000-2012 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.portal.kernel.cache.SingleVMPoolUtil;
018    import com.liferay.portal.kernel.scripting.BaseScriptingExecutor;
019    import com.liferay.portal.kernel.scripting.ScriptingException;
020    import com.liferay.portal.kernel.util.AggregateClassLoader;
021    import com.liferay.portal.security.pacl.PACLClassLoaderUtil;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.Set;
026    
027    import org.mozilla.javascript.Context;
028    import org.mozilla.javascript.Script;
029    import org.mozilla.javascript.Scriptable;
030    import org.mozilla.javascript.ScriptableObject;
031    
032    /**
033     * @author Alberto Montero
034     */
035    public class JavaScriptExecutor extends BaseScriptingExecutor {
036    
037            @Override
038            public void clearCache() {
039                    SingleVMPoolUtil.clear(_CACHE_NAME);
040            }
041    
042            public Map<String, Object> eval(
043                            Set<String> allowedClasses, Map<String, Object> inputObjects,
044                            Set<String> outputNames, String script, ClassLoader... classLoaders)
045                    throws ScriptingException {
046    
047                    Script compiledScript = getCompiledScript(script, classLoaders);
048    
049                    try {
050                            Context context = Context.enter();
051    
052                            Scriptable scriptable = context.initStandardObjects();
053    
054                            if ((classLoaders != null) && (classLoaders.length > 0)) {
055                                    ClassLoader aggregateClassLoader =
056                                            AggregateClassLoader.getAggregateClassLoader(
057                                                    PACLClassLoaderUtil.getPortalClassLoader(),
058                                                    classLoaders);
059    
060                                    context.setApplicationClassLoader(aggregateClassLoader);
061                            }
062    
063                            for (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
064                                    String key = entry.getKey();
065                                    Object value = entry.getValue();
066    
067                                    ScriptableObject.putProperty(
068                                            scriptable, key, Context.javaToJS(value, scriptable));
069                            }
070    
071                            if (allowedClasses != null) {
072                                    context.setClassShutter(
073                                            new JavaScriptClassVisibilityChecker(allowedClasses));
074                            }
075    
076                            compiledScript.exec(context, scriptable);
077    
078                            if (outputNames == null) {
079                                    return null;
080                            }
081    
082                            Map<String, Object> outputObjects = new HashMap<String, Object>();
083    
084                            for (String outputName : outputNames) {
085                                    outputObjects.put(
086                                            outputName,
087                                            ScriptableObject.getProperty(scriptable, outputName));
088                            }
089    
090                            return outputObjects;
091                    }
092                    catch (Exception e) {
093                            throw new ScriptingException(e.getMessage() + "\n\n", e);
094                    }
095                    finally {
096                            Context.exit();
097                    }
098            }
099    
100            public String getLanguage() {
101                    return _LANGUAGE;
102            }
103    
104            protected Script getCompiledScript(
105                    String script, ClassLoader... classLoaders) {
106    
107                    String key = String.valueOf(script.hashCode());
108    
109                    Script compiledScript = (Script)SingleVMPoolUtil.get(_CACHE_NAME, key);
110    
111                    if (compiledScript == null) {
112                            try {
113                                    Context context = Context.enter();
114    
115                                    if ((classLoaders != null) && (classLoaders.length > 0)) {
116                                            ClassLoader aggregateClassLoader =
117                                                    AggregateClassLoader.getAggregateClassLoader(
118                                                            PACLClassLoaderUtil.getPortalClassLoader(),
119                                                            classLoaders);
120    
121                                            context.setApplicationClassLoader(aggregateClassLoader);
122                                    }
123    
124                                    compiledScript = context.compileString(
125                                            script, "script", 0, null);
126                            }
127                            finally {
128                                    Context.exit();
129                            }
130    
131                            SingleVMPoolUtil.put(_CACHE_NAME, key, compiledScript);
132                    }
133    
134                    return compiledScript;
135            }
136    
137            private static final String _CACHE_NAME =
138                    JavaScriptExecutor.class.getName();
139    
140            private static final String _LANGUAGE = "javascript";
141    
142    }