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            public static Map<String, Object> eval(
043                            Set<String> allowedClasses, Map<String, Object> inputObjects,
044                            Set<String> outputNames, String language, String script)
045                    throws ScriptingException {
046    
047                    return getScripting().eval(
048                            allowedClasses, inputObjects, outputNames, language, script);
049            }
050    
051            /**
052             * @deprecated As of 6.2.0, replaced by {@link #eval(Set, Map, Set, String,
053             *             String, String...)}
054             */
055            @Deprecated
056            public static Map<String, Object> eval(
057                            Set<String> allowedClasses, Map<String, Object> inputObjects,
058                            Set<String> outputNames, String language, String script,
059                            ClassLoader... classLoaders)
060                    throws ScriptingException {
061    
062                    return getScripting().eval(
063                            allowedClasses, inputObjects, outputNames, language, script,
064                            _getServletContextNames(classLoaders));
065            }
066    
067            /**
068             * @deprecated As of 7.0.0, replaced by {@link #eval(Set, Map, Set, String,
069             *             String)}
070             */
071            @Deprecated
072            public static Map<String, Object> eval(
073                            Set<String> allowedClasses, Map<String, Object> inputObjects,
074                            Set<String> outputNames, String language, String script,
075                            String... servletContextNames)
076                    throws ScriptingException {
077    
078                    return getScripting().eval(
079                            allowedClasses, inputObjects, outputNames, language, script,
080                            servletContextNames);
081            }
082    
083            public static void exec(
084                            Set<String> allowedClasses, Map<String, Object> inputObjects,
085                            String language, String script)
086                    throws ScriptingException {
087    
088                    getScripting().exec(allowedClasses, inputObjects, language, script);
089            }
090    
091            /**
092             * @deprecated As of 6.2.0, replaced by {@link #exec(Set, Map, String,
093             *             String, String...)}
094             */
095            @Deprecated
096            public static void exec(
097                            Set<String> allowedClasses, Map<String, Object> inputObjects,
098                            String language, String script, ClassLoader... classLoaders)
099                    throws ScriptingException {
100    
101                    getScripting().exec(
102                            allowedClasses, inputObjects, language, script,
103                            _getServletContextNames(classLoaders));
104            }
105    
106            /**
107             * @deprecated As of 7.0.0, replaced by {@link #exec(Set, Map, String,
108             *             String)}
109             */
110            @Deprecated
111            public static void exec(
112                            Set<String> allowedClasses, Map<String, Object> inputObjects,
113                            String language, String script, String... servletContextNames)
114                    throws ScriptingException {
115    
116                    getScripting().exec(
117                            allowedClasses, inputObjects, language, script,
118                            servletContextNames);
119            }
120    
121            public static Scripting getScripting() {
122                    PortalRuntimePermission.checkGetBeanProperty(ScriptingUtil.class);
123    
124                    return _scripting;
125            }
126    
127            public static Set<String> getSupportedLanguages() {
128                    return getScripting().getSupportedLanguages();
129            }
130    
131            private static String[] _getServletContextNames(
132                    ClassLoader[] classLoaders) {
133    
134                    String[] servletContextNames = new String[classLoaders.length];
135    
136                    for (int i = 0; i < classLoaders.length; i++) {
137                            servletContextNames[i] = ClassLoaderPool.getContextName(
138                                    classLoaders[i]);
139                    }
140    
141                    return servletContextNames;
142            }
143    
144            private static final Scripting _scripting =
145                    ProxyFactory.newServiceTrackedInstance(Scripting.class);
146    
147    }