/*
 * Copyright 2012-2014 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.endpoint;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import org.apache.catalina.Container;
import org.apache.catalina.Context;
import org.apache.catalina.Manager;
import org.springframework.beans.BeansException;
import org.springframework.boot.actuate.metrics.Metric;
import org.springframework.boot.context.embedded.EmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * A {@link PublicMetrics} implementation that provides Tomcat statistics.
 *
 * @author Johannes Stelzer
 * @author Phillip Webb
 * @since 1.2.0
 */
public class TomcatPublicMetrics implements PublicMetrics, ApplicationContextAware {

	private ApplicationContext applicationContext;

	@Override
	public Collection<Metric<?>> metrics() {
		if (this.applicationContext instanceof EmbeddedWebApplicationContext) {
			Manager manager = getManager((EmbeddedWebApplicationContext) this.applicationContext);
			if (manager != null) {
				return metrics(manager);
			}
		}
		return Collections.emptySet();
	}

	private Manager getManager(EmbeddedWebApplicationContext applicationContext) {
		EmbeddedServletContainer embeddedServletContainer = applicationContext
				.getEmbeddedServletContainer();
		if (embeddedServletContainer instanceof TomcatEmbeddedServletContainer) {
			return getManager((TomcatEmbeddedServletContainer) embeddedServletContainer);
		}
		return null;
	}

	private Manager getManager(TomcatEmbeddedServletContainer servletContainer) {
		for (Container container : servletContainer.getTomcat().getHost().findChildren()) {
			if (container instanceof Context) {
				return ((Context) container).getManager();
			}
		}
		return null;
	}

	private Collection<Metric<?>> metrics(Manager manager) {
		List<Metric<?>> metrics = new ArrayList<Metric<?>>(2);
		metrics.add(new Metric<Integer>("httpsessions.max", manager.getMaxActive()));
		metrics.add(new Metric<Integer>("httpsessions.active", manager
				.getActiveSessions()));
		return metrics;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

}

@@ -884,24 +884,72 @@
 na.Manager;%0A
+import org.apache.catalina.session.ManagerBase;%0A
 import org.s
@@ -2776,67 +2776,115 @@
 ;%0A%09%09
-metrics.add(new Metric%3CInteger%3E(%22httpsessions.max%22,
+if (manager instanceof ManagerBase) %7B%0A%09%09%09addMetric(metrics, %22httpsessions.max%22,%0A%09%09%09%09%09((ManagerBase)
  manager
 .get
@@ -2879,16 +2879,17 @@
  manager
+)
 .getMaxA
@@ -2897,48 +2897,46 @@
 tive
-()));%0A%09%09metrics.add(new Metric%3CInteger%3E(
+Sessions());%0A%09%09%7D%0A%09%09addMetric(metrics, 
 %22htt
@@ -2961,21 +2961,16 @@
  manager
-%0A%09%09%09%09
 .getActi
@@ -2982,17 +2982,16 @@
 sions())
-)
 ;%0A%09%09retu
@@ -3006,16 +3006,148 @@
 cs;%0A%09%7D%0A%0A
+%09private void addMetric(List%3CMetric%3C?%3E%3E metrics, String name, Integer value) %7B%0A%09%09metrics.add(new Metric%3CInteger%3E(name, value));%0A%09%7D%0A%0A
 %09@Overri

Use getMaxActiveSessions() in Tomcat metrics