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.groovy;
016    
017    import com.liferay.portal.kernel.cache.SingleVMPoolUtil;
018    import com.liferay.portal.kernel.scripting.BaseScriptingExecutor;
019    import com.liferay.portal.kernel.scripting.ExecutionException;
020    import com.liferay.portal.kernel.scripting.ScriptingException;
021    import com.liferay.portal.kernel.util.AggregateClassLoader;
022    import com.liferay.portal.util.ClassLoaderUtil;
023    
024    import groovy.lang.Binding;
025    import groovy.lang.GroovyShell;
026    import groovy.lang.Script;
027    
028    import java.util.HashMap;
029    import java.util.Map;
030    import java.util.Set;
031    import java.util.WeakHashMap;
032    
033    /**
034     * @author Alberto Montero
035     * @author Brian Wing Shun Chan
036     */
037    public class GroovyExecutor extends BaseScriptingExecutor {
038    
039            @Override
040            public void clearCache() {
041                    SingleVMPoolUtil.clear(_CACHE_NAME);
042            }
043    
044            @Override
045            public Map<String, Object> eval(
046                            Set<String> allowedClasses, Map<String, Object> inputObjects,
047                            Set<String> outputNames, String script, ClassLoader... classLoaders)
048                    throws ScriptingException {
049    
050                    if (allowedClasses != null) {
051                            throw new ExecutionException(
052                                    "Constrained execution not supported for Groovy");
053                    }
054    
055                    Script compiledScript = getCompiledScript(script, classLoaders);
056    
057                    Binding binding = new Binding(inputObjects);
058    
059                    compiledScript.setBinding(binding);
060    
061                    compiledScript.run();
062    
063                    if (outputNames == null) {
064                            return null;
065                    }
066    
067                    Map<String, Object> outputObjects = new HashMap<String, Object>();
068    
069                    for (String outputName : outputNames) {
070                            outputObjects.put(outputName, binding.getVariable(outputName));
071                    }
072    
073                    return outputObjects;
074            }
075    
076            @Override
077            public String getLanguage() {
078                    return _LANGUAGE;
079            }
080    
081            protected Script getCompiledScript(
082                    String script, ClassLoader[] classLoaders) {
083    
084                    GroovyShell groovyShell = getGroovyShell(classLoaders);
085    
086                    String key = String.valueOf(script.hashCode());
087    
088                    Script compiledScript = (Script)SingleVMPoolUtil.get(_CACHE_NAME, key);
089    
090                    if (compiledScript == null) {
091                            compiledScript = groovyShell.parse(script);
092    
093                            SingleVMPoolUtil.put(_CACHE_NAME, key, compiledScript);
094                    }
095    
096                    return compiledScript;
097            }
098    
099            protected GroovyShell getGroovyShell(ClassLoader[] classLoaders) {
100                    if ((classLoaders == null) || (classLoaders.length == 0)) {
101                            if (_groovyShell == null) {
102                                    synchronized (this) {
103                                            if (_groovyShell == null) {
104                                                    _groovyShell = new GroovyShell();
105                                            }
106                                    }
107                            }
108    
109                            return _groovyShell;
110                    }
111    
112                    ClassLoader aggregateClassLoader =
113                            AggregateClassLoader.getAggregateClassLoader(
114                                    ClassLoaderUtil.getPortalClassLoader(), classLoaders);
115    
116                    GroovyShell groovyShell = null;
117    
118                    if (!_groovyShells.containsKey(aggregateClassLoader)) {
119                            synchronized (this) {
120                                    if (!_groovyShells.containsKey(aggregateClassLoader)) {
121                                            groovyShell = new GroovyShell(aggregateClassLoader);
122    
123                                            _groovyShells.put(aggregateClassLoader, groovyShell);
124                                    }
125                            }
126                    }
127    
128                    return groovyShell;
129            }
130    
131            private static final String _CACHE_NAME = GroovyExecutor.class.getName();
132    
133            private static final String _LANGUAGE = "groovy";
134    
135            private volatile GroovyShell _groovyShell = new GroovyShell();
136            private volatile Map<ClassLoader, GroovyShell> _groovyShells =
137                    new WeakHashMap<ClassLoader, GroovyShell>();
138    
139    }