This problem may exist in stock JSF as well. Where a component that adds a resource using ViewRoot().addComponentResource() can break when it renders dynamically.
What I found is that:
- UIComponentBase.restoreState(FacesContext context, Object state) casts the passed "state" object into an object array and expecting it to be an object array, but it came across that if the GMap being rendered dynamically then the "state" object happend to be an instance of "StateHolderSaver", that is causing class cast exception.
restoreState(FacesContext context, Object state) {
values = (Object[]) state;
- If the GMap component is part of the initial view, then the JSF invokes the following method when GMap adds resource to the viewRoot, but this method never gets called if the GMap was rendered dynamically.
com.sun.faces.application.view.StateManagementStrategyImpl.handleAddEvent(PostAddToViewEvent event) ;
The above method does the bunch of things including setting the following attribute on the resource being added:
com.sun.faces.application.view.DYNAMIC_COMPONENT
So when com.sun.faces.application.view.StateManagementStrategyImpl.restoreView() gets called, it skips the restoreState on dynamic components.
if (stateObj != null && !target.getAttributes().containsKey(DYNAMIC_COMPONENT))
target.restoreState(context.getFacesContext(), stateObj);
So when the GMap was rendered dynamically, the DYNAMIC_COMPONENT attribute was never set on the added resource and that is why restoreView() thinks that it is not a dynamic component and tries to invoke UIComponnet.restoreState() on it, and restoreState tries to cast the statObject into an object array, but founds it to be a instance of StateHolderSaver.
It come across that GMap component breaks and throw the following exception only when it gets rendered dynamically. If its the GMap component is a part of Initial rendering then it works fine.
javax.faces.FacesException: Unexpected error restoring state for component with id javax_faces_locat
ion_HEAD. Cause: java.lang.ClassCastException: com.sun.faces.application.view.StateHolderSaver.
at com.sun.faces.application.view.StateManagementStrategyImpl$2.visit(StateManagementStrateg
yImpl.java:240)
at com.sun.faces.component.visit.FullVisitContext.invokeVisitCallback(FullVisitContext.java:
147)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1443)
at javax.faces.component.UIComponent.visitTree(UIComponent.java:1454)
at com.sun.faces.application.view.StateManagementStrategyImpl.restoreView(StateManagementStr
ategyImpl.java:224)
at com.sun.faces.application.StateManagerImpl.restoreView(StateManagerImpl.java:177)
at com.sun.faces.application.view.ViewHandlingStrategy.restoreView(ViewHandlingStrategy.java
:131)
at com.sun.faces.application.view.FaceletViewHandlingStrategy.restoreView(FaceletViewHandlin
gStrategy.java:429)
at com.sun.faces.application.view.MultiViewHandler.restoreView(MultiViewHandler.java:143)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:288)
at javax.faces.application.ViewHandlerWrapper.restoreView(ViewHandlerWrapper.java:288)
at com.sun.faces.lifecycle.RestoreViewPhase.execute(RestoreViewPhase.java:199)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.RestoreViewPhase.doPhase(RestoreViewPhase.java:110)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:310)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.j
ava:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.ja
va:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)