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.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.FileUtil;
020    import com.liferay.portal.kernel.util.InstanceFactory;
021    import com.liferay.portal.kernel.util.MethodKey;
022    import com.liferay.portal.kernel.util.ReflectionUtil;
023    import com.liferay.portal.kernel.util.StringUtil;
024    import com.liferay.portal.util.ClassLoaderUtil;
025    import com.liferay.portal.util.FileImpl;
026    import com.liferay.portal.util.PropsValues;
027    
028    import java.io.File;
029    
030    import java.lang.reflect.Method;
031    
032    import java.net.URL;
033    
034    import java.util.HashMap;
035    import java.util.Map;
036    
037    /**
038     * @author Miguel Pastor
039     * @author Raymond Augé
040     */
041    public class ModuleFrameworkAdapterHelper {
042    
043            public static ClassLoader getClassLoader() {
044                    if (_classLoader != null) {
045                            return _classLoader;
046                    }
047    
048                    try {
049                            _initDir(
050                                    "com/liferay/portal/deploy/dependencies/osgi/core",
051                                    PropsValues.MODULE_FRAMEWORK_CORE_DIR);
052                            _initDir(
053                                    "com/liferay/portal/deploy/dependencies/osgi/portal",
054                                    PropsValues.MODULE_FRAMEWORK_PORTAL_DIR);
055    
056                            File coreDir = new File(PropsValues.MODULE_FRAMEWORK_CORE_DIR);
057    
058                            File[] files = coreDir.listFiles();
059    
060                            URL[] urls = new URL[files.length];
061    
062                            for (int i = 0; i < urls.length; i++) {
063                                    urls[i] = new URL("file:" + files[i].getAbsolutePath());
064                            }
065    
066                            _classLoader = new ModuleFrameworkClassLoader(
067                                    urls, ClassLoaderUtil.getPortalClassLoader());
068    
069                            return _classLoader;
070                    }
071                    catch (Exception e) {
072                            _log.error(
073                                    "Unable to configure the class loader for the module " +
074                                            "framework");
075    
076                            throw new RuntimeException(e);
077                    }
078            }
079    
080            public ModuleFrameworkAdapterHelper(String className) {
081                    try {
082                            _adaptedObject = InstanceFactory.newInstance(
083                                    getClassLoader(), className);
084                    }
085                    catch (Exception e) {
086                            _log.error("Unable to load the module framework");
087    
088                            throw new RuntimeException(e);
089                    }
090            }
091    
092            public Object exec(
093                    String methodName, Class<?>[] parameterTypes, Object...parameters) {
094    
095                    try {
096                            Method method = searchMethod(methodName, parameterTypes);
097    
098                            return method.invoke(_adaptedObject, parameters);
099                    }
100                    catch (Exception e) {
101                            _log.error(e, e);
102    
103                            throw new RuntimeException(e);
104                    }
105            }
106    
107            public Object execute(String methodName, Object...parameters) {
108                    Class<?>[] parameterTypes = ReflectionUtil.getParameterTypes(
109                            parameters);
110    
111                    return exec(methodName, parameterTypes, parameters);
112            }
113    
114            protected Method searchMethod(String methodName, Class<?>[] parameterTypes)
115                    throws Exception {
116    
117                    MethodKey methodKey = new MethodKey(
118                            _adaptedObject.getClass(), methodName, parameterTypes);
119    
120                    if (_methods.containsKey(methodKey)) {
121                            return _methods.get(methodKey);
122                    }
123    
124                    Method method = ReflectionUtil.getDeclaredMethod(
125                            _adaptedObject.getClass(), methodName, parameterTypes);
126    
127                    _methods.put(methodKey, method);
128    
129                    return method;
130            }
131    
132            private static void _initDir(String sourcePath, String destinationPath)
133                    throws Exception {
134    
135                    if (FileUtil.getFile() == null) {
136                            FileUtil fileUtil = new FileUtil();
137    
138                            fileUtil.setFile(new FileImpl());
139                    }
140    
141                    if (FileUtil.exists(destinationPath)) {
142                            return;
143                    }
144    
145                    FileUtil.mkdirs(destinationPath);
146    
147                    ClassLoader classLoader = ClassLoaderUtil.getPortalClassLoader();
148    
149                    String[] jarFileNames = StringUtil.split(
150                            StringUtil.read(classLoader, sourcePath + "/jars.txt"));
151    
152                    for (String jarFileName : jarFileNames) {
153                            byte[] bytes = FileUtil.getBytes(
154                                    classLoader.getResourceAsStream(
155                                            sourcePath + "/" + jarFileName));
156    
157                            FileUtil.write(new File(destinationPath, jarFileName), bytes);
158                    }
159            }
160    
161            private static Log _log = LogFactoryUtil.getLog(
162                    ModuleFrameworkAdapterHelper.class);
163    
164            private static ClassLoader _classLoader;
165            private static Map<MethodKey, Method> _methods =
166                    new HashMap<MethodKey, Method>();
167    
168            private Object _adaptedObject;
169    
170    }