|
| 1 | +/* |
| 2 | + * Copyright 2012-2017 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.actuate.autoconfigure.jolokia; |
| 18 | + |
| 19 | +import org.jolokia.http.AgentServlet; |
| 20 | + |
| 21 | +import org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration; |
| 22 | +import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext; |
| 23 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 24 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; |
| 27 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 28 | +import org.springframework.boot.web.servlet.ServletRegistrationBean; |
| 29 | +import org.springframework.context.annotation.Bean; |
| 30 | +import org.springframework.web.servlet.mvc.ServletWrappingController; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link ManagementContextConfiguration} for embedding Jolokia, a JMX-HTTP bridge giving |
| 34 | + * an alternative to JSR-160 connectors. |
| 35 | + * |
| 36 | + * <p> |
| 37 | + * This configuration will get automatically enabled as soon as the Jolokia |
| 38 | + * {@link AgentServlet} is on the classpath. To disable it set |
| 39 | + * {@code management.jolokia.enabled=false}. |
| 40 | + * |
| 41 | + * <p> |
| 42 | + * Additional configuration parameters for Jolokia can be provided by specifying |
| 43 | + * {@code management.jolokia.config.*} properties. See the |
| 44 | + * <a href="http://jolokia.org">http://jolokia.org</a> web site for more information on |
| 45 | + * supported configuration parameters. |
| 46 | + * |
| 47 | + * @author Christian Dupuis |
| 48 | + * @author Dave Syer |
| 49 | + * @author Andy Wilkinson |
| 50 | + * @author Madhura Bhave |
| 51 | + * @author Stephane Nicoll |
| 52 | + * @since 2.0.0 |
| 53 | + */ |
| 54 | +@ManagementContextConfiguration |
| 55 | +@ConditionalOnWebApplication(type = Type.SERVLET) |
| 56 | +@ConditionalOnClass({ AgentServlet.class, ServletWrappingController.class }) |
| 57 | +@ConditionalOnProperty(value = "management.jolokia.enabled", matchIfMissing = true) |
| 58 | +@EnableConfigurationProperties(JolokiaProperties.class) |
| 59 | +public class JolokiaManagementContextConfiguration { |
| 60 | + |
| 61 | + private final ManagementServletContext managementServletContext; |
| 62 | + |
| 63 | + private final JolokiaProperties properties; |
| 64 | + |
| 65 | + public JolokiaManagementContextConfiguration( |
| 66 | + ManagementServletContext managementServletContext, |
| 67 | + JolokiaProperties properties) { |
| 68 | + this.managementServletContext = managementServletContext; |
| 69 | + this.properties = properties; |
| 70 | + } |
| 71 | + |
| 72 | + @Bean |
| 73 | + public ServletRegistrationBean<AgentServlet> jolokiaServlet() { |
| 74 | + String path = this.managementServletContext.getContextPath() |
| 75 | + + this.properties.getPath(); |
| 76 | + String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*"); |
| 77 | + ServletRegistrationBean<AgentServlet> registration = new ServletRegistrationBean<>( |
| 78 | + new AgentServlet(), urlMapping); |
| 79 | + registration.setInitParameters(this.properties.getConfig()); |
| 80 | + return registration; |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments