Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Jason Smith
Portlet is initialized before Servlet Context in 6.1.1 CE
August 19, 2012 4:51 AM
Answer

Jason Smith

Rank: New Member

Posts: 17

Join Date: April 18, 2011

Recent Posts

I have web.xml, which initializes parent Spring Web Context for portlets.
And portlets create subContext using common web context.

When i upgraded from 6.1.0 to 6.1.1 it seems portlets are initialized before web context listener is executed.
Is there any fix for this? Because now portlets are initialized before web.xml ContextListener is run and therefore portlets dont find any beans that is initialized in web context and I get a lot of Autowire exceptions.

web.xml
-----
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans/default-*-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

portlet.xml
-----
<portlet>
<portlet-name>locale</portlet-name>
<display-name>Locale Portlet</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<name>contextConfigLocation</name>
<value>classpath:beans/portlet/locale-portlet-context.xml</value>
</init-param>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
</supports>
</portlet>
David H Nebinger
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 19, 2012 6:35 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 9468

Join Date: September 1, 2006

Recent Posts

ContextLoaderListener is really for servlets, not portlets.

Try using the com.liferay.portal.kernel.spring.context.PortletContextLoaderListener instead.
Jason Smith
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 19, 2012 6:37 AM
Answer

Jason Smith

Rank: New Member

Posts: 17

Join Date: April 18, 2011

Recent Posts

David H Nebinger:
ContextLoaderListener is really for servlets, not portlets.

Try using the com.liferay.portal.kernel.spring.context.PortletContextLoaderListener instead.


Is "PortletContextLoaderListener" the way to go, even thought I have pure Spring MVC portlet, which has nothing to do with anything liferay dependent?
David H Nebinger
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 19, 2012 7:03 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 9468

Join Date: September 1, 2006

Recent Posts

Jason Smith:
Is "PortletContextLoaderListener" the way to go, even thought I have pure Spring MVC portlet, which has nothing to do with anything liferay dependent?


PorletContextLoaderListener is actually an extension (indirectly) of Spring's ContextLoaderListener, so I don't think you're getting outside of your 'pure spring mvc portlet' concept.

I just think it's better for working in the portlet environment...

What you have to remember here is what you're actually deploying. There's two pieces - first is your portlet, but second (a little less well known) is a servlet (it is a war file distribution, remember?). The servlet side of things is actually a Liferay-injected servlet that redirects the incoming servlet requests into the portal logic to keep you in the portal's lifecycle. The portlet is your own, but it is effectively ignored by the application container and is only accessed by the main Liferay servlet.

So the portlet can be initialized first (because Liferay is handling that, not the app container). Using the PortletContextLoaderListener should ensure that the portlet context is initialized before the portlet gets invoked.
Jason Smith
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 19, 2012 10:16 PM
Answer

Jason Smith

Rank: New Member

Posts: 17

Join Date: April 18, 2011

Recent Posts

PorletContextLoaderListener doesn't help me here, since that also is called after the portlet is initialized.

With liferay 6.1.0 CE web context listener was called before the portlets were initialized. Therefore portlets inherited the Spring ApplicationContext that the web ContextListener created.

With liferay 6.1.1 CE portlets are initialized before web context listener is called. And therefore there is no ApplicationContext to inherit.

In my case, I cannot have portlets initialized before web servletContext is initialized.

Is there any way to change the order?
Satheesh Ravi
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 19, 2012 8:28 PM
Answer

Satheesh Ravi

Rank: Junior Member

Posts: 35

Join Date: November 24, 2011

Recent Posts

Hey Smith,

You need to understand one thing. The first application deployed in the server is the ROOT folder which is nothing but the liferay. It has it's own web.xml which redirects the requests to appropriate portlet. Check the web.xml in the ROOT folder once. Hope this is useful.
David H Nebinger
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 19, 2012 8:24 PM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 9468

Join Date: September 1, 2006

Recent Posts

Satheesh Ravi:
Hey Smith,

You need to understand one thing. The first application deployed in the server is the ROOT folder which is nothing but the liferay. It has it's own web.xml which redirects the requests to appropriate portlet. Portlet's 'll be initialized once it gets the request to the page that contains the portlet. Check the web.xml in the ROOT folder once. Hope this is useful.


