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