001    /**
002     * Copyright (c) 2000-2012 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.python;
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.ExecutionException;
021    import com.liferay.portal.kernel.scripting.ScriptingException;
022    
023    import java.util.HashMap;
024    import java.util.Map;
025    import java.util.Set;
026    
027    import org.python.core.CompileMode;
028    import org.python.core.Py;
029    import org.python.core.PyCode;
030    import org.python.core.PySystemState;
031    import org.python.util.InteractiveInterpreter;
032    
033    /**
034     * @author Alberto Montero
035     */
036    public class PythonExecutor extends BaseScriptingExecutor {
037    
038            @Override
039            public void clearCache() {
040                    _portalCache.removeAll();
041            }
042    
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                    if (allowedClasses != null) {
049                            throw new ExecutionException(
050                                    "Constrained execution not supported for Python");
051                    }
052    
053                    PyCode compiledScript = getCompiledScript(script);
054    
055                    InteractiveInterpreter interactiveInterpreter =
056                            new InteractiveInterpreter();
057    
058                    for (Map.Entry<String, Object> entry : inputObjects.entrySet()) {
059                            String key = entry.getKey();
060                            Object value = entry.getValue();
061    
062                            interactiveInterpreter.set(key, value);
063                    }
064    
065                    interactiveInterpreter.exec(compiledScript);
066    
067                    if (outputNames == null) {
068                            return null;
069                    }
070    
071                    Map<String, Object> outputObjects = new HashMap<String, Object>();
072    
073                    for (String outputName : outputNames) {
074                            outputObjects.put(
075                                    outputName, interactiveInterpreter.get(outputName));
076                    }
077    
078                    return outputObjects;
079            }
080    
081            public String getLanguage() {
082                    return _LANGUAGE;
083            }
084    
085            protected PyCode getCompiledScript(String script) {
086                    if (!_initialized) {
087                            synchronized (this) {
088                                    if (!_initialized) {
089                                            PySystemState.initialize();
090    
091                                            _initialized = true;
092                                    }
093                            }
094                    }
095    
096                    String key = String.valueOf(script.hashCode());
097    
098                    PyCode compiledScript = _portalCache.get(key);
099    
100                    if (compiledScript == null) {
101                            compiledScript = Py.compile_flags(
102                                    script, "<string>", CompileMode.exec, Py.getCompilerFlags());
103    
104                            _portalCache.put(key, compiledScript);
105                    }
106    
107                    return compiledScript;
108            }
109    
110            private static final String _CACHE_NAME = PythonExecutor.class.getName();
111    
112            private static final String _LANGUAGE = "python";
113    
114            private volatile boolean _initialized;
115            private PortalCache<String, PyCode> _portalCache =
116                    SingleVMPoolUtil.getCache(_CACHE_NAME);
117    
118    }