package com.westernasset.compliance.util;

import javax.servlet.ServletContext;

import org.jboss.resteasy.core.Dispatcher;
import org.jboss.resteasy.plugins.spring.SpringBeanProcessor;
import org.jboss.resteasy.spi.Registry;
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.springframework.core.PriorityOrdered;
import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.ContextLoader;

public class SpringContextLoaderListener extends org.jboss.resteasy.plugins.spring.SpringContextLoaderListener {
    
    public class SpringContextLoader extends ContextLoader {
        protected void customizeContext(ServletContext servletContext, ConfigurableWebApplicationContext configurableWebApplicationContext)
        {
           super.customizeContext(servletContext, configurableWebApplicationContext);
           
           ResteasyProviderFactory providerFactory = (ResteasyProviderFactory) servletContext.getAttribute(ResteasyProviderFactory.class.getName());
           if (providerFactory == null)
              throw new RuntimeException("RESTeasy Provider Factory is null, do you have the ResteasyBootstrap listener configured?");
    
           Registry registry = (Registry) servletContext.getAttribute(Registry.class.getName());
           if (registry == null)
              throw new RuntimeException("RESTeasy Registry is null, do ou have the ResteasyBootstrap listener configured?");
    
           Dispatcher dispatcher = (Dispatcher) servletContext.getAttribute(Dispatcher.class.getName());
           if (dispatcher == null)
              throw new RuntimeException("RESTeasy Dispatcher is null, do ou have the ResteasyBootstrap listener configured?");
           
           String precedenceString = servletContext.getInitParameter("resteasy.postprocessor.order");
           int precedence = PriorityOrdered.LOWEST_PRECEDENCE;
           if (precedenceString != null) {
               if ("HIGHEST_PRECEDENCE".equals(precedenceString)) {
                   precedence = PriorityOrdered.HIGHEST_PRECEDENCE;
               } else if ("LOWEST_PRECEDENCE".equals(precedenceString)) {
                   precedence = PriorityOrdered.LOWEST_PRECEDENCE;
               } else {
                   try {
                       Integer.valueOf(precedenceString);
                   } catch (Exception e) {
                       throw new RuntimeException("resteasy.postprocessor.order must be an integer or 'HIGHEST_PRECEDENCE' or 'LOWEST_PRECEDENCE'");
                   }
               }
           }
    
           SpringBeanProcessor processor = new SpringBeanProcessor(dispatcher, registry, providerFactory);
           processor.setOrder(precedence);
           configurableWebApplicationContext.addBeanFactoryPostProcessor(processor);
           configurableWebApplicationContext.addApplicationListener(processor);       
        }
    }
    
    protected ContextLoader createContextLoader() {
       return new SpringContextLoader();
    }
}
