package org.apache.wiki.ui.stripes; import java.io.IOException; import java.io.InputStream; import java.util.HashSet; import java.util.Properties; import java.util.Set; import net.sourceforge.stripes.action.ActionBean; import net.sourceforge.stripes.config.Configuration; import net.sourceforge.stripes.config.DontAutoLoad; import net.sourceforge.stripes.controller.NameBasedActionResolver; import net.sourceforge.stripes.controller.UrlBinding; import net.sourceforge.stripes.controller.UrlBindingFactory; import org.apache.wiki.api.WikiException; /** * Resolves Stripes ActionBeans in an identical fashion to {@link NameBasedActionResolver}, but allows * UrlBindings to be defined in an external file. */ @DontAutoLoad public class FileBasedActionResolver extends NameBasedActionResolver { private Set> m_processed = new HashSet>(); private Properties m_urlBindings = new Properties(); private static final String URL_BINDINGS = "/WEB-INF/urlpattern.properties"; @Override public void init( Configuration configuration ) throws Exception { // Get the url bindings file m_urlBindings.clear(); InputStream in = configuration.getServletContext().getResourceAsStream( URL_BINDINGS ); if ( in == null ) { throw new IOException( "Resource not returned by servlet context: " + URL_BINDINGS ); } // Load the URL patterns from the config file try { m_urlBindings.load( in ); in.close(); } catch( IOException e ) { throw new WikiException( "Could not find file " + URL_BINDINGS + ". Reason: " + e.getMessage(), e ); } // Initialize the URL bindings factory super.init( configuration ); } /** * */ @Override public String getUrlBinding( Class clazz ) { // See if we've processed the class first. if ( m_processed.contains( clazz ) ) { return super.getUrlBinding( clazz ); } // Have we defined a binding string somewhere? If not, delegate to Stripes String binding = m_urlBindings.getProperty( clazz.getName() ); if ( binding == null ) { return super.getUrlBinding( clazz ); } // Must be a new ActionBean we haven't processed yet, AND we have a binding for it. UrlBinding prototype = UrlBindingFactory.parseUrlBinding( clazz, binding ); UrlBindingFactory.getInstance().addBinding( clazz, prototype ); m_processed.add( clazz ); return prototype.toString(); } }