KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > config > ComponentElementProcessor


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.config;
18
19 import org.apache.commons.logging.Log;
20 import org.apache.commons.logging.LogFactory;
21 import org.apache.servicemix.jbi.config.spring.ElementProcessor;
22 import org.apache.servicemix.jbi.config.spring.QNameElementProcessor;
23 import org.apache.servicemix.jbi.util.DOMUtil;
24 import org.springframework.beans.factory.BeanDefinitionStoreException;
25 import org.springframework.beans.factory.support.BeanDefinitionReader;
26 import org.w3c.dom.Document JavaDoc;
27 import org.w3c.dom.Element JavaDoc;
28 import org.w3c.dom.Node JavaDoc;
29 import org.w3c.dom.NodeList JavaDoc;
30
31 /**
32  * Handles the 'component' element
33  *
34  * @version $Revision: 426415 $
35  */

36 public class ComponentElementProcessor extends QNameElementProcessor implements ElementProcessor {
37
38     private static final transient Log log = LogFactory.getLog(ComponentElementProcessor.class);
39
40     public void processElement(Element JavaDoc element, BeanDefinitionReader beanDefinitionReader) {
41         // lets add a new bean element
42
Document JavaDoc document = element.getOwnerDocument();
43
44         Element JavaDoc root = (Element JavaDoc) element.getParentNode();
45         String JavaDoc id = element.getAttribute("id");
46         if (id == null || id.length() == 0) {
47             throw new BeanDefinitionStoreException("A <component> must have an id attribute");
48         }
49
50         Element JavaDoc registration = addBeanElement(root, "org.apache.servicemix.jbi.container.ActivationSpec");
51         addPropertyElement(registration, "id", id);
52         Element JavaDoc componentProperty = addPropertyElement(registration, "component");
53         Element JavaDoc bean = document.createElement("bean");
54         componentProperty.appendChild(bean);
55
56         // lets remove any attributes we need
57
String JavaDoc service = element.getAttribute("service");
58         if (service != null) {
59             element.removeAttribute("service");
60             addQNameProperty(registration, "service", service, element);
61         }
62         String JavaDoc interfaceName = element.getAttribute("interface");
63         if (interfaceName != null) {
64             element.removeAttribute("interface");
65             addQNameProperty(registration, "interfaceName", interfaceName, element);
66         }
67         String JavaDoc operation = element.getAttribute("operation");
68         if (operation != null) {
69             element.removeAttribute("operation");
70             addQNameProperty(registration, "operation", operation, element);
71         }
72
73         String JavaDoc endpoint = element.getAttribute("endpoint");
74         if (endpoint != null) {
75             element.removeAttribute("endpoint");
76             if (endpoint.length() > 0) {
77                 addPropertyElement(registration, "endpoint", endpoint);
78             }
79         }
80         String JavaDoc destinationEndpoint = element.getAttribute("destinationEndpoint");
81         if (destinationEndpoint != null) {
82             element.removeAttribute("destinationEndpoint");
83             if (destinationEndpoint.length() > 0) {
84                 addPropertyElement(registration, "destinationEndpoint", destinationEndpoint);
85             }
86         }
87
88         String JavaDoc destinationService = element.getAttribute("destinationService");
89         if (destinationService != null) {
90             element.removeAttribute("destinationService");
91             addQNameProperty(registration, "destinationService", destinationService, element);
92         }
93         String JavaDoc destinationInterface = element.getAttribute("destinationInterface");
94         if (destinationInterface != null) {
95             element.removeAttribute("destinationInterface");
96             addQNameProperty(registration, "destinationInterface", destinationInterface, element);
97         }
98         String JavaDoc destinationOperation = element.getAttribute("destinationOperation");
99         if (destinationOperation != null) {
100             element.removeAttribute("destinationOperation");
101             addQNameProperty(registration, "destinationOperation", destinationOperation, element);
102         }
103         String JavaDoc failOnNoEndpoint = element.getAttribute("failIfNoDestinationEndpoint");
104         if (failOnNoEndpoint != null) {
105             element.removeAttribute("failIfNoDestinationEndpoint");
106             if (failOnNoEndpoint.length() > 0) {
107                 addPropertyElement(registration, "failIfNoDestinationEndpoint", failOnNoEndpoint);
108             }
109         }
110         String JavaDoc persistent = element.getAttribute("persistent");
111         if (persistent != null) {
112             element.removeAttribute("persistent");
113             if (persistent.length() > 0) {
114                 addPropertyElement(registration, "persistent", persistent);
115             }
116         }
117
118         // lets move any subscriptions into a new property
119
Element JavaDoc list = root.getOwnerDocument().createElement("list");
120         boolean hasSubscriptions = false;
121         NodeList JavaDoc childNodes = element.getChildNodes();
122         for (int index = 0; index < childNodes.getLength() - 1; ) {
123             Node JavaDoc node = childNodes.item(index);
124             if (node.getNodeType() == Node.ELEMENT_NODE && node.getLocalName().equals("subscription")) {
125                 element.removeChild(node);
126                 list.appendChild(node);
127                 hasSubscriptions = true;
128             }
129             else {
130                 index++;
131             }
132
133         }
134         if (hasSubscriptions) {
135             Element JavaDoc subscriptions = addPropertyElement(root, "subscriptions");
136             subscriptions.appendChild(list);
137             registration.appendChild(subscriptions);
138         }
139
140         DOMUtil.copyAttributes(element, bean);
141         DOMUtil.moveContent(element, bean);
142         root.removeChild(element);
143
144         logXmlGenerated(log, "component generated", registration);
145     }
146
147 }
148
Popular Tags