The problem with this approach is that there is no faces context available when the phase listener constructor runs. Potentially the listener could have an "isIntialised" flag, and on every call to beforePhase, if the initialised flag is false then do what you describe below. Not elegant though. And as a phase listener needs to be thread-safe, any initialisation process occurring in beforePhase would need synchronisation.

It should be possible to get the Application object directly rather than via a FacesContext:
 Application app = ((ApplicationFactory)
   FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY))
   .getApplication();
I haven't checked whether the Application will have been correctly initialised if it is fetched like this from within the constructor of a PhaseListener. If it is, then this seems better than initialising in beforePhase.

Regardless, this approach means that the phase listener needs to hard-wire the name of an application-scope bean. ecch. Maybe if it simply requires an app-scope bean that has a name matching the fully-qualified classname of the listener that isn't *too* ugly; there is generally only one instance of a PhaseListener class registered.

Errors also get reported only on the first access, not at app startup; not critical but not elegant either.

Cheers,

Simon

Mike Kienenberger wrote:
What about creating a normal application-scoped managed bean to store
your configuration and then manually fetching a reference to it using
a value binding in the phase listener?

To borrow Jeff's example:

              FacesContext context = FacesContext.getCurrentInstance();
              ValueBinding binding =
context.getApplication().createValueBinding("configBean");
              return (ConfigBean)binding.getValue(context);


On 12/7/06, Simon Kitching <[EMAIL PROTECTED]> wrote:
Matt Tyson wrote:
> Can you have a phase listener entry in the config.xml be a managed bean
> reference?
>
> I've got a phase listener that needs a reference to another object to get
> some data and I want to externalize that reference.

Not as far as I know. I would also love this to be possible in order to
pass configuration data to phase listeners but haven't figured out how.

Of course, you do mean an application-scope managed bean, yes? No other
scope makes sense as phase listeners are global for a webapp.

It is possible to create a class that registers itself as a
PhaseListener when an instance is created. However if this is listed as
an application-scope managed bean, then it doesn't get created until
something references it from a page. It would be wonderful if there was
some "lazy-init=false" option for application-scope managed beans to
force them to be created on webapp startup but this doesn't exist AFAIK.

As the app I'm currently working on uses Spring, I declare a Spring
singleton bean which explicitly looks up the Lifecycle, gets the phase
listener list, searches it for an instance of my phase listener type,
then calls setter methods to push the config data defined in the spring
file into the phase listener. Spring singletons are created when spring
is initialised (which is just after JSF is initialised).  This
implementation is not terribly clean or intuitive so if anyone has
alternate suggestions I'd be keen to hear them..

Regards,

Simon


Reply via email to