1 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 24 import org.apache.hivemind.Resource; 25 import org.apache.hivemind.definition.RegistryDefinition; 26 import org.apache.hivemind.impl.RegistryBuilder; 27 import org.apache.hivemind.impl.XmlModuleReader; 28 import org.apache.hivemind.impl.HivemoduleProvider; 29 import org.apache.hivemind.util.FileResource; 30 import org.apache.hivemind.util.URLResource; 31 import org.apache.tools.ant.BuildException; 32 import org.apache.tools.ant.Task; 33 import org.apache.tools.ant.types.Path; 34 import org.apache.xml.serialize.OutputFormat; 35 import org.apache.xml.serialize.XMLSerializer; 36 import org.w3c.dom.Document ; 37 38 62 public class ConstructRegistry extends Task 63 { 64 private File _output; 65 66 private Path _modulesPath; 67 68 public void execute() throws BuildException 69 { 70 if (_output == null) 71 throw new BuildException("You must specify an output file"); 72 73 if (_modulesPath == null) 74 throw new BuildException("You must specify a set of modules"); 75 76 RegistryDefinition registryDefinition = buildRegistry(); 77 Document registry = constructRegistryDocument(registryDefinition); 78 79 log("Writing registry to " + _output); 80 81 writeDocument(registry, _output); 82 83 } 84 85 private RegistryDefinition buildRegistry() 86 { 87 String [] paths = _modulesPath.list(); 88 int count = paths.length; 89 90 File [] modules = new File [count]; 91 92 for (int i = 0; i < count; i++) 93 { 94 File f = new File (paths[i]); 95 96 if (f.isDirectory()) 97 continue; 98 99 modules[i] = f; 100 } 101 RegistryBuilder builder = new RegistryBuilder(); 102 buildRegistry(modules, builder.getRegistryDefinition()); 103 return builder.getRegistryDefinition(); 104 } 105 106 private void buildRegistry(File [] modules, RegistryDefinition definition) throws BuildException 107 { 108 try 109 { 110 for (int i = 0; i < modules.length; i++) 111 enqueue(modules[i], definition); 112 } 113 catch (Exception ex) 114 { 115 throw new BuildException(ex); 116 } 117 } 118 119 123 private void enqueue(File file, RegistryDefinition definition) throws IOException 124 { 125 127 if (file == null) 128 return; 129 130 if (file.getName().endsWith(".jar")) 131 { 132 enqueueJar(file, definition); 133 return; 134 } 135 136 String path = file.getPath().replace('\\', '/'); 137 138 Resource r = new FileResource(path); 139 140 enqueue(r, definition); 141 } 142 143 private void enqueueJar(File jarFile, RegistryDefinition definition) throws IOException 144 { 145 URL jarRootURL = new URL ("jar:" + jarFile.toURL() + "!/" + HivemoduleProvider.HIVE_MODULE_XML); 146 147 Resource jarResource = new URLResource(jarRootURL); 148 enqueue(jarResource, definition); 149 } 150 151 private void enqueue(Resource resource, RegistryDefinition definition) 152 { 153 if (resource.getResourceURL() != null) { 154 XmlModuleReader reader = new XmlModuleReader(definition); 155 reader.readModule(resource); 156 } 157 } 158 159 private Document constructRegistryDocument(RegistryDefinition registryDefinition) throws BuildException 160 { 161 try 162 { 163 RegistrySerializer generator = new RegistrySerializer(); 164 165 Document result = generator.createRegistryDocument(registryDefinition); 166 167 return result; 168 } 169 catch (Exception ex) 170 { 171 throw new BuildException(ex); 172 } 173 } 174 175 private void writeDocument(Document document, File file) throws BuildException 176 { 177 try 178 { 179 OutputStream out = new FileOutputStream (file); 180 BufferedOutputStream buffered = new BufferedOutputStream (out); 181 182 writeDocument(document, buffered); 183 184 buffered.close(); 185 } 186 catch (IOException ex) 187 { 188 throw new BuildException( 189 "Unable to write registry to " + file + ": " + ex.getMessage(), ex); 190 } 191 } 192 193 private void writeDocument(Document document, OutputStream out) throws IOException 194 { 195 XMLSerializer serializer = new XMLSerializer(out, new OutputFormat(document, null, true)); 196 serializer.serialize(document); 197 } 198 199 public File getOutput() 200 { 201 return _output; 202 } 203 204 public void setOutput(File file) 205 { 206 _output = file; 207 } 208 209 public Path createModules() 210 { 211 _modulesPath = new Path(getProject()); 212 return _modulesPath; 213 } 214 215 } | Popular Tags |