1

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Ghasfarost
  • 742
  • 2
  • 8
  • 17
  • 1
    What an overcomplicated approach. The `@PostConstruct` issue makes no sense. It was introduced in Java SE 5. See http://stackoverflow.com/questions/11474882/how-to-configure-a-start-up-managed-bean/11476587#11476587 and http://stackoverflow.com/questions/13378221/how-can-i-initialize-a-java-facesservlet/13378700#13378700 for duplicate questions with a much simpler approach. – BalusC Dec 15 '12 at 20:51
  • Yes, I know, however the reason of not using `@PostConstruct` is that the annotations.jar (Apache Tomcat 6 lib) I was trying to import caused the classic message: "class file has wrong version 50.0, should be 49.0". But, taking some time this weekend i realized that `@PostConstruct` has been around since Java 1.0 so I would think that the issue would not happen using the the correct jar (javaee-api-5) – Ghasfarost Dec 17 '12 at 05:59
  • That "classic message" (not sure why you call it classic if you have apparently no idea what it means) is just caused by running the class on an older Java version than it was compiled with. This particular one means that it's compiled with Java 1.6, but you're trying to run it on Java 1.5. You need to compile it with Java 1.5 or older. You were thus looking in the completely wrong direction for the solution. – BalusC Dec 17 '12 at 10:35

1 Answers1

2

While I can't explain the stack trace you're dealing with currently,

  1. J2SE 5 supports annotations, in fact they were introduced in 5.

  2. I can tell you JSF caters for your use case(eager loading of beans) with a simple annotation parameter that will ensure your @ApplicationScoped bean is instantiated @ startup. Add eager=true to the @ManagedBean annotation as in

      @ManagedBean(eager=true)
    

To get the job done

kolossus
  • 20,559
  • 3
  • 52
  • 104