1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.ant; |
16 |
| |
17 |
| import java.io.BufferedOutputStream; |
18 |
| import java.io.File; |
19 |
| import java.io.FileOutputStream; |
20 |
| import java.io.IOException; |
21 |
| import java.io.OutputStream; |
22 |
| import java.net.URL; |
23 |
| import java.util.ArrayList; |
24 |
| import java.util.List; |
25 |
| |
26 |
| import org.apache.hivemind.ModuleDescriptorProvider; |
27 |
| import org.apache.hivemind.Resource; |
28 |
| import org.apache.hivemind.impl.DefaultClassResolver; |
29 |
| import org.apache.hivemind.impl.XmlModuleDescriptorProvider; |
30 |
| import org.apache.hivemind.util.FileResource; |
31 |
| import org.apache.hivemind.util.URLResource; |
32 |
| import org.apache.tools.ant.BuildException; |
33 |
| import org.apache.tools.ant.Task; |
34 |
| import org.apache.tools.ant.types.Path; |
35 |
| import org.apache.xml.serialize.OutputFormat; |
36 |
| import org.apache.xml.serialize.XMLSerializer; |
37 |
| import org.w3c.dom.Document; |
38 |
| |
39 |
| |
40 |
| |
41 |
| |
42 |
| |
43 |
| |
44 |
| |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| public class ConstructRegistry extends Task |
57 |
| { |
58 |
| private File _output; |
59 |
| |
60 |
| private Path _descriptorsPath; |
61 |
| |
62 |
| |
63 |
| |
64 |
| |
65 |
| private List _resourceQueue = new ArrayList(); |
66 |
| |
67 |
7
| public void execute() throws BuildException
|
68 |
| { |
69 |
7
| if (_output == null)
|
70 |
1
| throw new BuildException("You must specify an output file");
|
71 |
| |
72 |
6
| if (_descriptorsPath == null)
|
73 |
1
| throw new BuildException("You must specify a set of module descriptors");
|
74 |
| |
75 |
5
| long outputStamp = _output.lastModified();
|
76 |
| |
77 |
5
| String[] paths = _descriptorsPath.list();
|
78 |
5
| int count = paths.length;
|
79 |
| |
80 |
5
| boolean needsUpdate = false;
|
81 |
| |
82 |
5
| File[] descriptors = new File[count];
|
83 |
| |
84 |
5
| for (int i = 0; i < count; i++)
|
85 |
| { |
86 |
10
| File f = new File(paths[i]);
|
87 |
| |
88 |
10
| if (f.isDirectory())
|
89 |
0
| continue;
|
90 |
| |
91 |
10
| if (f.lastModified() > outputStamp)
|
92 |
8
| needsUpdate = true;
|
93 |
| |
94 |
10
| descriptors[i] = f;
|
95 |
| } |
96 |
| |
97 |
5
| if (needsUpdate)
|
98 |
| { |
99 |
4
| Document registry = constructRegistry(descriptors);
|
100 |
| |
101 |
4
| log("Writing registry to " + _output);
|
102 |
| |
103 |
4
| writeDocument(registry, _output);
|
104 |
| } |
105 |
| |
106 |
| } |
107 |
| |
108 |
4
| private Document constructRegistry(File[] moduleDescriptors) throws BuildException
|
109 |
| { |
110 |
4
| try
|
111 |
| { |
112 |
4
| enqueue(moduleDescriptors);
|
113 |
| |
114 |
4
| ModuleDescriptorProvider provider = new XmlModuleDescriptorProvider(
|
115 |
| new DefaultClassResolver(), _resourceQueue); |
116 |
| |
117 |
4
| RegistrySerializer generator = new RegistrySerializer();
|
118 |
| |
119 |
4
| generator.addModuleDescriptorProvider(provider);
|
120 |
| |
121 |
4
| Document result = generator.createRegistryDocument();
|
122 |
| |
123 |
4
| return result;
|
124 |
| } |
125 |
| catch (Exception ex) |
126 |
| { |
127 |
0
| throw new BuildException(ex);
|
128 |
| } |
129 |
| } |
130 |
| |
131 |
4
| private void enqueue(File[] descriptors) throws IOException
|
132 |
| { |
133 |
4
| for (int i = 0; i < descriptors.length; i++)
|
134 |
8
| enqueue(descriptors[i]);
|
135 |
| } |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
8
| private void enqueue(File file) throws IOException
|
142 |
| { |
143 |
| |
144 |
| |
145 |
8
| if (file == null)
|
146 |
0
| return;
|
147 |
| |
148 |
8
| if (file.getName().endsWith(".jar"))
|
149 |
| { |
150 |
2
| enqueueJar(file);
|
151 |
2
| return;
|
152 |
| } |
153 |
| |
154 |
6
| String path = file.getPath().replace('\\', '/');
|
155 |
| |
156 |
6
| Resource r = new FileResource(path);
|
157 |
| |
158 |
6
| enqueue(r);
|
159 |
| } |
160 |
| |
161 |
7
| private void enqueue(Resource resource)
|
162 |
| { |
163 |
7
| if (!_resourceQueue.contains(resource))
|
164 |
7
| _resourceQueue.add(resource);
|
165 |
| } |
166 |
| |
167 |
2
| private void enqueueJar(File jarFile) throws IOException
|
168 |
| { |
169 |
2
| URL jarRootURL = new URL("jar:" + jarFile.toURL() + "!/");
|
170 |
| |
171 |
2
| Resource jarResource = new URLResource(jarRootURL);
|
172 |
| |
173 |
2
| enqueueIfExists(jarResource, XmlModuleDescriptorProvider.HIVE_MODULE_XML);
|
174 |
| } |
175 |
| |
176 |
2
| private void enqueueIfExists(Resource jarResource, String path)
|
177 |
| { |
178 |
2
| Resource r = jarResource.getRelativeResource(path);
|
179 |
| |
180 |
2
| if (r.getResourceURL() != null)
|
181 |
1
| enqueue(r);
|
182 |
| } |
183 |
| |
184 |
4
| private void writeDocument(Document document, File file) throws BuildException
|
185 |
| { |
186 |
4
| try
|
187 |
| { |
188 |
4
| OutputStream out = new FileOutputStream(file);
|
189 |
4
| BufferedOutputStream buffered = new BufferedOutputStream(out);
|
190 |
| |
191 |
4
| writeDocument(document, buffered);
|
192 |
| |
193 |
4
| buffered.close();
|
194 |
| } |
195 |
| catch (IOException ex) |
196 |
| { |
197 |
0
| throw new BuildException(
|
198 |
| "Unable to write registry to " + file + ": " + ex.getMessage(), ex); |
199 |
| } |
200 |
| } |
201 |
| |
202 |
4
| private void writeDocument(Document document, OutputStream out) throws IOException
|
203 |
| { |
204 |
4
| XMLSerializer serializer = new XMLSerializer(out, new OutputFormat(document, null, true));
|
205 |
4
| serializer.serialize(document);
|
206 |
| } |
207 |
| |
208 |
4
| public Path createDescriptors()
|
209 |
| { |
210 |
4
| _descriptorsPath = new Path(getProject());
|
211 |
4
| return _descriptorsPath;
|
212 |
| } |
213 |
| |
214 |
1
| public File getOutput()
|
215 |
| { |
216 |
1
| return _output;
|
217 |
| } |
218 |
| |
219 |
5
| public void setOutput(File file)
|
220 |
| { |
221 |
5
| _output = file;
|
222 |
| } |
223 |
| |
224 |
| } |