001: /*
002: * Copyright 2004-2006 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.compass.spring.config;
018:
019: import org.compass.core.CompassException;
020: import org.compass.core.config.CompassConfiguration;
021: import org.compass.core.config.CompassConfigurationFactory;
022: import org.compass.core.config.builder.SchemaConfigurationBuilder;
023: import org.compass.core.util.ClassUtils;
024: import org.compass.core.util.DomUtils;
025: import org.compass.spring.LocalCompassBean;
026: import org.compass.spring.LocalCompassSessionBean;
027: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
028: import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
029: import org.springframework.beans.factory.xml.NamespaceHandlerSupport;
030: import org.w3c.dom.Element;
031:
032: /**
033: * @author kimchy
034: */
035: public class CompassNamespaceHandler extends NamespaceHandlerSupport {
036:
037: public CompassNamespaceHandler() {
038: }
039:
040: public void init() {
041: registerBeanDefinitionParser("compass",
042: new CompassBeanDefinitionParser());
043: registerBeanDefinitionParser("context",
044: new CompassBeanDefinitionParser());
045: registerBeanDefinitionParser("session",
046: new CompassBeanDefinitionParser());
047: }
048:
049: private static class CompassBeanDefinitionParser extends
050: AbstractSingleBeanDefinitionParser {
051:
052: protected void doParse(Element element,
053: BeanDefinitionBuilder beanDefinitionBuilder) {
054: if (element.getLocalName().equals("compass")) {
055: String id = element.getAttribute("name");
056:
057: // set the id so it will be registered under it in the base class
058: element.setAttribute(ID_ATTRIBUTE, id);
059:
060: SchemaConfigurationBuilder schemaConfigurationBuilder = new SchemaConfigurationBuilder();
061: CompassConfiguration config = CompassConfigurationFactory
062: .newConfiguration();
063: schemaConfigurationBuilder.processCompass(element,
064: config);
065: beanDefinitionBuilder.addPropertyValue(
066: "compassConfiguration", config);
067:
068: String txManagerRef = DomUtils.getElementAttribute(
069: element, "txManager");
070: if (txManagerRef != null) {
071: beanDefinitionBuilder.addPropertyReference(
072: "transactionManager", txManagerRef);
073: }
074:
075: String dataSourceRef = DomUtils.getElementAttribute(
076: element, "dataSource");
077: if (dataSourceRef != null) {
078: beanDefinitionBuilder.addPropertyReference(
079: "dataSource", dataSourceRef);
080: }
081:
082: String lazyInit = DomUtils.getElementAttribute(element,
083: "lazy-init");
084: if (lazyInit == null
085: || lazyInit.equalsIgnoreCase("false")) {
086: beanDefinitionBuilder.setLazyInit(false);
087: } else {
088: beanDefinitionBuilder.setLazyInit(true);
089: }
090:
091: String scope = DomUtils.getElementAttribute(element,
092: "scope");
093: if (scope != null) {
094: beanDefinitionBuilder.setScope(scope);
095: }
096:
097: String postProcessRef = DomUtils.getElementAttribute(
098: element, "postProcessor");
099: if (postProcessRef != null) {
100: beanDefinitionBuilder.addPropertyReference(
101: "postProcessor", postProcessRef);
102: }
103: } else if (element.getLocalName().equals("context")) {
104: element.setAttribute(ID_ATTRIBUTE, ""
105: + System.currentTimeMillis());
106: } else if (element.getLocalName().equals("session")) {
107: String compassRef = DomUtils.getElementAttribute(
108: element, "compass");
109: if (compassRef != null) {
110: beanDefinitionBuilder.addPropertyReference(
111: "compass", compassRef);
112: }
113: }
114: }
115:
116: protected Class getBeanClass(Element element) {
117: if (element.getLocalName().equals("compass")) {
118: return LocalCompassBean.class;
119: } else if (element.getLocalName().equals("context")) {
120: try {
121: return ClassUtils.forName(
122: COMPASS_CONTEXT_BEAN_POST_PROCESSOR, Thread
123: .currentThread()
124: .getContextClassLoader());
125: } catch (ClassNotFoundException e) {
126: throw new CompassException("Failed to find class ["
127: + COMPASS_CONTEXT_BEAN_POST_PROCESSOR + "]");
128: }
129: } else if (element.getLocalName().equals("session")) {
130: return LocalCompassSessionBean.class;
131: } else {
132: throw new CompassException("Failed to parse element ["
133: + element.getLocalName() + "]");
134: }
135: }
136: }
137:
138: private static final String COMPASS_CONTEXT_BEAN_POST_PROCESSOR = "org.compass.spring.support.CompassContextBeanPostProcessor";
139: }
|