001    /**
002     * Copyright (c) 2000-present 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.kernel.scripting;
016    
017    import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
018    import com.liferay.portal.kernel.util.ClassLoaderPool;
019    import com.liferay.portal.kernel.util.ProxyFactory;
020    
021    import java.util.Map;
022    import java.util.Set;
023    
024    /**
025     * @author Alberto Montero
026     * @author Brian Wing Shun Chan
027     * @author Shuyang Zhou
028     */
029    public class ScriptingUtil {
030    
031            public static void clearCache(String language) throws ScriptingException {
032                    getScripting().clearCache(language);
033            }
034    
035            public static ScriptingExecutor createScriptingExecutor(
036                    String language, boolean executeInSeparateThread) {
037    
038                    return getScripting().createScriptingExecutor(
039                            language, executeInSeparateThread);
040            }
041    
042            /**
043             * @deprecated As of 6.2.0, replaced by {@link #eval(Set, Map, Set, String,
044             *             String, String...)}
045             */
046            @Deprecated
047            public static Map<String, Object> eval(
048                            Set<String> allowedClasses, Map<String, Object> inputObjects,
049                            Set<String> outputNames, String language, String script,
050                            ClassLoader... classLoaders)
051                    throws ScriptingException {
052    
053                    return getScripting().eval(
054                            allowedClasses, inputObjects, outputNames, language, script,
055                            _getServletContextNames(classLoaders));
056            }
057    
058            public static Map<String, Object> eval(
059                            Set<String> allowedClasses, Map<String, Object> inputObjects,
060                            Set<String> outputNames, String language, String script,
061                            String... servletContextNames)
062                    throws ScriptingException {
063    
064                    return getScripting().eval(
065                            allowedClasses, inputObjects, outputNames, language, script,
066                            servletContextNames);
067            }
068    
069            /**
070             * @deprecated As of 6.2.0, replaced by {@link #exec(Set, Map, String,
071             *             String, String...)}
072             */
073            @Deprecated
074            public static void exec(
075                            Set<String> allowedClasses, Map<String, Object> inputObjects,
076                            String language, String script, ClassLoader... classLoaders)
077                    throws ScriptingException {
078    
079                    getScripting().exec(
080                            allowedClasses, inputObjects, language, script,
081                            _getServletContextNames(classLoaders));
082            }
083    
084            public static void exec(
085                            Set<String> allowedClasses, Map<String, Object> inputObjects,
086                            String language, String script, String... servletContextNames)
087                    throws ScriptingException {
088    
089                    getScripting().exec(
090                            allowedClasses, inputObjects, language, script,
091                            servletContextNames);
092            }
093    
094            public static Scripting getScripting() {
095                    PortalRuntimePermission.checkGetBeanProperty(ScriptingUtil.class);
096    
097                    return _scripting;
098            }
099    
100            public static Set<String> getSupportedLanguages() {
101                    return getScripting().getSupportedLanguages();
102            }
103    
104            private static String[] _getServletContextNames(
105                    ClassLoader[] classLoaders) {
106    
107                    String[] servletContextNames = new String[classLoaders.length];
108    
109                    for (int i = 0; i < classLoaders.length; i++) {
110                            servletContextNames[i] = ClassLoaderPool.getContextName(
111                                    classLoaders[i]);
112                    }
113    
114                    return servletContextNames;
115            }
116    
117            private static final Scripting _scripting =
118                    ProxyFactory.newServiceTrackedInstance(Scripting.class);
119    
120    }