1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.scripting.python;
16  
17  import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
18  import com.liferay.portal.kernel.scripting.ExecutionException;
19  import com.liferay.portal.kernel.scripting.ScriptingException;
20  import com.liferay.portal.kernel.scripting.ScriptingExecutor;
21  
22  import java.util.HashMap;
23  import java.util.Map;
24  import java.util.Set;
25  
26  import org.python.core.CompileMode;
27  import org.python.core.Py;
28  import org.python.core.PyCode;
29  import org.python.core.PySystemState;
30  import org.python.util.InteractiveInterpreter;
31  
32  /**
33   * <a href="PythonExecutor.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Alberto Montero
36   */
37  public class PythonExecutor implements ScriptingExecutor {
38  
39      public static final String CACHE_NAME = PythonExecutor.class.getName();
40  
41      public static final String LANGUAGE = "python";
42  
43      public void clearCache() {
44          SingleVMPoolUtil.clear(CACHE_NAME);
45      }
46  
47      public String getLanguage() {
48          return LANGUAGE;
49      }
50  
51      public Map<String, Object> eval(
52              Set<String> allowedClasses, Map<String, Object> inputObjects,
53              Set<String> outputNames, String script)
54          throws ScriptingException {
55  
56          if (allowedClasses != null) {
57              throw new ExecutionException(
58                  "Constrained execution not supported for Python");
59          }
60  
61          PyCode compiledScript = getCompiledScript(script);
62  
63          InteractiveInterpreter interactiveInterpreter =
64              new InteractiveInterpreter();
65  
66          for (String varName: inputObjects.keySet()) {
67              interactiveInterpreter.set(varName, inputObjects.get(varName));
68          }
69  
70          interactiveInterpreter.exec(compiledScript);
71  
72          if (outputNames == null) {
73              return null;
74          }
75  
76          Map<String, Object> outputObjects = new HashMap<String, Object>();
77  
78          for (String outputName : outputNames) {
79              outputObjects.put(
80                  outputName, interactiveInterpreter.get(outputName));
81          }
82  
83          return outputObjects;
84      }
85  
86      protected PyCode getCompiledScript(String script) {
87          if (!_initialized) {
88              synchronized (this) {
89                  PySystemState.initialize();
90  
91                  _initialized = true;
92              }
93          }
94  
95          String key = String.valueOf(script.hashCode());
96  
97          PyCode compiledScript = (PyCode)SingleVMPoolUtil.get(CACHE_NAME, key);
98  
99          if (compiledScript == null) {
100             compiledScript = Py.compile_flags(
101                 script, "<string>", CompileMode.exec, Py.getCompilerFlags());
102 
103             SingleVMPoolUtil.put(CACHE_NAME, key, compiledScript);
104         }
105 
106         return compiledScript;
107     }
108 
109     private boolean _initialized;
110 
111 }