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