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.javascript;
16  
17  import com.liferay.mozilla.javascript.Context;
18  import com.liferay.mozilla.javascript.Script;
19  import com.liferay.mozilla.javascript.Scriptable;
20  import com.liferay.mozilla.javascript.ScriptableObject;
21  import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
22  import com.liferay.portal.kernel.scripting.ScriptingException;
23  import com.liferay.portal.kernel.scripting.ScriptingExecutor;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.Set;
28  
29  /**
30   * <a href="JavaScriptExecutor.java.html"><b><i>View Source</i></b></a>
31   *
32   * @author Alberto Montero
33   */
34  public class JavaScriptExecutor implements ScriptingExecutor {
35  
36      public static final String CACHE_NAME = JavaScriptExecutor.class.getName();
37  
38      public static final String LANGUAGE = "javascript";
39  
40      public void clearCache() {
41          SingleVMPoolUtil.clear(CACHE_NAME);
42      }
43  
44      public String getLanguage() {
45          return LANGUAGE;
46      }
47  
48      public Map<String, Object> eval(
49              Set<String> allowedClasses, Map<String, Object> inputObjects,
50              Set<String> outputNames, String script)
51          throws ScriptingException {
52  
53          Script compiledScript = getCompiledScript(script);
54  
55          try {
56              Context context = Context.enter();
57  
58              Scriptable scriptable = context.initStandardObjects();
59  
60              for (String varName: inputObjects.keySet()) {
61                  ScriptableObject.putProperty(
62                      scriptable, varName,
63                      Context.javaToJS(inputObjects.get(varName), scriptable));
64              }
65  
66              if (allowedClasses != null) {
67                  context.setClassShutter(
68                      new JavaScriptClassVisibilityChecker(allowedClasses));
69              }
70  
71              compiledScript.exec(context, scriptable);
72  
73              if (outputNames == null) {
74                  return null;
75              }
76  
77              Map<String, Object> outputObjects = new HashMap<String, Object>();
78  
79              for (String outputName : outputNames) {
80                  outputObjects.put(
81                      outputName,
82                      ScriptableObject.getProperty(scriptable, outputName));
83              }
84  
85              return outputObjects;
86          }
87          catch (Exception e) {
88              throw new ScriptingException(e.getMessage() + "\n\n", e);
89          }
90          finally {
91              Context.exit();
92          }
93      }
94  
95      protected Script getCompiledScript(String script) {
96          String key = String.valueOf(script.hashCode());
97  
98          Script compiledScript = (Script)SingleVMPoolUtil.get(CACHE_NAME, key);
99  
100         if (compiledScript == null) {
101             try {
102                 Context context = Context.enter();
103 
104                 compiledScript = context.compileString(
105                     script, "script", 0, null);
106             }
107             finally {
108                 Context.exit();
109             }
110 
111             SingleVMPoolUtil.put(CACHE_NAME, key, compiledScript);
112         }
113 
114         return compiledScript;
115     }
116 
117 }