001    /**
002     * Copyright (c) 2000-2013 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.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.util.ClassLoaderUtil;
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            @Override
043            public Map<String, Object> eval(
044                            Set<String> allowedClasses, Map<String, Object> inputObjects,
045                            Set<String> outputNames, String script, ClassLoader... classLoaders)
046                    throws ScriptingException {
047    
048                    Script compiledScript = getCompiledScript(script, classLoaders);
049    
050                    try {
051                            Context context = Context.enter();
052    
053                            Scriptable scriptable = context.initStandardObjects();
054    
055                            if ((classLoaders != null) && (classLoaders.length > 0)) {
056                                    ClassLoader aggregateClassLoader =
057                                            AggregateClassLoader.getAggregateClassLoader(
058                                                    ClassLoaderUtil.getPortalClassLoader(), 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            @Override
101            public String getLanguage() {
102                    return _LANGUAGE;
103            }
104    
105            protected Script getCompiledScript(
106                    String script, ClassLoader... classLoaders) {
107    
108                    String key = String.valueOf(script.hashCode());
109    
110                    Script compiledScript = (Script)SingleVMPoolUtil.get(_CACHE_NAME, key);
111    
112                    if (compiledScript == null) {
113                            try {
114                                    Context context = Context.enter();
115    
116                                    if ((classLoaders != null) && (classLoaders.length > 0)) {
117                                            ClassLoader aggregateClassLoader =
118                                                    AggregateClassLoader.getAggregateClassLoader(
119                                                            ClassLoaderUtil.getPortalClassLoader(),
120                                                            classLoaders);
121    
122                                            context.setApplicationClassLoader(aggregateClassLoader);
123                                    }
124    
125                                    compiledScript = context.compileString(
126                                            script, "script", 0, null);
127                            }
128                            finally {
129                                    Context.exit();
130                            }
131    
132                            SingleVMPoolUtil.put(_CACHE_NAME, key, compiledScript);
133                    }
134    
135                    return compiledScript;
136            }
137    
138            private static final String _CACHE_NAME =
139                    JavaScriptExecutor.class.getName();
140    
141            private static final String _LANGUAGE = "javascript";
142    
143    }