You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Daniel Zwink <zw...@oio.de> on 2005/06/14 20:19:24 UTC

two instances of my PhaseListener

Hi,

perhaps I don't see the wood for the trees or I missunderstood the 
concept of a phase listener - but I cannot find the reason why my one is 
instanced twice.

faces-config.xml:

----- snip -----
<faces-config>
   <lifecycle>
     <phase-listener>de.oio.jsf.listener.MyPhaseListener</phase-listener>
   </lifecycle>

[...]
----- snap -----

MyPhaseListener.java:

----- snip -----
public class MyPhaseListener implements PhaseListener {

   public MyPhaseListener() {
     System.out.println("DEBUG: MyPhaseListener()");
     System.out.println("DEBUG: HashCode " + this.hashCode());
   }

[...]
----- snap -----

I'm using Tomcat 5.5.9 and MyFaces 1.0.9. When I deploy my war-file the 
two debug-lines are printed twice on sysout with different hashcodes 
which indicates to me that the phase listener is instanced twice, too.

If I request a JSF-Page the beforePhase- and afterPhase-method are also 
called twice for each phase.

Is this normal behaviour? I hope not :-). Any hints?


TIA, Daniel
-- 
Orientation in Objects GmbH
Weinheimerstr. 68
D-68309 Mannheim
http://www.oio.de
Tel +49(0)621-71839-0
Fax. +49(0)621-71839-50

Re: two instances of my PhaseListener

Posted by Martin Marinschek <ma...@gmail.com>.
You are welcome!

regards,

Martin

On 6/15/05, Daniel Zwink <zw...@oio.de> wrote:
> Hi,
> 
> on 15.06.2005 18:02 Martin Marinschek said the following:
> 
> > I think (would need to look it up in the spec, though) that the
> > faces-config.xml is taken automatically...
> 
> You're right: JSF Spec 1.1, section 10.1.3
> 
> Thanks for your help!
> 
> 
> Daniel
> --
> Orientation in Objects GmbH
> Weinheimerstr. 68
> D-68309 Mannheim
> http://www.oio.de
> Tel +49(0)621-71839-0
> Fax. +49(0)621-71839-50
>

Re: two instances of my PhaseListener

Posted by Daniel Zwink <zw...@oio.de>.
Hi,

on 15.06.2005 18:02 Martin Marinschek said the following:

> I think (would need to look it up in the spec, though) that the
> faces-config.xml is taken automatically...

You're right: JSF Spec 1.1, section 10.1.3

Thanks for your help!


Daniel
-- 
Orientation in Objects GmbH
Weinheimerstr. 68
D-68309 Mannheim
http://www.oio.de
Tel +49(0)621-71839-0
Fax. +49(0)621-71839-50

Re: two instances of my PhaseListener

Posted by Martin Marinschek <ma...@gmail.com>.
I think (would need to look it up in the spec, though) that the
faces-config.xml is taken automatically...

regards,

Martin

On 6/15/05, Daniel Zwink <zw...@oio.de> wrote:
> Hi,
> 
> on 15.06.2005 15:37 Martin Marinschek said the following:
> 
> > this is the code from MyFaces, and it shows that the phase listener
> > should only be added once:
> > [...]
> 
> Yep.
> 
> > I shortly looked over the rest of the code, don't see any problems there...
> >
> > Maybe you got two faces-config.xml files in different directories and
> > this is why they are called twice?
> 
> After debugging the faces servlet initialisation (which is done through
> "org.apache.myfaces.webapp.StartupServletContextListener") I found the
> solution:
> 
> During the servlet initialisation the method "configure()" in
> "org.apache.myfaces.config.FacesConfigurator" is called. This method
> calls a number of other methods to read configuration (files).
> Especially the following two methods are important in this case:
> 
>   feedContextSpecifiedConfig();
>   feedWebAppConfig();
> 
> Due to the fact that I declared my faces config file in web.xml as a
> context-param it was read twice because it is read from web.xml through
> "feedContextSpecifiedConfig()" and also hard coded in "feedWebAppConfig()"!
> 
> ----- snip -----
> String systemId = "/WEB-INF/faces-config.xml";
> ----- snap -----
> 
> 
> Is this desired behaviour? I thought I have to declare my faces config
> file in web.xml and didn't know, that it is hard coded.
> 
> 
> Greets, Daniel
> --
> Orientation in Objects GmbH
> Weinheimerstr. 68
> D-68309 Mannheim
> http://www.oio.de
> Tel +49(0)621-71839-0
> Fax. +49(0)621-71839-50
>

Re: two instances of my PhaseListener

Posted by Daniel Zwink <zw...@oio.de>.
Hi,

on 15.06.2005 15:37 Martin Marinschek said the following:

> this is the code from MyFaces, and it shows that the phase listener
> should only be added once:
> [...]

Yep.

> I shortly looked over the rest of the code, don't see any problems there...
> 
> Maybe you got two faces-config.xml files in different directories and
> this is why they are called twice?

After debugging the faces servlet initialisation (which is done through 
"org.apache.myfaces.webapp.StartupServletContextListener") I found the 
solution:

During the servlet initialisation the method "configure()" in 
"org.apache.myfaces.config.FacesConfigurator" is called. This method 
calls a number of other methods to read configuration (files). 
Especially the following two methods are important in this case:

  feedContextSpecifiedConfig();
  feedWebAppConfig();

