001
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.ClassLoaderUtil;
020 import com.liferay.portal.kernel.util.FileUtil;
021 import com.liferay.portal.kernel.util.InstanceFactory;
022 import com.liferay.portal.kernel.util.MethodKey;
023 import com.liferay.portal.kernel.util.ReflectionUtil;
024 import com.liferay.portal.kernel.util.ServiceLoader;
025 import com.liferay.portal.security.lang.DoPrivilegedUtil;
026 import com.liferay.portal.util.FileImpl;
027
028 import java.lang.reflect.Method;
029
030 import java.net.URL;
031
032 import java.util.HashMap;
033 import java.util.List;
034 import java.util.Map;
035
036
040 public class ModuleFrameworkAdapterHelper {
041
042 public static ClassLoader getClassLoader() {
043 if (_classLoader != null) {
044 return _classLoader;
045 }
046
047 try {
048 if (FileUtil.getFile() == null) {
049 FileUtil fileUtil = new FileUtil();
050
051 fileUtil.setFile(DoPrivilegedUtil.wrap(new FileImpl()));
052 }
053
054 List<ClasspathResolver> classpathResolvers = ServiceLoader.load(
055 ClasspathResolver.class);
056
057 ClasspathResolver classpathResolver = classpathResolvers.get(0);
058
059 URL[] classpathURLs = classpathResolver.getClasspathURLs();
060
061 _classLoader = new ModuleFrameworkClassLoader(
062 classpathURLs, ClassLoaderUtil.getPortalClassLoader());
063
064 return _classLoader;
065 }
066 catch (Exception e) {
067 _log.error(
068 "Unable to configure the class loader for the module " +
069 "framework");
070
071 throw new RuntimeException(e);
072 }
073 }
074
075 public ModuleFrameworkAdapterHelper(String className) {
076 try {
077 _adaptedObject = InstanceFactory.newInstance(
078 getClassLoader(), className);
079 }
080 catch (Exception e) {
081 _log.error("Unable to load the module framework");
082
083 throw new RuntimeException(e);
084 }
085 }
086
087 public Object exec(
088 String methodName, Class<?>[] parameterTypes, Object...parameters) {
089
090 try {
091 Method method = searchMethod(methodName, parameterTypes);
092
093 return method.invoke(_adaptedObject, parameters);
094 }
095 catch (Exception e) {
096 _log.error(e, e);
097
098 throw new RuntimeException(e);
099 }
100 }
101
102 public Object execute(String methodName, Object...parameters) {
103 Class<?>[] parameterTypes = ReflectionUtil.getParameterTypes(
104 parameters);
105
106 return exec(methodName, parameterTypes, parameters);
107 }
108
109 protected Method searchMethod(String methodName, Class<?>[] parameterTypes)
110 throws Exception {
111
112 MethodKey methodKey = new MethodKey(
113 _adaptedObject.getClass(), methodName, parameterTypes);
114
115 if (_methods.containsKey(methodKey)) {
116 return _methods.get(methodKey);
117 }
118
119 Method method = ReflectionUtil.getDeclaredMethod(
120 _adaptedObject.getClass(), methodName, parameterTypes);
121
122 _methods.put(methodKey, method);
123
124 return method;
125 }
126
127 private static final Log _log = LogFactoryUtil.getLog(
128 ModuleFrameworkAdapterHelper.class);
129
130 private static ClassLoader _classLoader;
131 private static final Map<MethodKey, Method> _methods = new HashMap<>();
132
133 private final Object _adaptedObject;
134
135 }