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.util.AggregateClassLoader;
018    import com.liferay.portal.kernel.util.FileUtil;
019    import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
020    
021    import java.io.File;
022    import java.io.IOException;
023    
024    import java.util.Map;
025    import java.util.Set;
026    
027    /**
028     * @author Alberto Montero
029     * @author Brian Wing Shun Chan
030     */
031    public abstract class BaseScriptingExecutor implements ScriptingExecutor {
032    
033            @Override
034            public void clearCache() {
035            }
036    
037            @Override
038            public Map<String, Object> eval(
039                            Set<String> allowedClasses, Map<String, Object> inputObjects,
040                            Set<String> outputNames, File scriptFile,
041                            ClassLoader... classloaders)
042                    throws ScriptingException {
043    
044                    try {
045                            String script = FileUtil.read(scriptFile);
046    
047                            return eval(
048                                    allowedClasses, inputObjects, outputNames, script,
049                                    classloaders);
050                    }
051                    catch (IOException ioe) {
052                            throw new ScriptingException(ioe);
053                    }
054            }
055    
056            @Override
057            public ScriptingContainer<?> getScriptingContainer() {
058                    return null;
059            }
060    
061            protected ClassLoader getAggregateClassLoader(ClassLoader... classLoaders) {
062                    return AggregateClassLoader.getAggregateClassLoader(
063                            getScriptingExecutorClassLoader(), classLoaders);
064            }
065    
066            protected ClassLoader getScriptingExecutorClassLoader() {
067                    return _scriptingExecutorClassLoader;
068            }
069    
070            protected void initScriptingExecutorClassLoader() {
071                    Class<?> clazz = getClass();
072    
073                    ClassLoader classLoader = clazz.getClassLoader();
074    
075                    if (!classLoader.equals(PortalClassLoaderUtil.getClassLoader())) {
076                            _scriptingExecutorClassLoader =
077                                    AggregateClassLoader.getAggregateClassLoader(
078                                            PortalClassLoaderUtil.getClassLoader(), classLoader);
079                    }
080                    else {
081                            _scriptingExecutorClassLoader = classLoader;
082                    }
083            }
084    
085            private ClassLoader _scriptingExecutorClassLoader;
086    
087    }