001/* 002 * Copyright 2002-2012 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; 022import javax.servlet.ServletContext; 023 024import org.springframework.context.ConfigurableApplicationContext; 025import org.springframework.context.support.LiveBeansView; 026import org.springframework.util.Assert; 027 028/** 029 * {@link LiveBeansView} subclass which looks for all ApplicationContexts 030 * in the web application, as exposed in ServletContext attributes. 031 * 032 * @author Juergen Hoeller 033 * @since 3.2 034 */ 035public class ServletContextLiveBeansView extends LiveBeansView { 036 037 private final ServletContext servletContext; 038 039 /** 040 * Create a new LiveBeansView for the given ServletContext. 041 * @param servletContext current ServletContext 042 */ 043 public ServletContextLiveBeansView(ServletContext servletContext) { 044 Assert.notNull(servletContext, "ServletContext must not be null"); 045 this.servletContext = servletContext; 046 } 047 048 @Override 049 protected Set<ConfigurableApplicationContext> findApplicationContexts() { 050 Set<ConfigurableApplicationContext> contexts = new LinkedHashSet<ConfigurableApplicationContext>(); 051 Enumeration<String> attrNames = this.servletContext.getAttributeNames(); 052 while (attrNames.hasMoreElements()) { 053 String attrName = attrNames.nextElement(); 054 Object attrValue = this.servletContext.getAttribute(attrName); 055 if (attrValue instanceof ConfigurableApplicationContext) { 056 contexts.add((ConfigurableApplicationContext) attrValue); 057 } 058 } 059 return contexts; 060 } 061 062}