Are you on crack? Portlets get initialized when they are loaded, not when they're first referenced on a page...
Jason Smith
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 19, 2012 10:24 PM
Answer

Jason Smith

Rank: New Member

Posts: 17

Join Date: April 18, 2011

Recent Posts

I understand how liferay forwards the request to the portlets and that part.

But my problem is that something has changed in Liferay 6.1.1 vs 6.1.0 and I dont know where that change is... So I cannot change it back. This used to work with 6.1.0 CE.

I have common WebApplicationContext created with Servlet ContextListener in my own war. This ApplicationContext contains all the common services and caches for all my portlets. And Portlets have their own subContext with their own beans.

I have used this approach for quite a while now without any problems, but I cannot upgrade to 6.1.1 since this doesnt work anymore and I dont know any other way to share an ApplicationContext with all my portlets and servlets in the same war.
David H Nebinger
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 20, 2012 5:28 AM
Answer

David H Nebinger

Community Moderator

Rank: Liferay Legend

Posts: 9468

Join Date: September 1, 2006

Recent Posts

When Liferay hot-deploys your portlets, it massages the web.xml file to insert missing pieces. One of those inserts is for listeners.

I'd check your web.xml for one that you deployed to 6.1.0 and one to 6.1.1. I'm betting you'll see the order of the listeners is different. You may even try copying the one from 6.1.0 to the 6.1.1 version and restart your app container.

If the order is different and the copy from 6.1.0 to 6.1.1 is successful, that would point to a change in the hot deploy service.

If that's the case, you'd have a couple of options:

  1. Patch the 6.1.1 hot deploy service to roll back the listener ordering.
  2. Don't rely on hot deploy, just expand your war in-place so the hot deploy doesn't tamper with your web.xml.
Andrius Kurtinaitis
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 23, 2012 8:33 AM
Answer

Andrius Kurtinaitis

Rank: Junior Member

Posts: 62

Join Date: January 24, 2010

Recent Posts

It is fixed at http://issues.liferay.com/browse/LPS-29103.
Jason Smith
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
August 24, 2012 1:13 AM
Answer

Jason Smith

Rank: New Member

Posts: 17

Join Date: April 18, 2011

Recent Posts

Andrius Kurtinaitis:
It is fixed at http://issues.liferay.com/browse/LPS-29103.


Yes, that fixed it. Thanks for the info.
Philippe Tran
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
October 30, 2012 2:36 AM
Answer

Philippe Tran

Rank: Junior Member

Posts: 34

Join Date: January 22, 2009

Recent Posts

Hi Jason,

Jason Smith:
I have common WebApplicationContext created with Servlet ContextListener in my own war. This ApplicationContext contains all the common services and caches for all my portlets. And Portlets have their own subContext with their own beans.


I have the same problem.

There may be a way to get around this bug: consider the web applicationContext "late" loading as some kind of "lazy loading". You change :

1@Autowired
2private MyBean myBean;


to:

 1@Autowired(required=false)
 2private MyBean myBean;    // may not be set by LR 6.1.1 initially
 3
 4public getMyBean(PortletRequest request)
 5{
 6    if (myBean == null)
 7    {
 8        // lazy loading of Web ApplicationContext bean "myBean" (LR 6.1.1 bug: portlet applicationContext loaded before web applicationContext)
 9        ApplicationContext appContext = PortletApplicationContextUtils.getWebApplicationContext(request.getPortletSession().getPortletContext());
10        if (appContext != null)
11            myBean = (MyBean) appContext.getBean("myBean");
12    }
13    return myBean;
14}


OK, the downside is that you must write some unnecessary code. But the pro is that this let you test on LR 6.1.1.
Marco Beelen (ex-iPROFS)
RE: Portlet is initialized before Servlet Context in 6.1.1 CE
March 18, 2013 2:16 AM
Answer

Marco Beelen (ex-iPROFS)

Rank: New Member

Posts: 4

Join Date: September 26, 2012

Recent Posts

The change in Liferay causing this bug can be found:

http://issues.liferay.com/browse/LPS-27136