/** * Build the view. * * @param ctx the {@link FacesContext} for the current request * @param view the {@link UIViewRoot} to populate based of the Facelet template * @throws IOException if an error occurs building the view. */ @Override public void buildView(FacesContext ctx, UIViewRoot view) throws IOException { if (Util.isViewPopulated(ctx, view)) { return; } updateStateSavingType(ctx, view.getViewId()); view.setViewId(view.getViewId()); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.fine("Building View: " + view.getViewId()); } if (faceletFactory == null) { ApplicationAssociate associate = ApplicationAssociate.getInstance(ctx.getExternalContext()); faceletFactory = associate.getFaceletFactory(); assert (faceletFactory != null); } RequestStateManager.set(ctx, RequestStateManager.FACELET_FACTORY, faceletFactory); Facelet f = faceletFactory.getFacelet(view.getViewId()); // populate UIViewRoot f.apply(ctx, view); doPostBuildActions(view); Util.setViewPopulated(ctx, view); }
protected void processAnnotations(Class<? extends Annotation> annotationType) { FacesContext ctx = FacesContext.getCurrentInstance(); ApplicationAssociate associate = ApplicationAssociate.getInstance(ctx.getExternalContext()); AnnotationManager manager = associate.getAnnotationManager(); manager.applyConfigAnnotations( ctx, annotationType, ConfigManager.getAnnotatedClasses(ctx).get(annotationType)); }
NavigationHandlerImpl(ApplicationAssociate associate) { if (associate == null) { throw new NullPointerException(); } else { caseListMap = associate.getNavigationCaseListMappings(); wildCardSet = associate.getNavigationWildCardList(); navigationConfigured = (wildCardSet != null && caseListMap != null); } }
public void registerELResolverAndListenerWithJsp(ServletContext context, boolean reloaded) { if (webConfig.isSet(WebContextInitParameter.ExpressionFactory) || !isJspTwoOne(context)) { // first try to load a factory defined in web.xml if (!installExpressionFactory( context, webConfig.getOptionValue(WebContextInitParameter.ExpressionFactory))) { throw new ConfigurationException( MessageUtils.getExceptionMessageString( MessageUtils.INCORRECT_JSP_VERSION_ID, WebContextInitParameter.ExpressionFactory.getDefaultValue(), WebContextInitParameter.ExpressionFactory.getQualifiedName())); } } else { // JSP 2.1 specific check if (JspFactory.getDefaultFactory().getJspApplicationContext(context) == null) { return; } // register an empty resolver for now. It will be populated after the // first request is serviced. FacesCompositeELResolver compositeELResolverForJsp = new ChainTypeCompositeELResolver(FacesCompositeELResolver.ELResolverChainType.JSP); ApplicationAssociate associate = ApplicationAssociate.getInstance(context); if (associate != null) { associate.setFacesELResolverForJsp(compositeELResolverForJsp); } // get JspApplicationContext. JspApplicationContext jspAppContext = JspFactory.getDefaultFactory().getJspApplicationContext(context); // cache the ExpressionFactory instance in ApplicationAssociate if (associate != null) { associate.setExpressionFactory(jspAppContext.getExpressionFactory()); } // register compositeELResolver with JSP try { jspAppContext.addELResolver(compositeELResolverForJsp); } catch (IllegalStateException e) { ApplicationFactory factory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); Application app = factory.getApplication(); if (app.getProjectStage() != ProjectStage.UnitTest && !reloaded) { throw e; } } // register JSF ELContextListenerImpl with Jsp ELContextListenerImpl elContextListener = new ELContextListenerImpl(); jspAppContext.addELContextListener(elContextListener); } }
private Class<?> loadBeanClass() { if (beanClass == null) { String className = beanInfo.getClassName(); Class<?> clazz = loadClass(className); ApplicationAssociate associate = ApplicationAssociate.getCurrentInstance(); if (!associate.isDevModeEnabled()) { beanClass = clazz; } // validate the bean class is public and has a public // no-arg ctor int classModifiers = clazz.getModifiers(); if (!Modifier.isPublic(classModifiers)) { String message = MessageUtils.getExceptionMessageString( MessageUtils.MANAGED_BEAN_CLASS_IS_NOT_PUBLIC_ERROR_ID, className, beanInfo.getName()); queueMessage(message); } if (Modifier.isInterface(classModifiers) || Modifier.isAbstract(classModifiers)) { String message = MessageUtils.getExceptionMessageString( MessageUtils.MANAGED_BEAN_CLASS_IS_ABSTRACT_ERROR_ID, className, beanInfo.getName()); queueMessage(message); } try { Constructor ctor = clazz.getConstructor(RIConstants.EMPTY_CLASS_ARGS); if (!Modifier.isPublic(ctor.getModifiers())) { String message = MessageUtils.getExceptionMessageString( MessageUtils.MANAGED_BEAN_CLASS_NO_PUBLIC_NOARG_CTOR_ERROR_ID, className, beanInfo.getName()); queueMessage(message); } } catch (NoSuchMethodException nsme) { String message = MessageUtils.getExceptionMessageString( MessageUtils.MANAGED_BEAN_CLASS_NO_PUBLIC_NOARG_CTOR_ERROR_ID, className, beanInfo.getName()); queueMessage(message); } if (!hasMessages()) { // class is ok, scan for annotations this.isInjectible = scanForAnnotations(clazz); } return clazz; } return beanClass; }
public void contextDestroyed(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); InitFacesContext initContext = null; try { initContext = new InitFacesContext(context); if (webAppListener != null) { webAppListener.contextDestroyed(sce); webAppListener = null; } if (webResourcePool != null) { webResourcePool.shutdownNow(); } if (!ConfigManager.getInstance().hasBeenInitialized(context)) { return; } GroovyHelper helper = GroovyHelper.getCurrentInstance(context); if (helper != null) { helper.setClassLoader(); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log( Level.FINE, "ConfigureListener.contextDestroyed({0})", context.getServletContextName()); } ELContext elctx = new ELContextImpl(initContext.getApplication().getELResolver()); elctx.putContext(FacesContext.class, initContext); initContext.setELContext(elctx); Application app = initContext.getApplication(); app.publishEvent(initContext, PreDestroyApplicationEvent.class, Application.class, app); Util.setNonFacesContextApplicationMap(null); } catch (Exception e) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log( Level.SEVERE, "Unexpected exception when attempting to tear down the Mojarra runtime", e); } } finally { ApplicationAssociate.clearInstance(initContext.getExternalContext()); ApplicationAssociate.setCurrentInstance(null); com.sun.faces.application.ApplicationImpl.clearInstance(initContext.getExternalContext()); com.sun.faces.application.InjectionApplicationFactory.clearInstance( initContext.getExternalContext()); // Release the initialization mark on this web application ConfigManager.getInstance().destory(context); if (initContext != null) { initContext.release(); } ReflectionUtils.clearCache(Thread.currentThread().getContextClassLoader()); WebConfiguration.clear(context); } }
public void push(FacesContext ctx) { if (namedEvents != null) { ApplicationAssociate associate = ApplicationAssociate.getInstance(ctx.getExternalContext()); if (associate != null) { NamedEventManager nem = associate.getNamedEventManager(); for (Map.Entry<Class<?>, Annotation> entry : namedEvents.entrySet()) { process(nem, entry.getKey(), entry.getValue()); } } } }
/** * @param ctx the <code>FacesContext</code> for the current request * @return <code>StateContext</code> for this request */ public static StateContext getStateContext(FacesContext ctx) { StateContext stateCtx = (StateContext) ctx.getAttributes().get(KEY); if (stateCtx == null) { ApplicationAssociate associate = ApplicationAssociate.getCurrentInstance(); ApplicationStateInfo info = associate.getApplicationStateInfo(); stateCtx = new StateContext(info); ctx.getAttributes().put(KEY, stateCtx); } return stateCtx; }
/** * @see ConfigProcessor#process(javax.servlet.ServletContext,com.sun.faces.config.DocumentInfo[]) */ public void process(ServletContext sc, DocumentInfo[] documentInfos) throws Exception { // process annotated managed beans first as managed beans configured // via config files take precedence processAnnotations(ManagedBean.class); BeanManager beanManager = ApplicationAssociate.getInstance(sc).getBeanManager(); for (int i = 0; i < documentInfos.length; i++) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log( Level.FINE, MessageFormat.format( "Processing managed-bean elements for document: ''{0}''", documentInfos[i].getSourceURL())); } Document document = documentInfos[i].getDocument(); String namespace = document.getDocumentElement().getNamespaceURI(); NodeList managedBeans = document.getDocumentElement().getElementsByTagNameNS(namespace, MANAGED_BEAN); if (managedBeans != null && managedBeans.getLength() > 0) { for (int m = 0, size = managedBeans.getLength(); m < size; m++) { addManagedBean(beanManager, managedBeans.item(m)); } } } beanManager.preProcessesBeans(); invokeNext(sc, documentInfos); }
public void testMapTypeProperty() throws Exception { Map<String, String> entry = new HashMap(1, 1.0f); entry.put("name", "23"); ManagedBeanInfo.MapEntry mapEntry = new ManagedBeanInfo.MapEntry("java.lang.String", "java.lang.Integer", entry); List<ManagedBeanInfo.ManagedProperty> properties = new ArrayList<ManagedBeanInfo.ManagedProperty>(1); ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty("mapProperty", null, null, mapEntry, null); properties.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "request", null, null, properties, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); // make sure bean instantiated properly. Get property back from bean. HashMap mapProperty = (HashMap) testBean.getMapProperty(); assertTrue(mapProperty.get("name") instanceof Integer); Integer integer = new Integer("23"); assertTrue(mapProperty.get("name").equals(integer)); }
// Test managed bean public void testNoProperty() throws Exception { // Testing with no properties set ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "session", null, null, null, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); assertNotNull(beanManager.create(beanName, getFacesContext())); BeanBuilder builder = beanManager.getBuilder(beanName); assertTrue(builder.getScope() == ELUtils.Scope.SESSION); }
/** * This test verifies that if the variable resolver does not find a managed bean it tries to * instantiate it if it was added to the application's managed bean factory list. */ public void testManagedBean() throws Exception { String beanName = "com.sun.faces.TestBean"; ManagedBeanInfo beanInfo = new ManagedBeanInfo(beanName, beanName, "session", null, null, null, null); ApplicationFactory aFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); ApplicationImpl application = (ApplicationImpl) aFactory.getApplication(); ApplicationAssociate associate = ApplicationAssociate.getCurrentInstance(); BeanManager manager = associate.getBeanManager(); manager.register(beanInfo); VariableResolver variableResolver = application.getVariableResolver(); Object result = variableResolver.resolveVariable(getFacesContext(), beanName); assertTrue(result instanceof TestBean); }
/** * This constructor uses the current <code>Application</code> instance to obtain the navigation * mappings used to make navigational decisions. */ public NavigationHandlerImpl() { super(); if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Created NavigationHandler instance "); } // if the user is using the decorator pattern, this would cause // our ApplicationAssociate to be created, if it isn't already // created. ApplicationFactory aFactory = (ApplicationFactory) FactoryFinder.getFactory(FactoryFinder.APPLICATION_FACTORY); aFactory.getApplication(); ApplicationAssociate associate = ApplicationAssociate.getInstance(FacesContext.getCurrentInstance().getExternalContext()); if (associate != null) { caseListMap = associate.getNavigationCaseListMappings(); wildCardSet = associate.getNavigationWildCardList(); navigationConfigured = (wildCardSet != null && caseListMap != null); development = associate.isDevModeEnabled(); } }
private boolean installExpressionFactory(ServletContext sc, String elFactoryType) { if (elFactoryType == null) { return false; } try { ExpressionFactory factory = (ExpressionFactory) Util.loadClass(elFactoryType, this).newInstance(); ApplicationAssociate associate = ApplicationAssociate.getInstance(sc); if (associate != null) { associate.setExpressionFactory(factory); } return true; } catch (Exception e) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.severe( MessageFormat.format("Unable to instantiate ExpressionFactory ''{0}''", elFactoryType)); } return false; } }
public void testSimpleNumericProperty() throws Exception { // If a property value is "" ensure numeric properties // are set to 0. List<ManagedBeanInfo.ManagedProperty> list = new ArrayList<ManagedBeanInfo.ManagedProperty>(1); boolean testBoolean = true; ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty( "boolProp", null, Boolean.toString(testBoolean), null, null); list.add(property); property = new ManagedBeanInfo.ManagedProperty("byteProp", null, "", null, null); list.add(property); property = new ManagedBeanInfo.ManagedProperty("charProp", null, "", null, null); list.add(property); property = new ManagedBeanInfo.ManagedProperty("doubleProp", null, "", null, null); list.add(property); property = new ManagedBeanInfo.ManagedProperty("floatProp", null, "", null, null); list.add(property); property = new ManagedBeanInfo.ManagedProperty("intProp", null, "", null, null); list.add(property); property = new ManagedBeanInfo.ManagedProperty("longProp", null, "", null, null); list.add(property); property = new ManagedBeanInfo.ManagedProperty("shortProp", null, "", null, null); list.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "session", null, null, list, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); assertTrue(testBean.getByteProp() == 0); assertTrue(testBean.getCharProp() == 0); assertTrue(testBean.getDoubleProp() == 0); assertTrue(testBean.getFloatProp() == 0); assertTrue(testBean.getIntProp() == 0); assertTrue(testBean.getLongProp() == 0); assertTrue(testBean.getShortProp() == 0); }
public static ApplicationAssociate getCurrentInstance() { ApplicationAssociate associate = instance.get(); if (associate == null) { // Fallback to ExternalContext lookup FacesContext fc = FacesContext.getCurrentInstance(); if (fc != null) { ExternalContext extContext = fc.getExternalContext(); if (extContext != null) { return ApplicationAssociate.getInstance(extContext); } } } return associate; }
public void testNoneScope() throws Exception { // Testing value ref scope TestBean testBean = new TestBean(); testBean.setOne("one"); boolean exceptionThrown = false; // valueref in request scope // managed bean in none scope // this should fail getFacesContext().getExternalContext().getRequestMap().put("TestRefBean", testBean); ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty("one", null, "#{TestRefBean.one}", null, null); List<ManagedBeanInfo.ManagedProperty> list = new ArrayList<ManagedBeanInfo.ManagedProperty>(1); list.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "none", null, null, list, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); exceptionThrown = false; try { beanManager.create(beanName, getFacesContext()); fail("Should have thrown FacesException"); } catch (FacesException ex) { exceptionThrown = true; } assertTrue(exceptionThrown); // cleanup getFacesContext().getExternalContext().getRequestMap().remove("TestRefBean"); // valueref in none scope // managed bean in none scope // this should pass ValueExpression valueExpression1 = ELUtils.createValueExpression("#{testBean.customerBean.name}"); exceptionThrown = false; try { valueExpression1.getValue(getFacesContext().getELContext()); } catch (FacesException ex) { exceptionThrown = true; } assertTrue(!exceptionThrown); }
public void testSimpleProperty() throws Exception { ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty("one", null, "one", null, null); List<ManagedBeanInfo.ManagedProperty> list = new ArrayList<ManagedBeanInfo.ManagedProperty>(1); list.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "session", null, null, list, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); // make sure bean instantiated properly. Get property back from bean. assertTrue(testBean.getOne().equals("one")); // make sure scope is stored properly BeanBuilder builder = beanManager.getBuilder(beanName); assertTrue(builder.getScope() == ELUtils.Scope.SESSION); }
public void testValueRefProperty() throws Exception { TestBean testBean = new TestBean(); testBean.setOne("one"); getFacesContext().getExternalContext().getSessionMap().put("TestRefBean", testBean); ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty("one", null, "#{TestRefBean.one}", null, null); List<ManagedBeanInfo.ManagedProperty> list = new ArrayList<ManagedBeanInfo.ManagedProperty>(1); list.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "session", null, null, list, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); // make sure bean instantiated properly. Get property back from bean. assertTrue(testBean.getOne().equals("one")); }
public void testIndexProperty() throws Exception { List<String> values = new ArrayList<String>(2); values.add("foo"); values.add("bar"); ManagedBeanInfo.ListEntry listEntry = new ManagedBeanInfo.ListEntry(null, values); List<ManagedBeanInfo.ManagedProperty> properties = new ArrayList<ManagedBeanInfo.ManagedProperty>(2); ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty("indexProperties", null, null, null, listEntry); properties.add(property); property = new ManagedBeanInfo.ManagedProperty("indexPropertiesNull", null, null, null, listEntry); properties.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "request", null, null, properties, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); // make sure bean instantiated properly. Get property back from bean. ArrayList props = (ArrayList) testBean.getIndexProperties(); assertTrue(props.get(5).equals("foo")); assertTrue(props.get(6).equals("bar")); // setter shouldn't be called if bean getter returns List assertTrue(!testBean.getListSetterCalled()); // setter should be called if bean getter returned null assertTrue(testBean.getListNullSetterCalled()); // make sure scope is stored properly BeanBuilder builder = beanManager.getBuilder(beanName); assertTrue(builder.getScope() == ELUtils.Scope.REQUEST); }
public void testIsInjectable() throws Exception { ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "session", null, null, null, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); BeanBuilder builder = beanManager.getBuilder(beanName); Boolean isInjectable = (Boolean) TestingUtil.invokePrivateMethod( "scanForAnnotations", new Class[] {Class.class}, new Object[] {TestBean.class}, BeanBuilder.class, builder); assertTrue(!isInjectable); bean = new ManagedBeanInfo( beanName, "com.sun.faces.config.TestManagedBeanFactory$InjectionBean", "request", null, null, null, null); beanManager.register(bean); isInjectable = (Boolean) TestingUtil.invokePrivateMethod( "scanForAnnotations", new Class[] {Class.class}, new Object[] {InjectionBean.class}, BeanBuilder.class, builder); assertTrue(isInjectable); }
public void testMapProperty() throws Exception { Map<String, String> entry = new HashMap(1, 1.0f); entry.put("name", "Justyna"); ManagedBeanInfo.MapEntry mapEntry = new ManagedBeanInfo.MapEntry(null, null, entry); List<ManagedBeanInfo.ManagedProperty> properties = new ArrayList<ManagedBeanInfo.ManagedProperty>(2); ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty("mapProperty", null, null, mapEntry, null); properties.add(property); property = new ManagedBeanInfo.ManagedProperty("mapPropertyNull", null, null, mapEntry, null); properties.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "request", null, null, properties, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); // make sure bean instantiated properly. Get property back from bean. HashMap mapProperty = (HashMap) testBean.getMapProperty(); assertTrue(((String) mapProperty.get("name")).equals("Justyna")); // setter shouldn't be called if bean getter returns Map assertTrue(!testBean.getMapPropertySetterCalled()); // setter should be called if bean getter returned null assertTrue(testBean.getMapPropertyNullSetterCalled()); // make sure scope is stored properly BeanBuilder builder = beanManager.getBuilder(beanName); assertTrue(builder.getScope() == ELUtils.Scope.REQUEST); }
public void testIndexTypeProperty() throws Exception { List<String> val = new ArrayList<String>(1); val.add("23"); ManagedBeanInfo.ListEntry listEntry = new ManagedBeanInfo.ListEntry("java.lang.Integer", val); ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty( "indexIntegerProperties", "java.lang.Integer", null, null, listEntry); List<ManagedBeanInfo.ManagedProperty> list = new ArrayList<ManagedBeanInfo.ManagedProperty>(1); list.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "session", null, null, list, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); // make sure bean instantiated properly. Get property back from bean. ArrayList properties = (ArrayList) testBean.getIndexIntegerProperties(); assertTrue(properties.get(1) instanceof Integer); Integer integer = new Integer("23"); assertTrue(properties.get(2).equals(integer)); }
public void testExceptions() { boolean thrown; // 1. Verify NullPointer exception which occurs when attempting // to set a null ActionListener // thrown = false; try { application.setActionListener(null); } catch (NullPointerException e) { thrown = true; } assertTrue(thrown); // 3. Verify NullPointer exception which occurs when attempting // to set a null NavigationHandler // thrown = false; try { application.setNavigationHandler(null); } catch (NullPointerException e) { thrown = true; } assertTrue(thrown); // 4. Verify NPE occurs when attempting to set // a null PropertyResolver thrown = false; try { application.setPropertyResolver(null); } catch (NullPointerException npe) { thrown = true; } assertTrue(thrown); // 5. Verify NPE occurs when attempting to set // a null VariableResolver thrown = false; try { application.setVariableResolver(null); } catch (NullPointerException npe) { thrown = true; } assertTrue(thrown); // 5. Verify ISE occurs when attempting to set // a VariableResolver after a request has been processed ApplicationAssociate associate = ApplicationAssociate.getInstance(getFacesContext().getExternalContext()); associate.setRequestServiced(); thrown = false; try { application.setVariableResolver(application.getVariableResolver()); } catch (IllegalStateException e) { thrown = true; } assertTrue(thrown); // 6. Verify ISE occurs when attempting to set // a PropertyResolver after a request has been processed thrown = false; try { application.setPropertyResolver(application.getPropertyResolver()); } catch (IllegalStateException e) { thrown = true; } assertTrue(thrown); // 7. Verify NullPointer exception which occurs when attempting // to get a ValueBinding with a null ref // thrown = false; try { application.createValueBinding(null); } catch (Exception e) { thrown = true; } assertTrue(thrown); // 8.Verify NullPointerException occurs when attempting to pass a // null VariableResolver // thrown = false; try { application.setVariableResolver(null); } catch (NullPointerException e) { thrown = true; } assertTrue(thrown); // 9. Verify NullPointer exception which occurs when attempting // to set a null StateManager // thrown = false; try { application.setStateManager(null); } catch (NullPointerException e) { thrown = true; } assertTrue(thrown); thrown = false; try { application.createValueBinding("improperexpression"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("improper expression"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("improper\texpression"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("improper\rexpression"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("improper\nexpression"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("#improperexpression"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("#{improperexpression"); } catch (ReferenceSyntaxException e) { thrown = true; } assertTrue(thrown); thrown = false; try { application.createValueBinding("improperexpression}"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("{improperexpression}"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("improperexpression}#"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("#{proper[\"a key\"]}"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); try { application.createValueBinding("#{proper[\"a { } key\"]}"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); try { application.createValueBinding("bean.a{indentifer"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("bean['invalid'"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("bean[[\"invalid\"]].foo"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); thrown = false; try { application.createValueBinding("#{bean[\"[a\"]}"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); try { application.createValueBinding("#{bean[\".a\"]}"); } catch (ReferenceSyntaxException e) { thrown = true; } assertFalse(thrown); }
public void testPrimitiveProperty() throws Exception { List<ManagedBeanInfo.ManagedProperty> list = new ArrayList<ManagedBeanInfo.ManagedProperty>(1); boolean testBoolean = true; ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty( "boolProp", null, Boolean.toString(testBoolean), null, null); list.add(property); byte testByte = 100; property = new ManagedBeanInfo.ManagedProperty( "byteProp", null, Byte.valueOf(testByte).toString(), null, null); list.add(property); char testChar = 'z'; property = new ManagedBeanInfo.ManagedProperty( "charProp", null, Character.valueOf(testChar).toString(), null, null); list.add(property); double testDouble = 11.278D; property = new ManagedBeanInfo.ManagedProperty( "doubleProp", null, Double.valueOf(testDouble).toString(), null, null); list.add(property); float testFloat = 45.789F; property = new ManagedBeanInfo.ManagedProperty( "floatProp", null, Float.valueOf(testFloat).toString(), null, null); list.add(property); int testInt = 42; property = new ManagedBeanInfo.ManagedProperty( "intProp", null, Integer.valueOf(testInt).toString(), null, null); list.add(property); long testLong = 3147893289L; property = new ManagedBeanInfo.ManagedProperty( "longProp", null, Long.valueOf(testLong).toString(), null, null); list.add(property); short testShort = 25432; property = new ManagedBeanInfo.ManagedProperty( "shortProp", null, Short.valueOf(testShort).toString(), null, null); list.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "session", null, null, list, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); // make sure bean instantiated properly. Get property back from bean. assertTrue(testBean.getBoolProp() == testBoolean); assertTrue(testBean.getByteProp() == testByte); assertTrue(testBean.getCharProp() == testChar); assertTrue(testBean.getDoubleProp() == testDouble); assertTrue(testBean.getFloatProp() == testFloat); assertTrue(testBean.getIntProp() == testInt); assertTrue(testBean.getLongProp() == testLong); assertTrue(testBean.getShortProp() == testShort); // make sure scope is stored properly BeanBuilder builder = beanManager.getBuilder(beanName); assertTrue(builder.getScope() == ELUtils.Scope.SESSION); }
/** * This method will be invoked {@link WebConfigResourceMonitor} when changes to any of the * faces-config.xml files included in WEB-INF are modified. */ private void reload(ServletContext sc) { if (LOGGER.isLoggable(Level.INFO)) { LOGGER.log( Level.INFO, "Reloading JSF configuration for context {0}", getServletContextIdentifier(sc)); } GroovyHelper helper = GroovyHelper.getCurrentInstance(); if (helper != null) { helper.setClassLoader(); } // tear down the application try { // this will only be true in the automated test usage scenario if (null != webAppListener) { List<HttpSession> sessions = webAppListener.getActiveSessions(); if (sessions != null) { for (HttpSession session : sessions) { if (LOGGER.isLoggable(Level.INFO)) { LOGGER.log(Level.INFO, "Invalidating Session {0}", session.getId()); } session.invalidate(); } } } ApplicationAssociate associate = ApplicationAssociate.getInstance(sc); if (associate != null) { BeanManager manager = associate.getBeanManager(); for (Map.Entry<String, BeanBuilder> entry : manager.getRegisteredBeans().entrySet()) { String name = entry.getKey(); BeanBuilder bean = entry.getValue(); if (ELUtils.Scope.APPLICATION.toString().equals(bean.getScope())) { if (LOGGER.isLoggable(Level.INFO)) { LOGGER.log(Level.INFO, "Removing application scoped managed bean: {0}", name); } sc.removeAttribute(name); } } } // Release any allocated application resources FactoryFinder.releaseFactories(); } catch (Exception e) { e.printStackTrace(); } finally { FacesContext initContext = new InitFacesContext(sc); ApplicationAssociate.clearInstance(initContext.getExternalContext()); ApplicationAssociate.setCurrentInstance(null); // Release the initialization mark on this web application ConfigManager.getInstance().destory(sc); initContext.release(); ReflectionUtils.clearCache(Thread.currentThread().getContextClassLoader()); WebConfiguration.clear(sc); } // bring the application back up, avoid re-registration of certain JSP // artifacts. No verification will be performed either to make this // light weight. // init a new WebAppLifecycleListener so that the cached ApplicationAssociate // is removed. webAppListener = new WebappLifecycleListener(sc); FacesContext initContext = new InitFacesContext(sc); ReflectionUtils.initCache(Thread.currentThread().getContextClassLoader()); try { ConfigManager configManager = ConfigManager.getInstance(); configManager.initialize(sc); registerELResolverAndListenerWithJsp(sc, true); ApplicationAssociate associate = ApplicationAssociate.getInstance(sc); if (associate != null) { Boolean errorPagePresent = (Boolean) sc.getAttribute(RIConstants.ERROR_PAGE_PRESENT_KEY_NAME); if (null != errorPagePresent) { associate.setErrorPagePresent(errorPagePresent); associate.setContextName(getServletContextIdentifier(sc)); } } } catch (Exception e) { e.printStackTrace(); } finally { initContext.release(); } if (LOGGER.isLoggable(Level.INFO)) { LOGGER.log(Level.INFO, "Reload complete.", getServletContextIdentifier(sc)); } }
public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); Timer timer = Timer.getInstance(); if (timer != null) { timer.startTiming(); } if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log( Level.FINE, MessageFormat.format( "ConfigureListener.contextInitialized({0})", getServletContextIdentifier(context))); } webConfig = WebConfiguration.getInstance(context); ConfigManager configManager = ConfigManager.getInstance(); if (configManager.hasBeenInitialized(context)) { return; } // Check to see if the FacesServlet is present in the // web.xml. If it is, perform faces configuration as normal, // otherwise, simply return. Object mappingsAdded = context.getAttribute(RIConstants.FACES_INITIALIZER_MAPPINGS_ADDED); if (mappingsAdded != null) { context.removeAttribute(RIConstants.FACES_INITIALIZER_MAPPINGS_ADDED); } WebXmlProcessor webXmlProcessor = new WebXmlProcessor(context); if (mappingsAdded == null) { if (!webXmlProcessor.isFacesServletPresent()) { if (!webConfig.isOptionEnabled(ForceLoadFacesConfigFiles)) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log( Level.FINE, "No FacesServlet found in deployment descriptor - bypassing configuration"); } WebConfiguration.clear(context); return; } } else { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log( Level.FINE, "FacesServlet found in deployment descriptor - processing configuration."); } } } // bootstrap of faces required webAppListener = new WebappLifecycleListener(context); webAppListener.contextInitialized(sce); InitFacesContext initContext = new InitFacesContext(context); ReflectionUtils.initCache(Thread.currentThread().getContextClassLoader()); Throwable caughtThrowable = null; try { if (LOGGER.isLoggable(Level.INFO)) { LOGGER.log(Level.INFO, "jsf.config.listener.version", getServletContextIdentifier(context)); } // see if we need to disable our TLValidator Util.setHtmlTLVActive(webConfig.isOptionEnabled(EnableHtmlTagLibraryValidator)); if (webConfig.isOptionEnabled(VerifyFacesConfigObjects)) { if (LOGGER.isLoggable(Level.WARNING)) { LOGGER.warning("jsf.config.verifyobjects.development_only"); } // if we're verifying, force bean validation to occur at startup as well webConfig.overrideContextInitParameter(EnableLazyBeanValidation, false); Verifier.setCurrentInstance(new Verifier()); } initScripting(); configManager.initialize(context); if (shouldInitConfigMonitoring()) { initConfigMonitoring(context); } // Step 7, verify that all the configured factories are available // and optionall that configured objects can be created. Verifier v = Verifier.getCurrentInstance(); if (v != null && !v.isApplicationValid()) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.severe("jsf.config.verifyobjects.failures_detected"); StringBuilder sb = new StringBuilder(128); for (String m : v.getMessages()) { sb.append(m).append('\n'); } LOGGER.severe(sb.toString()); } } registerELResolverAndListenerWithJsp(context, false); ELContext elctx = new ELContextImpl(initContext.getApplication().getELResolver()); elctx.putContext(FacesContext.class, initContext); initContext.setELContext(elctx); ApplicationAssociate associate = ApplicationAssociate.getInstance(context); if (associate != null) { associate.setContextName(getServletContextIdentifier(context)); BeanManager manager = associate.getBeanManager(); List<String> eagerBeans = manager.getEagerBeanNames(); if (!eagerBeans.isEmpty()) { for (String name : eagerBeans) { manager.create(name, initContext); } } boolean isErrorPagePresent = webXmlProcessor.isErrorPagePresent(); associate.setErrorPagePresent(isErrorPagePresent); context.setAttribute(RIConstants.ERROR_PAGE_PRESENT_KEY_NAME, isErrorPagePresent); } Application app = initContext.getApplication(); app.subscribeToEvent(PostConstructViewMapEvent.class, UIViewRoot.class, webAppListener); app.subscribeToEvent(PreDestroyViewMapEvent.class, UIViewRoot.class, webAppListener); webConfig.doPostBringupActions(); } catch (Throwable t) { if (LOGGER.isLoggable(Level.SEVERE)) { LOGGER.log(Level.SEVERE, "Critical error during deployment: ", t); } caughtThrowable = t; } finally { Verifier.setCurrentInstance(null); initContext.release(); if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log(Level.FINE, "jsf.config.listener.version.complete"); } if (timer != null) { timer.stopTiming(); timer.logResult("Initialization of context " + getServletContextIdentifier(context)); } if (null != caughtThrowable) { throw new RuntimeException(caughtThrowable); } } }
public void testValueRefScope() throws Exception { // Testing value ref scope TestBean testBean = new TestBean(); testBean.setOne("one"); boolean exceptionThrown = false; // testing with: // valueref in application scope // managed bean in session scope getFacesContext().getExternalContext().getApplicationMap().put("TestRefBean", testBean); ManagedBeanInfo.ManagedProperty property = new ManagedBeanInfo.ManagedProperty("one", null, "#{TestRefBean.one}", null, null); List<ManagedBeanInfo.ManagedProperty> list = new ArrayList<ManagedBeanInfo.ManagedProperty>(1); list.add(property); ManagedBeanInfo bean = new ManagedBeanInfo(beanName, beanName, "session", null, null, list, null); BeanManager beanManager = ApplicationAssociate.getCurrentInstance().getBeanManager(); beanManager.register(bean); // testing with a property set assertNotNull(testBean = (TestBean) beanManager.create(beanName, getFacesContext())); // make sure bean instantiated properly. Get property back from bean. assertTrue(testBean.getOne().equals("one")); // testing with: // valueref in request scope // managed bean in application scope getFacesContext().getExternalContext().getApplicationMap().remove("TestRefBean"); getFacesContext().getExternalContext().getRequestMap().put("TestRefBean", testBean); bean = new ManagedBeanInfo(beanName, beanName, "application", null, null, list, null); beanManager.register(bean); exceptionThrown = false; try { // testing with a property set beanManager.create(beanName, getFacesContext()); fail("Should have thrown FacesException"); } catch (FacesException ex) { exceptionThrown = true; } assertTrue(exceptionThrown); // cleanup getFacesContext().getExternalContext().getRequestMap().remove("TestRefBean"); // testing with: // valueref in session scope // managed bean in no scope getFacesContext().getExternalContext().getSessionMap().put("TestRefBean", testBean); bean = new ManagedBeanInfo(beanName, beanName, "none", null, null, list, null); beanManager.register(bean); exceptionThrown = false; try { beanManager.create(beanName, getFacesContext()); fail("Should have thrown FacesException"); } catch (FacesException ex) { exceptionThrown = true; } assertTrue(exceptionThrown); }