]> source.dussan.org Git - vaadin-framework.git/commitdiff
Liferay 6.2 compatibility (#11751)
authorHenri Sara <hesara@vaadin.com>
Fri, 10 May 2013 08:36:46 +0000 (11:36 +0300)
committerVaadin Code Review <review@vaadin.com>
Fri, 10 May 2013 09:58:12 +0000 (09:58 +0000)
Change-Id: I5a14aba47f3e92249b245392acaf5ff9bda62083

server/src/com/vaadin/server/VaadinPortlet.java

index 7d4618d58e4fd99f786bca306cab8a431f53c61a..ac4e904898387f3085b34bfc38130563dfaf0c87 100644 (file)
@@ -21,6 +21,7 @@ import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.Serializable;
+import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.security.GeneralSecurityException;
@@ -47,7 +48,7 @@ import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 import javax.servlet.http.HttpServletResponse;
 
-import com.liferay.portal.kernel.util.PortalClassInvoker;
+import com.liferay.portal.kernel.util.PortalClassLoaderUtil;
 import com.liferay.portal.kernel.util.PropsUtil;
 import com.vaadin.server.AbstractCommunicationManager.Callback;
 import com.vaadin.ui.UI;
@@ -178,19 +179,75 @@ public class VaadinPortlet extends GenericPortlet implements Constants,
             return PropsUtil.get(name);
         }
 
+        /**
+         * Simplified version of what Liferay PortalClassInvoker did. This is
+         * used because the API of PortalClassInvoker has changed in Liferay
+         * 6.2.
+         * 
+         * This simply uses reflection with Liferay class loader. Parameters are
+         * Strings to avoid static dependencies and to load all classes with
+         * Liferay's own class loader. Only static utility methods are
+         * supported.
+         * 
+         * This method is for internal use only and may change in future
+         * versions.
+         * 
+         * @param className
+         *            name of the Liferay class to call
+         * @param methodName
+         *            name of the method to call
+         * @param parameterClassName
+         *            name of the parameter class of the method
+         * @throws Exception
+         * @return return value of the invoked method
+         */
+        private static Object invokeStaticLiferayMethod(String className,
+                String methodName, Object argument, String parameterClassName)
+                throws Exception {
+            Thread currentThread = Thread.currentThread();
+
+            ClassLoader contextClassLoader = currentThread
+                    .getContextClassLoader();
+
+            try {
+                // this should be available across all Liferay versions with no
+                // problematic static dependencies
+                ClassLoader portalClassLoader = PortalClassLoaderUtil
+                        .getClassLoader();
+                // this is in case the class loading triggers code that
+                // explicitly
+                // uses current thread class loader
+                currentThread.setContextClassLoader(portalClassLoader);
+
+                Class<?> targetClass = portalClassLoader.loadClass(className);
+                Class<?> parameterClass = portalClassLoader
+                        .loadClass(parameterClassName);
+                Method method = targetClass.getMethod(methodName,
+                        parameterClass);
+
+                return method.invoke(null, new Object[] { argument });
+            } catch (InvocationTargetException ite) {
+                throw (Exception) ite.getCause();
+            } finally {
+                currentThread.setContextClassLoader(contextClassLoader);
+            }
+        }
+
         private static HttpServletRequest getOriginalRequest(
                 PortletRequest request) {
             try {
                 // httpRequest = PortalUtil.getHttpServletRequest(request);
-                HttpServletRequest httpRequest = (HttpServletRequest) PortalClassInvoker
-                        .invoke("com.liferay.portal.util.PortalUtil",
-                                "getHttpServletRequest", request);
+                HttpServletRequest httpRequest = (HttpServletRequest) invokeStaticLiferayMethod(
+                        "com.liferay.portal.util.PortalUtil",
+                        "getHttpServletRequest", request,
+                        "javax.portlet.PortletRequest");
 
                 // httpRequest =
                 // PortalUtil.getOriginalServletRequest(httpRequest);
-                httpRequest = (HttpServletRequest) PortalClassInvoker.invoke(
+                httpRequest = (HttpServletRequest) invokeStaticLiferayMethod(
                         "com.liferay.portal.util.PortalUtil",
-                        "getOriginalServletRequest", httpRequest);
+                        "getOriginalServletRequest", httpRequest,
+                        "javax.servlet.http.HttpServletRequest");
                 return httpRequest;
             } catch (Exception e) {
                 throw new IllegalStateException("Liferay request not detected",