Platform:
JSF (Mojarra) 2.1.14
I am trying to initialize an application scoped managed bean using a PostConstructApplicationEvent, that is because I have an important constraint which is not to use @PostConstruct annotation since the required build tool compiles using Java 5. For this I have implemented the SystemEventListener interface in this class
public class ApplicationListener implements SystemEventListener {
private final static Logger LOGGER = Logger
.getLogger(ApplicationListener.class.getName());
public boolean isListenerForSource(Object app) {
boolean isApp = (app instanceof Application);
return isApp;
}
public void processEvent(SystemEvent event) throws AbortProcessingException {
try {
if (event instanceof PostConstructApplicationEvent) {
Application app = ((PostConstructApplicationEvent) event).getApplication();
LOGGER.info("PostConstructApplicationEvent is Called");
FacesContext context = FacesContext.getCurrentInstance();
MyBean myBean = ((PostConstructApplicationEvent) event).getApplication().evaluateExpressionGet(context,"#{myBean}", MyBean.class);
myBean.init();
} catch (Exception e) {
FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error while initiliazing app bean", e.getMessage());
LOGGER.error("Error while initiliazing app bean", e);
}
}
}
}
The problem is that I am getting these exceptions:
2012-12-14 12:19:48,822 INFO [ApplicationListener] - PostConstructApplicationEvent is Called
14/12/2012 12:19:48 PM com.sun.faces.config.ConfigureListener contextInitialized
GRAVE: Critical error during deployment:
com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! null
at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:376)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:223)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NullPointerException
at com.sun.faces.application.ApplicationImpl.evaluateExpressionGet(ApplicationImpl.java:426)
at com.myproject.event.ApplicationListener.processEvent(ApplicationListener.java:39)
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106)
at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2169)
at com.sun.faces.application.ApplicationImpl.invokeListenersFor(ApplicationImpl.java:2145)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:303)
at com.sun.faces.config.ConfigManager.publishPostConfigEvent(ConfigManager.java:601)
at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:370)
... 11 more
14/12/2012 12:19:48 PM org.apache.catalina.core.StandardContext listenerStart
GRAVE: Excepción enviando evento inicializado de contexto a instancia de escuchador de clase com.sun.faces.config.ConfigureListener
java.lang.RuntimeException: com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! null
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:290)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4791)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5285)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: com.sun.faces.config.ConfigurationException: CONFIGURATION FAILED! null
at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:376)
at com.sun.faces.config.ConfigureListener.contextInitialized(ConfigureListener.java:223)
... 10 more
Caused by: java.lang.NullPointerException
at com.sun.faces.application.ApplicationImpl.evaluateExpressionGet(ApplicationImpl.java:426)
at com.myproject.event.ApplicationListener.processEvent(ApplicationListener.java:39)
at javax.faces.event.SystemEvent.processListener(SystemEvent.java:106)
at com.sun.faces.application.ApplicationImpl.processListeners(ApplicationImpl.java:2169)
at com.sun.faces.application.ApplicationImpl.invokeListenersFor(ApplicationImpl.java:2145)
at com.sun.faces.application.ApplicationImpl.publishEvent(ApplicationImpl.java:303)
at com.sun.faces.config.ConfigManager.publishPostConfigEvent(ConfigManager.java:601)
at com.sun.faces.config.ConfigManager.initialize(ConfigManager.java:370)
... 11 more
14/12/2012 12:19:48 PM org.apache.catalina.core.StandardContext startInternal
GRAVE: Error listenerStart
14/12/2012 12:19:48 PM org.apache.catalina.core.StandardContext startInternal
GRAVE: Falló en arranque del Contexto [/calibre] debido a errores previos
14/12/2012 12:19:48 PM javax.faces.FactoryFinder$FactoryManager getFactory
GRAVE: La aplicación no se ha inicializado correctamente durante el inicio, no se encuentra la fábrica: javax.faces.application.ApplicationFactory. Attempting to find backup.
14/12/2012 12:19:48 PM com.sun.faces.config.ConfigureListener contextDestroyed
GRAVE: Unexpected exception when attempting to tear down the Mojarra runtime
java.lang.IllegalStateException: Could not find backup for factory javax.faces.application.ApplicationFactory.
at javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:1010)
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:342)
at com.sun.faces.config.InitFacesContext.getApplication(InitFacesContext.java:136)
at com.sun.faces.config.ConfigureListener.contextDestroyed(ConfigureListener.java:328)
at org.apache.catalina.core.StandardContext.listenerStop(StandardContext.java:4831)
at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5478)
at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:160)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Now, for what I have begun to understand of the problem is that the ELContext has been released from the InitFacesContext and therefore it throws a NullPointerException when trying to call the getELContext() method.
So my question is, what am I doing wrong here? Are we supposed not to retrieve programatically Managed Beans during a PostConstructApplicationEvent? If so, why?
Thanks in advance on any help to shed some light into this problem.