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