KickJava   Java API By Example, From Geeks To Geeks.

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


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