001/*
002 * Copyright 2002-2017 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.io.IOException;
020import javax.servlet.ServletException;
021import javax.servlet.http.HttpServlet;
022import javax.servlet.http.HttpServletRequest;
023import javax.servlet.http.HttpServletResponse;
024
025import org.springframework.context.support.LiveBeansView;
026
027/**
028 * Servlet variant of {@link LiveBeansView}'s MBean exposure.
029 *
030 * <p>Generates a JSON snapshot for current beans and their dependencies in
031 * all ApplicationContexts that live within the current web application.
032 *
033 * @author Juergen Hoeller
034 * @since 3.2
035 * @see org.springframework.context.support.LiveBeansView#getSnapshotAsJson()
036 */
037@SuppressWarnings("serial")
038public class LiveBeansViewServlet extends HttpServlet {
039
040        private LiveBeansView liveBeansView;
041
042
043        @Override
044        public void init() throws ServletException {
045                this.liveBeansView = buildLiveBeansView();
046        }
047
048        protected LiveBeansView buildLiveBeansView() {
049                return new ServletContextLiveBeansView(getServletContext());
050        }
051
052        @Override
053        protected void doGet(HttpServletRequest request, HttpServletResponse response)
054                        throws ServletException, IOException {
055
056                String content = this.liveBeansView.getSnapshotAsJson();
057                response.setContentType("application/json");
058                response.setContentLength(content.length());
059                response.getWriter().write(content);
060        }
061
062}