SeamFramework.orgCommunity Documentation

Chapter 48. Project Stage Support

48.1. Project Stage Injection
48.2. Restricting Bean Activation

The Seam Faces module provides integration with the JSF project stage system. Beside the injection of the current project stage into beans, Seam Faces also allows to enable specific beans only for certain project stages.

Seam Faces supports the injection of the current project stage directly into CDI managed beans. This allows to implement special behavior for certain project stages very easily.



public class Bean {
    @Inject
    private ProjectStage projectStage;
    public void someMethod() {
        if (projectStage == ProjectStage.Development) {
            /* do something special for development mode here */
        }
    }
}
        

Seam Faces provides a set of annotations that can be used to activate beans only in specific project stages. The following table shows the JSF project stages and the corresponding Seam Faces annotations.

Project StageProjectStage EnumSeam Faces Anntotation
ProductionProjectStage.Production@Production
DevelopmentProjectStage.Development@Development
UnitTestProjectStage.UnitTest@UnitTest
SystemTestProjectStage.SystemTest@SystemTest

To restrict a bean to a project stage just place the correspoding annotation on the class.

The following example shows a bean that logs the beginning and end of each JSF lifecycle phase. As the bean is annotated with @Development, it will only be activated if the application runs in the Development project stage.



@Development
public class PhaseEventLogBean {
    public void before(@Observes @Before PhaseEvent event) {
        log.debug("Before: " + event.getPhaseId());
    }
    
    public void after(@Observes @After PhaseEvent event) {
        log.debug("After: " + event.getPhaseId());
    }
}
        

Seam Faces will automatically detect the project stage that is used for the application. If you want Seam Faces to use a different project stage, you can use one of the following two ways.

The first possibility to change the project stage for Seam Faces is to set the system property org.jboss.seam.faces.PROJECT_STAGE. This option is the most easiest to use but is also very unflexible because the project stage be set for all applications running in a container.

-Dorg.jboss.seam.faces.PROJECT_STAGE=Development

If you need more control over the project stage that is used you can implement the Seam Faces SPI org.jboss.seam.faces.projectstage.ProjectStageDetector. Just implement this interface and add the fully-qualified classname of the class to the file META-INF/services/org.jboss.seam.faces.projectstage.ProjectStageDetector. See the following class for an example:



public class MyProjectStageDetector implements ProjectStageDetector {
    @Override
    public int getPrecedence() {
        return 10;
    }
    @Override
    public ProjectStage getProjectStage() {
    
        if (System.getProperty("user.name").startsWith("dev-")) {
            return ProjectStage.Development;
        }
        
        return ProjectStage.Production;
        
    }
}