From: Henri Sara Date: Fri, 10 May 2013 08:36:46 +0000 (+0300) Subject: Liferay 6.2 compatibility (#11751) X-Git-Tag: 7.0.6~17 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=3f5d022e7fca887341cc7ffb0ff48ac99ed558b4;p=vaadin-framework.git Liferay 6.2 compatibility (#11751) Change-Id: I5a14aba47f3e92249b245392acaf5ff9bda62083 --- diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index 7d4618d58e..ac4e904898 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -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",