001    /**
002     * Copyright (c) 2000-2012 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.module.framework;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.InstanceFactory;
020    import com.liferay.portal.kernel.util.MethodKey;
021    import com.liferay.portal.kernel.util.ReflectionUtil;
022    import com.liferay.portal.util.ClassLoaderUtil;
023    import com.liferay.portal.util.PropsValues;
024    
025    import java.io.File;
026    
027    import java.lang.reflect.Method;
028    
029    import java.net.URL;
030    
031    import java.util.HashMap;
032    import java.util.Map;
033    
034    /**
035     * @author Miguel Pastor
036     * @author Raymond Augé
037     */
038    public class ModuleFrameworkAdapterHelper {
039    
040            public static ClassLoader getClassLoader() {
041                    if (_classLoader != null) {
042                            return _classLoader;
043                    }
044    
045                    try {
046                            File coreDir = new File(PropsValues.MODULE_FRAMEWORK_CORE_DIR);
047    
048                            File[] files = coreDir.listFiles();
049    
050                            URL[] urls = new URL[files.length];
051    
052                            for (int i = 0; i < urls.length; i++) {
053                                    urls[i] = new URL("file:" + files[i].getAbsolutePath());
054                            }
055    
056                            _classLoader = new ModuleFrameworkClassLoader(
057                                    urls, ClassLoaderUtil.getPortalClassLoader());
058    
059                            return _classLoader;
060                    }
061                    catch (Exception e) {
062                            _log.error(
063                                    "Unable to configure the class loader for the module " +
064                                            "framework");
065    
066                            throw new RuntimeException(e);
067                    }
068            }
069    
070            public ModuleFrameworkAdapterHelper(String className) {
071                    try {
072                            _adaptedObject = InstanceFactory.newInstance(
073                                    getClassLoader(), className);
074                    }
075                    catch (Exception e) {
076                            _log.error("Unable to load the module framework");
077    
078                            throw new RuntimeException(e);
079                    }
080            }
081    
082            public Object exec(
083                    String methodName, Class<?>[] parameterTypes, Object...parameters) {
084    
085                    try {
086                            Method method = searchMethod(methodName, parameterTypes);
087    
088                            return method.invoke(_adaptedObject, parameters);
089                    }
090                    catch (Exception e) {
091                            _log.error(e, e);
092    
093                            throw new RuntimeException(e);
094                    }
095            }
096    
097            public Object execute(String methodName, Object...parameters) {
098                    Class<?>[] parameterTypes = ReflectionUtil.getParameterTypes(
099                            parameters);
100    
101                    return exec(methodName, parameterTypes, parameters);
102            }
103    
104            private Method searchMethod(String methodName, Class<?>[] parameterTypes)
105                    throws Exception {
106    
107                    MethodKey methodKey = new MethodKey(
108                            _adaptedObject.getClass(), methodName, parameterTypes);
109    
110                    if (_methods.containsKey(methodKey)) {
111                            return _methods.get(methodKey);
112                    }
113    
114                    Method method = ReflectionUtil.getDeclaredMethod(
115                            _adaptedObject.getClass(), methodName, parameterTypes);
116    
117                    _methods.put(methodKey, method);
118    
119                    return method;
120            }
121    
122            private static Log _log = LogFactoryUtil.getLog(
123                    ModuleFrameworkAdapterHelper.class);
124    
125            private static ClassLoader _classLoader;
126            private static Map<MethodKey, Method> _methods =
127                    new HashMap<MethodKey, Method>();
128    
129            private Object _adaptedObject;
130    
131    }