Due to the fact that I declared my faces config file in web.xml as a 
context-param it was read twice because it is read from web.xml through 
"feedContextSpecifiedConfig()" and also hard coded in "feedWebAppConfig()"!

----- snip -----
String systemId = "/WEB-INF/faces-config.xml";
----- snap -----


Is this desired behaviour? I thought I have to declare my faces config 
file in web.xml and didn't know, that it is hard coded.


Greets, Daniel
-- 
Orientation in Objects GmbH
Weinheimerstr. 68
D-68309 Mannheim
http://www.oio.de
Tel +49(0)621-71839-0
Fax. +49(0)621-71839-50

Re: two instances of my PhaseListener

Posted by Martin Marinschek <ma...@gmail.com>.
this is the code from MyFaces, and it shows that the phase listener
should only be added once:

        // add phase listeners
        for (Iterator iterator =
_dispenser.getLifecyclePhaseListeners(); iterator.hasNext();)
        {
            String listenerClassName = (String) iterator.next();
            try
            {
                lifecycle.addPhaseListener((PhaseListener)
ClassUtils.newInstance(listenerClassName));
            } catch (ClassCastException e)
            {
                log.error("Class " + listenerClassName + " does not
implement PhaseListener");
            }
        }

I shortly looked over the rest of the code, don't see any problems there...

Maybe you got two faces-config.xml files in different directories and
this is why they are called twice?

regards,

Martin

On 6/15/05, Daniel Zwink <zw...@oio.de> wrote:
> Hi,
> 
> on 15.06.2005 00:57 Werner Punz said the following:
> >>
> >> I'm using Tomcat 5.5.9 and MyFaces 1.0.9. When I deploy my war-file
> >> the two debug-lines are printed twice on sysout with different
> >> hashcodes which indicates to me that the phase listener is instanced
> >> twice, too.
> >>
> >> If I request a JSF-Page the beforePhase- and afterPhase-method are
> >> also called twice for each phase.
> >>
> >> Is this normal behaviour? I hope not :-). Any hints?
> >>
> >>
> > did you check your rootId?
> >
> > String rootId = event.getFacesContext().getViewRoot().getViewId();
> >
> > the reason for doing that simply is, that the phase listeners
> > get chained, and basically at every request you run into the chain
> > of phase listeners sending different root ids, the phase listeners
> > are supposed to trigger on the phase (via callback) and on the rootid
> > which is sent into the listener cycle.
> 
> It's the same viewId. I don't think there are two requests (guessing you
> assumed that). I have no redirect and no forwarding - just a "plain"
> faces site with some content from a database (same behaviour if I call a
> simple "Hello World" page).
> 
> As I said, there seem to be two instances 'cause the constructor is
> called twice while deploying my test application to tomcat.
> 
> Any ideas?
> 
> 
> Daniel
> --
> Orientation in Objects GmbH
> Weinheimerstr. 68
> D-68309 Mannheim
> http://www.oio.de
> Tel +49(0)621-71839-0
> Fax. +49(0)621-71839-50
>

Re: two instances of my PhaseListener

Posted by Daniel Zwink <zw...@oio.de>.
Hi,

on 15.06.2005 00:57 Werner Punz said the following:
>>
>> I'm using Tomcat 5.5.9 and MyFaces 1.0.9. When I deploy my war-file 
>> the two debug-lines are printed twice on sysout with different 
>> hashcodes which indicates to me that the phase listener is instanced 
>> twice, too.
>>
>> If I request a JSF-Page the beforePhase- and afterPhase-method are 
>> also called twice for each phase.
>>
>> Is this normal behaviour? I hope not :-). Any hints?
>>
>>
> did you check your rootId?
> 
> String rootId = event.getFacesContext().getViewRoot().getViewId();
> 
> the reason for doing that simply is, that the phase listeners
> get chained, and basically at every request you run into the chain
> of phase listeners sending different root ids, the phase listeners
> are supposed to trigger on the phase (via callback) and on the rootid
> which is sent into the listener cycle.

It's the same viewId. I don't think there are two requests (guessing you 
assumed that). I have no redirect and no forwarding - just a "plain" 
faces site with some content from a database (same behaviour if I call a 
simple "Hello World" page).

As I said, there seem to be two instances 'cause the constructor is 
called twice while deploying my test application to tomcat.

Any ideas?


Daniel
-- 
Orientation in Objects GmbH
Weinheimerstr. 68
D-68309 Mannheim
http://www.oio.de
Tel +49(0)621-71839-0
Fax. +49(0)621-71839-50

Re: two instances of my PhaseListener

Posted by Werner Punz <we...@gmx.at>.
Daniel Zwink wrote:

> 
> I'm using Tomcat 5.5.9 and MyFaces 1.0.9. When I deploy my war-file the 
> two debug-lines are printed twice on sysout with different hashcodes 
> which indicates to me that the phase listener is instanced twice, too.
> 
> If I request a JSF-Page the beforePhase- and afterPhase-method are also 
> called twice for each phase.
> 
> Is this normal behaviour? I hope not :-). Any hints?
> 
> 
did you check your rootId?

String rootId = event.getFacesContext().getViewRoot().getViewId();

the reason for doing that simply is, that the phase listeners
get chained, and basically at every request you run into the chain
of phase listeners sending different root ids, the phase listeners
are supposed to trigger on the phase (via callback) and on the rootid
which is sent into the listener cycle.