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            public static Map<String, Object> eval(
035                            Set<String> allowedClasses, Map<String, Object> inputObjects,
036                            Set<String> outputNames, String language, String script,
037                            ClassLoader... classLoaders)
038                    throws ScriptingException {
039    
040                    return getScripting().eval(
041                            allowedClasses, inputObjects, outputNames, language, script,
042                            _getServletContextNames(classLoaders));
043            }
044    
045            public static void exec(
046                            Set<String> allowedClasses, Map<String, Object> inputObjects,
047                            String language, String script, ClassLoader... classLoaders)
048                    throws ScriptingException {
049    
050                    getScripting().exec(
051                            allowedClasses, inputObjects, language, script,
052                            _getServletContextNames(classLoaders));
053            }
054    
055            public static Scripting getScripting() {
056                    PortalRuntimePermission.checkGetBeanProperty(ScriptingUtil.class);
057    
058                    return _scripting;
059            }
060    
061            public static Set<String> getSupportedLanguages() {
062                    return getScripting().getSupportedLanguages();
063            }
064    
065            public void setScripting(Scripting scripting) {
066                    PortalRuntimePermission.checkSetBeanProperty(getClass());
067    
068                    _scripting = scripting;
069            }
070    
071            private static String[] _getServletContextNames(
072                    ClassLoader[] classLoaders) {
073    
074                    String[] servletContextNames = new String[classLoaders.length];
075    
076                    for (int i = 0; i < classLoaders.length; i++) {
077                            servletContextNames[i] = ClassLoaderPool.getContextName(
078                                    classLoaders[i]);
079                    }
080    
081                    return servletContextNames;
082            }
083    
084            private static Scripting _scripting;
085    
086    }