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.xml.BeanDefinitionParser;
23: import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
24: import org.springframework.beans.factory.xml.ParserContext;
25: import org.springframework.core.JdkVersion;
26: import org.springframework.util.ClassUtils;
27:
28: /**
29: * {@link org.springframework.beans.factory.xml.NamespaceHandler}
30: * for the '<code>context</code>' namespace.
31: *
32: * @author Mark Fisher
33: * @author Juergen Hoeller
34: * @since 2.5
35: */
36: public class ContextNamespaceHandler extends NamespaceHandlerSupport {
37:
38: public void init() {
39: registerBeanDefinitionParser("property-placeholder",
40: new PropertyPlaceholderBeanDefinitionParser());
41: registerJava5DependentParser("annotation-config",
42: "org.springframework.context.annotation.AnnotationConfigBeanDefinitionParser");
43: registerJava5DependentParser("component-scan",
44: "org.springframework.context.annotation.ComponentScanBeanDefinitionParser");
45: registerBeanDefinitionParser("load-time-weaver",
46: new LoadTimeWeaverBeanDefinitionParser());
47: registerBeanDefinitionParser("spring-configured",
48: new SpringConfiguredBeanDefinitionParser());
49: registerBeanDefinitionParser("mbean-export",
50: new MBeanExportBeanDefinitionParser());
51: registerBeanDefinitionParser("mbean-server",
52: new MBeanServerBeanDefinitionParser());
53: }
54:
55: private void registerJava5DependentParser(final String elementName,
56: final String parserClassName) {
57: BeanDefinitionParser parser = null;
58: if (JdkVersion.isAtLeastJava15()) {
59: try {
60: Class parserClass = ClassUtils.forName(parserClassName,
61: getClass().getClassLoader());
62: parser = (BeanDefinitionParser) parserClass
63: .newInstance();
64: } catch (Throwable ex) {
65: throw new IllegalStateException(
66: "Unable to create Java 1.5 dependent parser: "
67: + parserClassName, ex);
68: }
69: } else {
70: parser = new BeanDefinitionParser() {
71: public BeanDefinition parse(Element element,
72: ParserContext parserContext) {
73: throw new IllegalStateException(
74: "Context namespace element '"
75: + elementName
76: + "' and its parser class ["
77: + parserClassName
78: + "] are only available on JDK 1.5 and higher");
79: }
80: };
81: }
82: registerBeanDefinitionParser(elementName, parser);
83: }
84:
85: }
|