protected void initCDIIntegration ( ServletContext servletContext , ExternalContext externalContext ) { // Lookup bean manager and put it into an application scope attribute to // access it later. Remember the trick here is do not call any CDI api // directly, so if no CDI api is on the classpath no exception will be thrown. // Try with servlet context Object beanManager = servletContext . getAttribute ( CDI_SERVLET_CONTEXT_BEAN_MANAGER_ATTRIBUTE ) ; if ( beanManager == null ) { // Use reflection to avoid restricted API in GAE Class icclazz = null ; Method lookupMethod = null ; try { icclazz = ClassUtils . simpleClassForName ( "javax.naming.InitialContext" ) ; if ( icclazz != null ) { lookupMethod = icclazz . getMethod ( "doLookup" , String . class ) ; } } catch ( Throwable t ) { // } if ( lookupMethod != null ) { // Try with JNDI try { // in an application server //beanManager = InitialContext.doLookup("java:comp/BeanManager"); beanManager = lookupMethod . invoke ( icclazz , "java:comp/BeanManager" ) ; } catch ( Exception e ) { // silently ignore } catch ( NoClassDefFoundError e ) { //On Google App Engine, javax.naming.Context is a restricted class. //In that case, NoClassDefFoundError is thrown. stageName needs to be configured //below by context parameter. } if ( beanManager == null ) { try { // in a servlet container //beanManager = InitialContext.doLookup("java:comp/env/BeanManager"); beanManager = lookupMethod . invoke ( icclazz , "java:comp/env/BeanManager" ) ; } catch ( Exception e ) { // silently ignore } catch ( NoClassDefFoundError e ) { //On Google App Engine, javax.naming.Context is a restricted class. //In that case, NoClassDefFoundError is thrown. stageName needs to be configured //below by context parameter. } } } } if ( beanManager != null ) { externalContext . getApplicationMap ( ) . put ( CDI_BEAN_MANAGER_INSTANCE , beanManager ) ; } }
The intention of this method is provide a point where CDI integration is done . Faces Flow and javax . faces . view . ViewScope requires CDI in order to work so this method should set a BeanManager instance on application map under the key oam . cdi . BEAN_MANAGER_INSTANCE . The default implementation look on ServletContext first and then use JNDI .