|
| 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 | + |
| 18 | +package org.apache.dubbo.config.spring.initializer; |
| 19 | + |
| 20 | +import org.apache.commons.logging.Log; |
| 21 | +import org.apache.commons.logging.LogFactory; |
| 22 | + |
| 23 | +import javax.servlet.ServletContext; |
| 24 | +import javax.servlet.ServletContextEvent; |
| 25 | +import javax.servlet.ServletContextListener; |
| 26 | + |
| 27 | +/** |
| 28 | + * A Dubbo context listener is a delegation to org.springframework.web.context.ContextLoaderListener. This is necessary, |
| 29 | + * because Dubbo is packaged into all-in-one jar, therefore it contains a web-fragment.xml from this sub module which's |
| 30 | + * used for helping to assemble spring context listener automatically when it's not configured explicitly by user in |
| 31 | + * web.xml. It works fine with spring, but it will lead to ClassNotFound exception and fail tomcat's bootup when user |
| 32 | + * doesn't depend on spring framework. |
| 33 | + */ |
| 34 | +public class DubboContextListener implements ServletContextListener { |
| 35 | + private static final Log logger = LogFactory.getLog(DubboContextListener.class); |
| 36 | + |
| 37 | + private static final String SPRING_CONTEXT_LISTENER = "org.springframework.web.context.ContextLoaderListener"; |
| 38 | + private static final String SPRING_CONTEXT_ROOT = "org.springframework.web.context.WebApplicationContext.ROOT"; |
| 39 | + |
| 40 | + private ServletContextListener springContextListener; |
| 41 | + private boolean executed = false; |
| 42 | + |
| 43 | + public DubboContextListener() { |
| 44 | + try { |
| 45 | + Class c = Class.forName(SPRING_CONTEXT_LISTENER); |
| 46 | + springContextListener = (ServletContextListener) c.newInstance(); |
| 47 | + } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { |
| 48 | + logger.warn("Servlet container detects dubbo's web fragment configuration, and tries to load " + |
| 49 | + "org.springframework.web.context.ContextLoaderListener but fails to find the class. " + |
| 50 | + "If the application don't rely on Spring framework, pls. simply ignore"); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void contextInitialized(ServletContextEvent servletContextEvent) { |
| 56 | + if (springContextListener != null) { |
| 57 | + // if spring context listener has already been registered, then do nothing |
| 58 | + ServletContext context = servletContextEvent.getServletContext(); |
| 59 | + if (context.getAttribute(SPRING_CONTEXT_ROOT) == null) { |
| 60 | + executed = true; |
| 61 | + springContextListener.contextInitialized(servletContextEvent); |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void contextDestroyed(ServletContextEvent servletContextEvent) { |
| 68 | + if (springContextListener != null && executed) { |
| 69 | + springContextListener.contextDestroyed(servletContextEvent); |
| 70 | + } |
| 71 | + } |
| 72 | +} |
0 commit comments