1 17 package org.apache.servicemix.http; 18 19 import java.io.IOException ; 20 21 import javax.servlet.ServletConfig ; 22 import javax.servlet.ServletException ; 23 import javax.servlet.http.HttpServletRequest ; 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.servicemix.jbi.container.JBIContainer; 27 import org.apache.servicemix.jbi.framework.ComponentMBeanImpl; 28 import org.springframework.context.ApplicationContext; 29 import org.springframework.web.context.support.WebApplicationContextUtils; 30 31 37 public class HttpManagedServlet extends javax.servlet.http.HttpServlet { 38 39 public static final String CONTAINER_PROPERTY = "container"; 40 public static final String CONTAINER_DEFAULT = "jbi"; 41 42 public static final String COMPONENT_PROPERTY = "component"; 43 public static final String COMPONENT_DEFAULT = "servicemix-http"; 44 45 public static final String MAPPING_PROPERTY = "mapping"; 46 47 private HttpProcessor processor; 48 49 public void init(ServletConfig config) throws ServletException { 50 super.init(config); 51 52 ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); 54 55 String containerName = config.getInitParameter(CONTAINER_PROPERTY); 57 if (containerName == null) { 58 containerName = CONTAINER_DEFAULT; 59 } 60 JBIContainer container = (JBIContainer) applicationContext.getBean(containerName); 61 if (container == null) { 62 throw new IllegalStateException ("Unable to find jbi container " + containerName); 63 } 64 String componentName = config.getInitParameter(COMPONENT_PROPERTY); 65 if (componentName == null) { 66 componentName = COMPONENT_DEFAULT; 67 } 68 ComponentMBeanImpl componentMBean = container.getComponent(componentName); 69 if (componentMBean == null) { 70 throw new IllegalStateException ("Unable to find component " + componentName); 71 } 72 if (componentMBean.getComponent() instanceof HttpSpringComponent == false) { 73 throw new IllegalStateException ("The component is not an instance of HttpSpringComponent"); 74 } 75 HttpSpringComponent component = (HttpSpringComponent) componentMBean.getComponent(); 76 String mapping = config.getInitParameter(MAPPING_PROPERTY); 77 if (mapping != null) { 78 component.getConfiguration().setMapping(mapping); 79 } 80 processor = component.getMainProcessor(); 81 } 82 83 protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException , IOException { 84 try { 85 processor.process(request, response); 86 } catch (IOException e) { 87 throw e; 88 } catch (ServletException e) { 89 throw e; 90 } catch (RuntimeException e) { 91 throw e; 92 } catch (Exception e) { 93 throw new ServletException ("Failed to process request: " + e, e); 94 } 95 } 96 } 97 | Popular Tags |