01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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.context.config;
18:
19: import org.w3c.dom.Element;
20:
21: import org.springframework.beans.factory.config.BeanDefinition;
22: import org.springframework.beans.factory.support.AbstractBeanDefinition;
23: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
24: import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
25: import org.springframework.beans.factory.xml.ParserContext;
26: import org.springframework.core.JdkVersion;
27: import org.springframework.util.StringUtils;
28:
29: /**
30: * Parser for the <context:mbean-export/> element.
31: *
32: * <p>Registers an instance of
33: * {@link org.springframework.jmx.export.annotation.AnnotationMBeanExporter}
34: * within the context.
35: *
36: * @author Mark Fisher
37: * @author Juergen Hoeller
38: * @since 2.5
39: * @see org.springframework.jmx.export.annotation.AnnotationMBeanExporter
40: */
41: class MBeanExportBeanDefinitionParser extends
42: AbstractBeanDefinitionParser {
43:
44: private static final String MBEAN_EXPORTER_BEAN_NAME = "mbeanExporter";
45:
46: private static final String DEFAULT_DOMAIN_ATTRIBUTE = "default-domain";
47:
48: private static final String SERVER_ATTRIBUTE = "server";
49:
50: protected String resolveId(Element element,
51: AbstractBeanDefinition definition,
52: ParserContext parserContext) {
53: return MBEAN_EXPORTER_BEAN_NAME;
54: }
55:
56: protected AbstractBeanDefinition parseInternal(Element element,
57: ParserContext parserContext) {
58: String beanClassName = (JdkVersion.isAtLeastJava15() ? "org.springframework.jmx.export.annotation.AnnotationMBeanExporter"
59: : "org.springframework.jmx.export.MBeanExporter");
60: BeanDefinitionBuilder builder = BeanDefinitionBuilder
61: .rootBeanDefinition(beanClassName);
62: builder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
63: String defaultDomain = element
64: .getAttribute(DEFAULT_DOMAIN_ATTRIBUTE);
65: if (StringUtils.hasText(defaultDomain)) {
66: builder.addPropertyValue("defaultDomain", defaultDomain);
67: }
68: String serverBeanName = element.getAttribute(SERVER_ATTRIBUTE);
69: if (StringUtils.hasText(serverBeanName)) {
70: builder.addPropertyReference("server", serverBeanName);
71: } else {
72: AbstractBeanDefinition specialServer = MBeanServerBeanDefinitionParser
73: .findServerForSpecialEnvironment();
74: if (specialServer != null) {
75: builder.addPropertyValue("server", specialServer);
76: }
77: }
78: return builder.getBeanDefinition();
79: }
80:
81: }
|