KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
27 import org.codehaus.plexus.util.xml.XMLWriter;
28
29 /**
30  * Helper that can be used to write the jbi.xml for a service assembly
31  *
32  */

33 public class JbiServiceAssemblyDescriptorWriter {
34
35     private final String JavaDoc encoding;
36
37     public JbiServiceAssemblyDescriptorWriter(String JavaDoc encoding) {
38         this.encoding = encoding;
39     }
40
41     public void write(File JavaDoc descriptor, String JavaDoc name, String JavaDoc description,
42             List JavaDoc uris) throws JbiPluginException {
43         FileWriter JavaDoc w;
44         try {
45             w = new FileWriter JavaDoc(descriptor);
46         } catch (IOException JavaDoc ex) {
47             throw new JbiPluginException("Exception while opening file["
48                     + descriptor.getAbsolutePath() + "]", ex);
49         }
50
51         XMLWriter writer = new PrettyPrintXMLWriter(w, encoding, null);
52         writer.startElement("jbi");
53         writer.addAttribute("xmlns", "http://java.sun.com/xml/ns/jbi");
54         writer.addAttribute("version", "1.0");
55
56         writer.startElement("service-assembly");
57
58         writer.startElement("identification");
59         writer.startElement("name");
60         writer.writeText(name);
61         writer.endElement();
62         writer.startElement("description");
63         writer.writeText(description);
64         writer.endElement();
65         writer.endElement();
66
67         for (Iterator JavaDoc it = uris.iterator(); it.hasNext();) {
68             DependencyInformation serviceUnitInfo = (DependencyInformation) it
69                     .next();
70             writeServiceUnit(writer, serviceUnitInfo);
71
72         }
73
74         writer.endElement();
75         writer.endElement();
76
77         close(w);
78     }
79
80     private void writeServiceUnit(XMLWriter writer,
81             DependencyInformation serviceUnitInfo) throws JbiPluginException {
82         writer.startElement("service-unit");
83         writer.startElement("identification");
84         writer.startElement("name");
85         writer.writeText(serviceUnitInfo.getName());
86         writer.endElement();
87         if (serviceUnitInfo.getDescription() != null) {
88             writer.startElement("description");
89             writer.writeText(serviceUnitInfo.getDescription());
90             writer.endElement();
91         }
92         writer.endElement();
93
94         writer.startElement("target");
95         writer.startElement("artifacts-zip");
96         writer.writeText(serviceUnitInfo.getFilename());
97         writer.endElement();
98
99         writer.startElement("component-name");
100         writer.writeText(serviceUnitInfo.getComponent());
101         writer.endElement();
102
103         writer.endElement();
104
105         writer.endElement();
106     }
107
108     private void close(Writer JavaDoc closeable) {
109         if (closeable != null) {
110             try {
111                 closeable.close();
112             } catch (Exception JavaDoc e) {
113                 // TODO: warn
114
}
115         }
116     }
117
118 }
119
Popular Tags