KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > http > HttpManagedServlet


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.http;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletConfig JavaDoc;
22 import javax.servlet.ServletException JavaDoc;
23 import javax.servlet.http.HttpServletRequest JavaDoc;
24 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
32  * This servlet is meant to be used when embedding ServiceMix in a web application
33  * so that so servicemix-http component will reuse the web container.
34  *
35  * @author gnodet
36  */

37 public class HttpManagedServlet extends javax.servlet.http.HttpServlet JavaDoc {
38
39     public static final String JavaDoc CONTAINER_PROPERTY = "container";
40     public static final String JavaDoc CONTAINER_DEFAULT = "jbi";
41     
42     public static final String JavaDoc COMPONENT_PROPERTY = "component";
43     public static final String JavaDoc COMPONENT_DEFAULT = "servicemix-http";
44     
45     public static final String JavaDoc MAPPING_PROPERTY = "mapping";
46     
47     private HttpProcessor processor;
48     
49     public void init(ServletConfig JavaDoc config) throws ServletException JavaDoc {
50         super.init(config);
51         
52         // Retrieve spring application context
53
ApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
54         
55         // Retrieve
56
String JavaDoc 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 JavaDoc("Unable to find jbi container " + containerName);
63         }
64         String JavaDoc 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 JavaDoc("Unable to find component " + componentName);
71         }
72         if (componentMBean.getComponent() instanceof HttpSpringComponent == false) {
73             throw new IllegalStateException JavaDoc("The component is not an instance of HttpSpringComponent");
74         }
75         HttpSpringComponent component = (HttpSpringComponent) componentMBean.getComponent();
76         String JavaDoc 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 JavaDoc request, HttpServletResponse JavaDoc response) throws ServletException JavaDoc, IOException JavaDoc {
84         try {
85             processor.process(request, response);
86         } catch (IOException JavaDoc e) {
87             throw e;
88         } catch (ServletException JavaDoc e) {
89             throw e;
90         } catch (RuntimeException JavaDoc e) {
91             throw e;
92         } catch (Exception JavaDoc e) {
93             throw new ServletException JavaDoc("Failed to process request: " + e, e);
94         }
95     }
96 }
97
Popular Tags