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