Discussion:
getting SEVERE:Unable to obtain InjectionProvider from init time FacesContext from tomcat and grails
will.woodman
2013-08-22 22:04:31 UTC
Permalink
I've been trying to slowly build my own grails jsf plugin - the current one
doesn't seem to work with latest 2.0 grails builds.

I've been trying to do this from first principles and I have a separate
working JSF-spring project that I hand crafted using eclipse dynamic web
project and maven to handle dependencies.


Having managed that I then went back to my grails plugin and started to
build it up slowly - however I am having error I don't see the hand crafted
dyn web project

under grails you don't have a web.xml to edit - its generated for you - but
you can instrument how the file is generated

I am using GGTS 3.3.0, groovy2.1, and latest jsf 2.2

the error I am getting for embedded tomcat looks like this

Aug 22, 2013 4:21:34 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Aug 22, 2013 4:21:34 PM org.apache.catalina.core.StandardService
startInternal
INFO: Starting service Tomcat
Aug 22, 2013 4:21:34 PM org.apache.catalina.core.StandardEngine
startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.39
Aug 22, 2013 4:21:34 PM org.apache.catalina.startup.ContextConfig
getDefaultWebXmlFragment
INFO: No global web.xml found
Aug 22, 2013 4:21:34 PM org.apache.catalina.core.ApplicationContext log
INFO: Initializing Spring root WebApplicationContext
Aug 22, 2013 4:21:39 PM javax.faces.FactoryFinder$FactoryManager
copyInjectionProviderFromFacesContext
SEVERE: Unable to obtain InjectionProvider from init time FacesContext. Does
this container implement the Mojarra Injection SPI?
Aug 22, 2013 4:21:39 PM javax.faces.FactoryFinder$FactoryManager getFactory
SEVERE: Application was not properly initialized at startup, could not find
Factory: javax.faces.context.FacesContextFactory. Attempting to find backup.
Aug 22, 2013 4:21:39 PM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
java.lang.IllegalStateException: Could not find backup for factory
javax.faces.context.FacesContextFactory.
at
javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:1135)
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:379)
at javax.faces.webapp.FacesServlet.init(FacesServlet.java:350)


what I have done so far is edit the buildConfig script like this

...
grails.servlet.version = "3.0" //generate servlet web-app v3
...

dependencies {
// specify dependencies here under either 'build', 'compile',
'runtime', 'test' or 'provided' scopes eg.

compile "javax.inject:javax.inject:1"
compile "org.glassfish:javax.faces:2.2.2" //faces
compile "com.sun.faces:jsf-api:$grails.jsf.version" //jsf
compile "com.sun.faces:jsf-impl:$grails.jsf.version" //jsf
compile "com.sun.faces:jsf-api:$grails.jsf.version" //jsf
compile "javax.servlet:javax.servlet-api:3.0.1" //not sure its needed
compile "javax.servlet.jsp:javax.servlet.jsp-api:2.2.1"
compile "org.glassfish.web:el-impl:2.2"
compile "javax.el:javax.el-api:2.2.1"
compile "javax.servlet.jsp.jstl:jstl-api:1.2"
compile "com.sun.el:el-ri:1.0" // required for tomat

}

I have manually copied my faces-config from the working jsf-spring project
into grails plugin looking like this

<?xml version="1.0" encoding="UTF-8"?>
<faces-config
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"
version="2.2">


<application>
<el-resolver>
org.springframework.web.jsf.el.SpringBeanFacesELResolver
</el-resolver>
</application>


</faces-config>


and in the plugin descriptor I have edited the doWithWeb closure like this


def doWithWebDescriptor = { xml ->
// TODO Implement additions to web.xml (optional), this event occurs
before

def contextParams = xml.'context-param'[0]
contextParams + {
'context-param' {
'param-name' ('javax.faces.DEFAULT_SUFFIX')
'param-value' ('.xhtml')
}
'context-param' {
'param-name' ('javax.faces.PROJECT_STAGE')
'param-value' ('Development')
}
'context-param' {
'param-name' ('javax.faces.STATE_SAVING_METHOD')
'param-value' ('client')
}
'context-param' {
'param-name' ('javax.faces.FACELETS_REFRESH_PERIOD')
'param-value' ('1')

}
}

def servletsList = xml.servlet
def servlets = servletsList[servletsList.size() -1]
// add the Faces servlet
servlets + {
servlet {
'servlet-name'('Faces Servlet')
'servlet-class'('javax.faces.webapp.FacesServlet')
'load-on-startup'('1')
}
}

def mappingsList = xml.'servlet-mapping'
def mappings = mappingsList[mappingsList.size() - 1]
mappings + {
'servlet-mapping' {
'servlet-name'('Faces Servlet')
'url-pattern'('*.xhtml')
}
}


def listenersList = xml.'listener'
def lastListener = listenersList[listenersList.size() - 1]
lastListener + {
listener { //setup the faces config listener
'listener-class'('com.sun.faces.config.ConfigureListener')
}
/* listener { //effectivellt the same as GrailsContextLoaderListener set
as default

'listener-class'('org.springframework.web.context.ContextLoaderListener')
} */
listener {

'listener-class'('org.springframework.web.context.request.RequestContextListener')
}
}


println xml
}

