01: package org.springframework.context.config;
02:
03: import org.w3c.dom.Element;
04:
05: import org.springframework.beans.factory.support.AbstractBeanDefinition;
06: import org.springframework.beans.factory.support.RootBeanDefinition;
07: import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
08: import org.springframework.beans.factory.xml.ParserContext;
09: import org.springframework.jmx.support.MBeanServerFactoryBean;
10: import org.springframework.jmx.support.WebSphereMBeanServerFactoryBean;
11: import org.springframework.jndi.JndiObjectFactoryBean;
12: import org.springframework.util.ClassUtils;
13: import org.springframework.util.StringUtils;
14:
15: /**
16: * Parser for the <context:mbean-server/> element.
17: * <p>Registers an instance of {@link org.springframework.jmx.export.annotation.AnnotationMBeanExporter} within the context.
18: *
19: * @author Mark Fisher
20: * @author Juergen Hoeller
21: * @since 2.5
22: * @see org.springframework.jmx.export.annotation.AnnotationMBeanExporter
23: */
24: class MBeanServerBeanDefinitionParser extends
25: AbstractBeanDefinitionParser {
26:
27: private static final String MBEAN_SERVER_BEAN_NAME = "mbeanServer";
28:
29: private static final String AGENT_ID_ATTRIBUTE = "agent-id";
30:
31: private static final boolean weblogicPresent = ClassUtils
32: .isPresent("weblogic.management.Helper");
33:
34: private static final boolean webspherePresent = ClassUtils
35: .isPresent("com.ibm.websphere.management.AdminServiceFactory");
36:
37: protected String resolveId(Element element,
38: AbstractBeanDefinition definition,
39: ParserContext parserContext) {
40: String id = element.getAttribute(ID_ATTRIBUTE);
41: return (StringUtils.hasText(id) ? id : MBEAN_SERVER_BEAN_NAME);
42: }
43:
44: protected AbstractBeanDefinition parseInternal(Element element,
45: ParserContext parserContext) {
46: String agentId = element.getAttribute(AGENT_ID_ATTRIBUTE);
47: if (StringUtils.hasText(agentId)) {
48: RootBeanDefinition bd = new RootBeanDefinition(
49: MBeanServerFactoryBean.class);
50: bd.getPropertyValues().addPropertyValue("agentId", agentId);
51: return bd;
52: }
53: AbstractBeanDefinition specialServer = findServerForSpecialEnvironment();
54: if (specialServer != null) {
55: return specialServer;
56: }
57: RootBeanDefinition bd = new RootBeanDefinition(
58: MBeanServerFactoryBean.class);
59: bd.getPropertyValues().addPropertyValue(
60: "locateExistingServerIfPossible", Boolean.TRUE);
61: return bd;
62: }
63:
64: static AbstractBeanDefinition findServerForSpecialEnvironment() {
65: if (weblogicPresent) {
66: RootBeanDefinition bd = new RootBeanDefinition(
67: JndiObjectFactoryBean.class);
68: bd.getPropertyValues().addPropertyValue("jndiName",
69: "java:comp/env/jmx/runtime");
70: return bd;
71: } else if (webspherePresent) {
72: return new RootBeanDefinition(
73: WebSphereMBeanServerFactoryBean.class);
74: } else {
75: return null;
76: }
77: }
78:
79: }
|