001/* 002 * Copyright 2002-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.context.support; 018 019import java.util.Enumeration; 020import java.util.LinkedHashSet; 021import java.util.Set; 022 023import javax.servlet.ServletContext; 024 025import org.springframework.context.ConfigurableApplicationContext; 026import org.springframework.context.support.LiveBeansView; 027import org.springframework.util.Assert; 028 029/** 030 * {@link LiveBeansView} subclass which looks for all ApplicationContexts 031 * in the web application, as exposed in ServletContext attributes. 032 * 033 * @author Juergen Hoeller 034 * @since 3.2 035 */ 036public class ServletContextLiveBeansView extends LiveBeansView { 037 038 private final ServletContext servletContext; 039 040 /** 041 * Create a new LiveBeansView for the given ServletContext. 042 * @param servletContext current ServletContext 043 */ 044 public ServletContextLiveBeansView(ServletContext servletContext) { 045 Assert.notNull(servletContext, "ServletContext must not be null"); 046 this.servletContext = servletContext; 047 } 048 049 @Override 050 protected Set<ConfigurableApplicationContext> findApplicationContexts() { 051 Set<ConfigurableApplicationContext> contexts = new LinkedHashSet<>(); 052 Enumeration<String> attrNames = this.servletContext.getAttributeNames(); 053 while (attrNames.hasMoreElements()) { 054 String attrName = attrNames.nextElement(); 055 Object attrValue = this.servletContext.getAttribute(attrName); 056 if (attrValue instanceof ConfigurableApplicationContext) { 057 contexts.add((ConfigurableApplicationContext) attrValue); 058 } 059 } 060 return contexts; 061 } 062 063}