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