KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > maven > plugin > jbi > JbiServiceUnitDescriptorWriter


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.maven.plugin.jbi;
18
19 import java.io.File JavaDoc;
20 import java.io.FileWriter JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.Writer JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import javax.xml.namespace.QName JavaDoc;
29
30 import org.apache.servicemix.common.packaging.Consumes;
31 import org.apache.servicemix.common.packaging.Provides;
32 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
33 import org.codehaus.plexus.util.xml.XMLWriter;
34
35 /**
36  * Helper that is used to write the jbi.xml for a service unit
37  *
38  */

39 public class JbiServiceUnitDescriptorWriter {
40
41     private final String JavaDoc encoding;
42
43     public JbiServiceUnitDescriptorWriter(String JavaDoc encoding) {
44         this.encoding = encoding;
45     }
46
47     public void write(File JavaDoc descriptor, String JavaDoc name, String JavaDoc description,
48             List JavaDoc uris, List JavaDoc consumes, List JavaDoc provides) throws JbiPluginException {
49         FileWriter JavaDoc w;
50         try {
51             w = new FileWriter JavaDoc(descriptor);
52         } catch (IOException JavaDoc ex) {
53             throw new JbiPluginException("Exception while opening file["
54                     + descriptor.getAbsolutePath() + "]", ex);
55         }
56
57         XMLWriter writer = new PrettyPrintXMLWriter(w, encoding, null);
58         writer.startElement("jbi");
59         writer.addAttribute("xmlns", "http://java.sun.com/xml/ns/jbi");
60         writer.addAttribute("version", "1.0");
61
62         writer.startElement("services");
63
64         // We need to get all the namespaces into a hashmap so we
65
// can get the QName output correctly
66
Map JavaDoc namespaceMap = getNamespaceMap(provides, consumes);
67
68         // Set-up the namespaces
69
for (Iterator JavaDoc iterator = namespaceMap.keySet().iterator(); iterator
70                 .hasNext();) {
71             String JavaDoc key = (String JavaDoc) iterator.next();
72             StringBuffer JavaDoc namespaceDecl = new StringBuffer JavaDoc();
73             namespaceDecl.append("xmlns:");
74             namespaceDecl.append(namespaceMap.get(key));
75             writer.addAttribute(namespaceDecl.toString(), key);
76         }
77
78         // Put in the provides
79
for (Iterator JavaDoc iterator = provides.iterator(); iterator.hasNext();) {
80             Provides providesEntry = (Provides) iterator.next();
81             writer.startElement("provides");
82             addQNameAttribute(writer, "interface-name", providesEntry
83                     .getInterfaceName(), namespaceMap);
84             addQNameAttribute(writer, "service-name", providesEntry
85                     .getServiceName(), namespaceMap);
86             addStringAttribute(writer, "endpoint-name", providesEntry
87                     .getEndpointName());
88             writer.endElement();
89         }
90
91         // Put in the consumes
92
for (Iterator JavaDoc iterator = consumes.iterator(); iterator.hasNext();) {
93             Consumes consumesEntry = (Consumes) iterator.next();
94             writer.startElement("consumes");
95             addQNameAttribute(writer, "interface-name", consumesEntry
96                     .getInterfaceName(), namespaceMap);
97             addQNameAttribute(writer, "service-name", consumesEntry
98                     .getServiceName(), namespaceMap);
99             addStringAttribute(writer, "endpoint-name", consumesEntry
100                     .getEndpointName());
101
102             // TODO Handling of LinkType?
103

104             writer.endElement();
105         }
106
107         writer.endElement();
108
109         writer.endElement();
110
111         close(w);
112     }
113
114     private void addStringAttribute(XMLWriter writer, String JavaDoc attributeName,
115             String JavaDoc attributeValue) {
116         if (attributeValue != null)
117             writer.addAttribute(attributeName, attributeValue);
118     }
119
120     private void addQNameAttribute(XMLWriter writer, String JavaDoc attributeName,
121             QName JavaDoc attributeValue, Map JavaDoc namespaceMap) {
122         System.out.println("attributeName=" + attributeValue);
123         if (attributeValue != null) {
124             StringBuffer JavaDoc attributeStringValue = new StringBuffer JavaDoc();
125             attributeStringValue.append(namespaceMap.get(attributeValue
126                     .getNamespaceURI()));
127             attributeStringValue.append(":");
128             attributeStringValue.append(attributeValue.getLocalPart());
129             writer.addAttribute(attributeName, attributeStringValue.toString());
130         }
131
132     }
133
134     private Map JavaDoc getNamespaceMap(List JavaDoc provides, List JavaDoc consumes) {
135         Map JavaDoc namespaceMap = new HashMap JavaDoc();
136         int namespaceCounter = 1;
137         for (Iterator JavaDoc iterator = provides.iterator(); iterator.hasNext();) {
138             Provides providesEntry = (Provides) iterator.next();
139             resolveMapEntry(namespaceMap, providesEntry.getInterfaceName(),
140                     namespaceCounter);
141             resolveMapEntry(namespaceMap, providesEntry.getServiceName(),
142                     namespaceCounter);
143         }
144
145         for (Iterator JavaDoc iterator = consumes.iterator(); iterator.hasNext();) {
146             Consumes consumesEntry = (Consumes) iterator.next();
147             resolveMapEntry(namespaceMap, consumesEntry.getInterfaceName(),
148                     namespaceCounter);
149             resolveMapEntry(namespaceMap, consumesEntry.getServiceName(),
150                     namespaceCounter);
151         }
152
153         return namespaceMap;
154     }
155
156     private void resolveMapEntry(Map JavaDoc namespaceMap, QName JavaDoc qname,
157             int namespaceCounter) {
158         if ((qname != null)
159                 && (!namespaceMap.containsKey(qname.getNamespaceURI()))) {
160             if (qname.getPrefix() == null || qname.getPrefix().equals("") ) {
161                 namespaceMap.put(qname.getNamespaceURI(), "ns"
162                         + namespaceCounter++);
163             } else
164                 namespaceMap.put(qname.getNamespaceURI(), qname.getPrefix());
165         }
166     }
167
168     private void close(Writer JavaDoc closeable) {
169         if (closeable != null) {
170             try {
171                 closeable.close();
172             } catch (Exception JavaDoc e) {
173                 // TODO: warn
174
}
175         }
176     }
177
178 }
179
Popular Tags