io.openmessaging.spring.config.AccessPointBeanDefinitionParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openmessaging-spring Show documentation
Show all versions of openmessaging-spring Show documentation
Spring Support for the OpenMessaging API
The newest version!
/**
* Copyright 2019 The JoyQueue Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.openmessaging.spring.config;
import io.openmessaging.spring.OMSSpringConsts;
import io.openmessaging.spring.support.AccessPointContainer;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.ManagedList;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.DomUtils;
import org.w3c.dom.Element;
import java.util.List;
/**
* Parser for the access-point element.
*
* @version OMS 1.0.0
* @since OMS 1.0.0
*/
public class AccessPointBeanDefinitionParser implements BeanDefinitionParser {
private static final String ATTRIBUTE_ID = "id";
private static final String ATTRIBUTE_URL = "url";
private static final String ELEMENT_ATTRIBUTE = "attribute";
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
String id = element.getAttribute(ATTRIBUTE_ID);
String url = element.getAttribute(ATTRIBUTE_URL);
Assert.hasText(url, String.format("%s can not be blank", ATTRIBUTE_URL));
if (!StringUtils.hasText(id)) {
id = OMSSpringConsts.DEFAULT_ACCESS_POINT_ID;
}
BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(AccessPointContainer.class)
.addConstructorArgValue(id)
.addConstructorArgValue(url);
List attributes = parseAttributes(beanDefinitionBuilder, element, parserContext);
beanDefinitionBuilder.addConstructorArgValue(attributes);
AbstractBeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
parserContext.getRegistry().registerBeanDefinition(id, beanDefinition);
return beanDefinition;
}
protected List parseAttributes(BeanDefinitionBuilder builder, Element element, ParserContext parserContext) {
List attributeElements = DomUtils.getChildElementsByTagName(element, ELEMENT_ATTRIBUTE);
List result = new ManagedList(attributeElements.size());
for (Element attributeElement : attributeElements) {
result.add(parserContext.getDelegate().parseCustomElement(attributeElement, builder.getRawBeanDefinition()));
}
return result;
}
}