all of which generates a web.xml like this (using the println xml to
console) - which I think looks ok

<?xml version="1.0" encoding="UTF-8"?><web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
metadata-complete="true" version="3.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<display-name>/JsfPlugin-development-null</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.DEFAULT_SUFFIX</param-name>
<param-value>.xhtml</param-value>
</context-param>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
<param-value>1</param-value>
</context-param>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>JsfPlugin-development-null</param-value>
</context-param>
<filter>
<filter-name>sitemesh</filter-name>

<filter-class>org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter</filter-class>
</filter>
<filter>
<filter-name>charEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>characterEncodingFilter</param-value>
</init-param>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter>
<filter-name>urlMapping</filter-name>

<filter-class>org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>charEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>urlMapping</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<listener>

<listener-class>org.codehaus.groovy.grails.plugins.log4j.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>

<listener-class>org.codehaus.groovy.grails.web.context.GrailsContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<listener>

<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<servlet>
<servlet-name>H2Console</servlet-name>
<servlet-class>org.h2.server.web.WebServlet</servlet-class>
<init-param>
<param-name>-webAllowOthers</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>H2Console</servlet-name>
<url-pattern>/dbconsole/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>grails</servlet-name>

<servlet-class>org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>

<servlet>
<servlet-name>gsp</servlet-name>

<servlet-class>org.codehaus.groovy.grails.web.pages.GroovyPagesServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>grails-errorhandler</servlet-name>

<servlet-class>org.codehaus.groovy.grails.web.servlet.ErrorHandlingServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>gsp</servlet-name>
<url-pattern>*.gsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>grails-errorhandler</servlet-name>
<url-pattern>/grails-errorhandler</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>

<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>

<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.gsp</welcome-file>
</welcome-file-list>
<error-page>
<error-code>500</error-code>
<location>/grails-errorhandler</location>
</error-page>
</web-app>


however when I run this plugin I get the error about the InjectionProvider
when initializing the spring webApplicationContext for the embedded tomcat.

from console log
...
INFO: Initializing Spring root WebApplicationContext
Aug 22, 2013 4:21:39 PM javax.faces.FactoryFinder$FactoryManager
copyInjectionProviderFromFacesContext
SEVERE: Unable to obtain InjectionProvider from init time FacesContext. Does
this container implement the Mojarra Injection SPI?
Aug 22, 2013 4:21:39 PM javax.faces.FactoryFinder$FactoryManager getFactory
SEVERE: Application was not properly initialized at startup, could not find
Factory: javax.faces.context.FacesContextFactory. Attempting to find backup.
Aug 22, 2013 4:21:39 PM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
java.lang.IllegalStateException: Could not find backup for factory
javax.faces.context.FacesContextFactory.
at
javax.faces.FactoryFinder$FactoryManager.getFactory(FactoryFinder.java:1135)
at javax.faces.FactoryFinder.getFactory(FactoryFinder.java:379)
....


I don't get this with the hand crafted jsf-spring sample project (which is
happily linking a facelet page with a bean in the spring context)

what am I missing for grails is it a jar etc, or is it that the packaging
process for the plugin isn't copying certain dependencies into the plugin
war.

If I cant get this working the rest I intend to do falls right here - help
appreciated at this point




--
View this message in context: http://grails.1312388.n4.nabble.com/getting-SEVERE-Unable-to-obtain-InjectionProvider-from-init-time-FacesContext-from-tomcat-and-grails-tp4648290.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
will.woodman
2013-08-22 22:09:16 UTC
Permalink
one last thing -

I tried to add a context param to the web.xml like this

<context-param>
<param-name>com.sun.faces.injectionProvider</param-name>
<param-value>com.softwood.SpringInjectionProvider</param-value>
</context-param>


