001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.jaxws.spring;
019:
020: import java.util.List;
021: import java.util.Map;
022:
023: import javax.xml.namespace.QName;
024:
025: import org.w3c.dom.Attr;
026: import org.w3c.dom.Element;
027: import org.w3c.dom.NamedNodeMap;
028: import org.w3c.dom.Node;
029: import org.w3c.dom.NodeList;
030:
031: import org.apache.cxf.Bus;
032: import org.apache.cxf.common.classloader.ClassLoaderUtils;
033: import org.apache.cxf.common.util.StringUtils;
034: import org.apache.cxf.configuration.spring.AbstractBeanDefinitionParser;
035: import org.apache.cxf.jaxws.EndpointImpl;
036: import org.springframework.beans.FatalBeanException;
037: import org.springframework.beans.factory.BeanDefinitionStoreException;
038: import org.springframework.beans.factory.support.AbstractBeanDefinition;
039: import org.springframework.beans.factory.support.BeanDefinitionBuilder;
040: import org.springframework.beans.factory.xml.ParserContext;
041:
042: public class EndpointDefinitionParser extends
043: AbstractBeanDefinitionParser {
044:
045: private static final String IMPLEMENTOR = "implementor";
046:
047: public EndpointDefinitionParser() {
048: super ();
049: setBeanClass(EndpointImpl.class);
050: }
051:
052: @Override
053: protected String getSuffix() {
054: return ".jaxws-endpoint";
055: }
056:
057: @Override
058: protected void doParse(Element element, ParserContext ctx,
059: BeanDefinitionBuilder bean) {
060: NamedNodeMap atts = element.getAttributes();
061: String bus = element.getAttribute("bus");
062: if (StringUtils.isEmpty(bus)) {
063: if (ctx.getRegistry().containsBeanDefinition(
064: Bus.DEFAULT_BUS_ID)) {
065: bean.addConstructorArgReference(Bus.DEFAULT_BUS_ID);
066: }
067: } else {
068: if (ctx.getRegistry().containsBeanDefinition(bus)) {
069: bean.addConstructorArgReference(bus);
070: }
071: }
072: for (int i = 0; i < atts.getLength(); i++) {
073: Attr node = (Attr) atts.item(i);
074: String val = node.getValue();
075: String pre = node.getPrefix();
076: String name = node.getLocalName();
077:
078: if ("createdFromAPI".equals(name)) {
079: bean.setAbstract(true);
080: } else if (isAttribute(pre, name)
081: && !"publish".equals(name) && !"bus".equals(name)) {
082: if ("endpointName".equals(name)
083: || "serviceName".equals(name)) {
084: QName q = parseQName(element, val);
085: bean.addPropertyValue(name, q);
086: } else if (IMPLEMENTOR.equals(name)) {
087: loadImplementor(bean, val);
088: } else if (!"name".equals(name)) {
089: mapToProperty(bean, name, val);
090: }
091: } else if ("abstract".equals(name)) {
092: bean.setAbstract(true);
093: }
094: }
095:
096: NodeList children = element.getChildNodes();
097: for (int i = 0; i < children.getLength(); i++) {
098: Node n = children.item(i);
099: if (n.getNodeType() == Node.ELEMENT_NODE) {
100: String name = n.getLocalName();
101: if ("properties".equals(n.getLocalName())) {
102: Map map = ctx.getDelegate().parseMapElement(
103: (Element) n, bean.getBeanDefinition());
104: bean.addPropertyValue("properties", map);
105: } else if ("binding".equals(name)) {
106: setFirstChildAsProperty((Element) n, ctx, bean,
107: "bindingConfig");
108: } else if ("inInterceptors".equals(name)
109: || "inFaultInterceptors".equals(name)
110: || "outInterceptors".equals(name)
111: || "outFaultInterceptors".equals(name)
112: || "features".equals(name)) {
113: List list = ctx.getDelegate().parseListElement(
114: (Element) n, bean.getBeanDefinition());
115: bean.addPropertyValue(name, list);
116: } else if (IMPLEMENTOR.equals(name)) {
117: ctx.getDelegate().parseConstructorArgElement(
118: (Element) n, bean.getBeanDefinition());
119: } else {
120: setFirstChildAsProperty((Element) n, ctx, bean,
121: name);
122: }
123: }
124: }
125:
126: bean.setInitMethodName("publish");
127: bean.setDestroyMethodName("stop");
128:
129: // We don't want to delay the registration of our Server
130: bean.setLazyInit(false);
131: }
132:
133: private void loadImplementor(BeanDefinitionBuilder bean, String val) {
134: if (!StringUtils.isEmpty(val)) {
135: if (val.startsWith("#")) {
136: bean.addConstructorArgReference(val.substring(1));
137: } else {
138: try {
139: Object obj = ClassLoaderUtils.loadClass(val,
140: getClass()).newInstance();
141: bean.addConstructorArg(obj);
142: } catch (Exception e) {
143: throw new FatalBeanException(
144: "Could not load class: " + val, e);
145: }
146: }
147: }
148: }
149:
150: @Override
151: protected String resolveId(Element elem,
152: AbstractBeanDefinition definition, ParserContext ctx)
153: throws BeanDefinitionStoreException {
154: String id = super .resolveId(elem, definition, ctx);
155: if (StringUtils.isEmpty(id)) {
156: id = getBeanClass().getName() + "--" + hashCode();
157: }
158:
159: return id;
160: }
161:
162: }
|