Question

Questions

  1. Is there any way to configure pretty-faces programmatically, without using the pretty-config.xml file?
  2. If pretty-config.xml must be used then; is there any way to override/augment its UrlMapppings programmatically?

Problem Context:

In my situation the mapppings have to come from another xml resource and there are some additional logic towards it. So i devised the following solution I implemented a ServletContextAttributeListener where I monitor when the prettyConfig object is added to the servlet context by the PrettyFilter

@Override
public void attributeAdded(ServletContextAttributeEvent event) {
    Object value = event.getValue();
    if(value != null && value instanceof PrettyConfig){
        PrettyConfig prettyConfig = (PrettyConfig)value ;
        try {
            PrettyFacesConfigProvider provider = new PrettyFacesConfigProvider() ;
            PrettyConfig config = provider.loadConfiguration(event.getServletContext());
            prettyConfig.setMappings(config.getMappings());
        } catch (Exception ex) {
            Logger.getLogger(CRSWebListener.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

PrettyFacesConfigProvider class is the implementation that builds my custom mapping configuration. I inspected the updated config and seems all mappings are added properly. But mappings are not having any effect if I take this route. Ofcourse if I put the mapping in the pretty-config.xml it works perfectly.

I am using pretty-faces 3.3.3, jsf 2.2, prime-faces 4.0, tomcat 7

Was it helpful?

Solution

There is an SPI called ConfigurationProvider that allows you to implement a custom solution for providing the PrettyFaces configuration. PrettyFaces uses this SPI internally to provided different mechanisms for configuration like XML or annotations. But you can also use the SPI yourself to implement custom ways of building the configuration.

Just create an implementation of ConfigurationProvider and then add a file called META-INF/services/com.ocpsoft.pretty.faces.spi.ConfigurationProvider to your classpath and add the full class name of your implementation class there (see this example for such a file and the javadocs of ServiceLoader for a descriptions of this plugin mechanism).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top