/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.boot.actuate.beans;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;

/**
 * {@link Endpoint} to expose details of an application's beans, grouped by application
 * context.
 *
 * @author Dave Syer
 * @author Andy Wilkinson
 * @since 2.0.0
 */
@Endpoint(id = "beans")
public class BeansEndpoint {

	private final ConfigurableApplicationContext context;

	/**
	 * Creates a new {@code BeansEndpoint} that will describe the beans in the given
	 * {@code context} and all of its ancestors.
	 *
	 * @param context the application context
	 * @see ConfigurableApplicationContext#getParent()
	 */
	public BeansEndpoint(ConfigurableApplicationContext context) {
		this.context = context;
	}

	@ReadOperation
	public ApplicationContextDescriptor beans() {
		return ApplicationContextDescriptor.describing(this.context);
	}

	/**
	 * Response produced by the {@link BeansEndpoint}, primarily intended for
	 * serialization to JSON.
	 */
	public static class BeansEndpointResponse {

		private List<ApplicationContextDescriptor> contexts;

		public BeansEndpointResponse(List<ApplicationContextDescriptor> contexts) {
			this.contexts = contexts;
		}

		public List<ApplicationContextDescriptor> getContexts() {
			return this.contexts;
		}

	}

	/**
	 * A description of an application context, primarily intended for serialization to
	 * JSON.
	 */
	public static final class ApplicationContextDescriptor {

		private final String id;

		private final Map<String, BeanDescriptor> beans;

		private final ApplicationContextDescriptor parent;

		private ApplicationContextDescriptor(String id, Map<String, BeanDescriptor> beans,
				ApplicationContextDescriptor parent) {
			this.id = id;
			this.beans = beans;
			this.parent = parent;
		}

		public String getId() {
			return this.id;
		}

		public ApplicationContextDescriptor getParent() {
			return this.parent;
		}

		public Map<String, BeanDescriptor> getBeans() {
			return this.beans;
		}

		private static ApplicationContextDescriptor describing(
				ConfigurableApplicationContext context) {
			if (context == null) {
				return null;
			}
			return new ApplicationContextDescriptor(context.getId(),
					describeBeans(context.getBeanFactory()),
					describing(getConfigurableParent(context)));
		}

		private static Map<String, BeanDescriptor> describeBeans(
				ConfigurableListableBeanFactory beanFactory) {
			Map<String, BeanDescriptor> beans = new HashMap<>();
			for (String beanName : beanFactory.getBeanDefinitionNames()) {
				BeanDefinition definition = beanFactory.getBeanDefinition(beanName);
				if (isBeanEligible(beanName, definition, beanFactory)) {
					beans.put(beanName, describeBean(beanName, definition, beanFactory));
				}
			}
			return beans;
		}

		private static BeanDescriptor describeBean(String name, BeanDefinition definition,
				ConfigurableListableBeanFactory factory) {
			return new BeanDescriptor(factory.getAliases(name), definition.getScope(),
					factory.getType(name), definition.getResourceDescription(),
					factory.getDependenciesForBean(name));
		}

		private static boolean isBeanEligible(String beanName, BeanDefinition bd,
				ConfigurableBeanFactory bf) {
			return (bd.getRole() != BeanDefinition.ROLE_INFRASTRUCTURE
					&& (!bd.isLazyInit() || bf.containsSingleton(beanName)));
		}

		private static ConfigurableApplicationContext getConfigurableParent(
				ConfigurableApplicationContext context) {
			ApplicationContext parent = context.getParent();
			if (parent instanceof ConfigurableApplicationContext) {
				return (ConfigurableApplicationContext) parent;
			}
			return null;
		}

	}

	/**
	 * A description of a bean in an application context, primarily intended for
	 * serialization to JSON.
	 */
	public static final class BeanDescriptor {

		private final String[] aliases;

		private final String scope;

		private final Class<?> type;

		private final String resource;

		private final String[] dependencies;

		private BeanDescriptor(String[] aliases, String scope, Class<?> type,
				String resource, String[] dependencies) {
			this.aliases = aliases;
			this.scope = StringUtils.hasText(scope) ? scope
					: BeanDefinition.SCOPE_SINGLETON;
			this.type = type;
			this.resource = resource;
			this.dependencies = dependencies;
		}

		public String[] getAliases() {
			return this.aliases;
		}

		public String getScope() {
			return this.scope;
		}

		public Class<?> getType() {
			return this.type;
		}

		public String getResource() {
			return this.resource;
		}

		public String[] getDependencies() {
			return this.dependencies;
		}

	}

}

@@ -694,31 +694,8 @@
 ap;%0A
-import java.util.List;%0A
 impo
@@ -1994,428 +1994,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Response produced by the %7B@link BeansEndpoint%7D, primarily intended for%0A%09 * serialization to JSON.%0A%09 */%0A%09public static class BeansEndpointResponse %7B%0A%0A%09%09private List%3CApplicationContextDescriptor%3E contexts;%0A%0A%09%09public BeansEndpointResponse(List%3CApplicationContextDescriptor%3E contexts) %7B%0A%09%09%09this.contexts = contexts;%0A%09%09%7D%0A%0A%09%09public List%3CApplicationContextDescriptor%3E getContexts() %7B%0A%09%09%09return this.contexts;%0A%09%09%7D%0A%0A%09%7D%0A%0A
 %09/**

Remove BeansEndpoint.BeansEndpointResponse