and added below class to the plugin project (I found this off a google
search on missing Injection provider) - but it made no difference and I
still get the same error

package com.softwood


import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.sun.faces.spi.InjectionProvider;
import com.sun.faces.spi.InjectionProviderException;
import com.sun.faces.vendor.WebContainerInjectionProvider;

public class SpringInjectionProvider implements InjectionProvider {

private static final WebContainerInjectionProvider con = new
WebContainerInjectionProvider();
private static final Log logger =
LogFactory.getLog(SpringInjectionProvider.class);

public void inject(Object managedBean) throws InjectionProviderException {
if(logger.isDebugEnabled()) {
logger.debug("inject: " + managedBean.getClass());
}
con.inject(managedBean);
}

public void invokePostConstruct(Object managedBean)
throws InjectionProviderException {
if(logger.isDebugEnabled()) {
logger.debug("invokePostConstruct: " + managedBean.getClass());
}
ServletContext context =
(ServletContext)FacesContext.getCurrentInstance().getExternalContext().getContext();
if(logger.isDebugEnabled()) {
logger.debug("context: " + context);
}
WebApplicationContext wCtx =
WebApplicationContextUtils.getWebApplicationContext(context);
if(logger.isDebugEnabled()) {
logger.debug("spring context: " + wCtx);
}
if(wCtx != null) {
if(logger.isDebugEnabled()) {
logger.debug("trying to autowire");
}
try {
wCtx.getAutowireCapableBeanFactory().autowireBeanProperties(managedBean,
AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);
} catch(Exception ex) {
logger.error("Autowire exception", ex);
}
}
con.invokePostConstruct(managedBean);
}

public void invokePreDestroy(Object managedBean) throws
InjectionProviderException {
if(logger.isDebugEnabled()) {
logger.debug("invokePreDestroy: " + managedBean.getClass());
}
con.invokePreDestroy(managedBean);
}
}

bit stuck as to how get past this now




--
View this message in context: http://grails.1312388.n4.nabble.com/getting-SEVERE-Unable-to-obtain-InjectionProvider-from-init-time-FacesContext-from-tomcat-and-grails-tp4648290p4648292.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email
will.woodman
2013-08-23 11:35:23 UTC
Permalink
I think at last I have a fix after 1 year of looking - but don't quite
understand how it works - needs someone clever than me to understand what
its doing

essentially I found the clue in this post found on google way in the
background about half way down

related link
<https://java.net/jira/browse/GRIZZLY-1550?page=com.atlassian.jira.plugin.system.issuetabpanels%3Aall-tabpanel>

in essence its to do with the fact that your deploying to embedded tomcat
and the grails projects don't have a web.xml in the project. this confuses
tomcat as there is no war as such and no direct web.xml (which is why my
hand crafted version worked as I added one explicitly)

the fix is to add a contextParam to the dynamically generated web.xml like
this

def doWithWebDescriptor = { xml ->
// TODO Implement additions to web.xml (optional), this event occurs
before

def contextParams = xml.'context-param'[0]
contextParams + {
'context-param' {
'param-name' ('javax.faces.DEFAULT_SUFFIX')
'param-value' ('.xhtml')
}
'context-param' {
'param-name' ('javax.faces.PROJECT_STAGE')
'param-value' ('Development')
}
'context-param' {
'param-name' ('javax.faces.STATE_SAVING_METHOD')
'param-value' ('client')
}
'context-param' {
'param-name' ('javax.faces.FACELETS_REFRESH_PERIOD')
'param-value' ('1')

}
'context-param' {
'param-name' ('com.sun.faces.injectionProvider')
//'param-value' ('com.sun.faces.vendor.WebContainerInjectionProvider')
'param-value' ('com.softwood.SpringInjectionProvider')

}
//use this for embedded tomcat when web.xml is defined programatically
and its not in the project directly
* 'context-param' {
'param-name' ('com.sun.faces.forceLoadConfiguration')
'param-value' ('true')
}*
}....
}


this forceLoadConfiguration somehow fixes the problem and the facesContext
is loaded correctly and the project starts to work

whooa - that's 1 years worth of angst gone in three lines of config. i'll
see if I can get the rest of my plugin to start working now.

yippeee



--
View this message in context: http://grails.1312388.n4.nabble.com/getting-SEVERE-Unable-to-obtain-InjectionProvider-from-init-time-FacesContext-from-tomcat-and-grails-tp4648290p4648306.html
Sent from the Grails - user mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Loading...