1 18 package org.apache.tools.ant.taskdefs.optional.extension; 19 20 import java.io.File ; 21 import java.io.FileOutputStream ; 22 import java.io.IOException ; 23 import java.util.ArrayList ; 24 import java.util.Iterator ; 25 import java.util.jar.Attributes ; 26 import java.util.jar.Manifest ; 27 import org.apache.tools.ant.BuildException; 28 import org.apache.tools.ant.MagicNames; 29 import org.apache.tools.ant.Project; 30 import org.apache.tools.ant.Task; 31 32 47 public final class JarLibManifestTask extends Task { 48 51 private static final String MANIFEST_VERSION = "1.0"; 52 53 56 private static final String CREATED_BY = "Created-By"; 57 58 61 private File destFile; 62 63 66 private Extension extension; 67 68 72 private final ArrayList dependencies = new ArrayList (); 73 74 78 private final ArrayList optionals = new ArrayList (); 79 80 84 private final ArrayList extraAttributes = new ArrayList (); 85 86 91 public void setDestfile(final File destFile) { 92 this.destFile = destFile; 93 } 94 95 103 public void addConfiguredExtension(final ExtensionAdapter extensionAdapter) 104 throws BuildException { 105 if (null != extension) { 106 final String message = 107 "Can not have multiple extensions defined in one library."; 108 throw new BuildException(message); 109 } 110 extension = extensionAdapter.toExtension(); 111 } 112 113 118 public void addConfiguredDepends(final ExtensionSet extensionSet) { 119 dependencies.add(extensionSet); 120 } 121 122 127 public void addConfiguredOptions(final ExtensionSet extensionSet) { 128 optionals.add(extensionSet); 129 } 130 131 136 public void addConfiguredAttribute(final ExtraAttribute attribute) { 137 extraAttributes.add(attribute); 138 } 139 140 145 public void execute() throws BuildException { 146 validate(); 147 148 final Manifest manifest = new Manifest (); 149 final Attributes attributes = manifest.getMainAttributes(); 150 151 attributes.put(Attributes.Name.MANIFEST_VERSION, MANIFEST_VERSION); 152 final String createdBy = "Apache Ant " + getProject().getProperty(MagicNames.ANT_VERSION); 153 attributes.putValue(CREATED_BY, createdBy); 154 155 appendExtraAttributes(attributes); 156 157 if (null != extension) { 158 Extension.addExtension(extension, attributes); 159 } 160 161 final ArrayList depends = toExtensions(dependencies); 163 appendExtensionList(attributes, 164 Extension.EXTENSION_LIST, 165 "lib", 166 depends.size()); 167 appendLibraryList(attributes, "lib", depends); 168 169 final ArrayList option = toExtensions(optionals); 172 appendExtensionList(attributes, 173 Extension.OPTIONAL_EXTENSION_LIST, 174 "opt", 175 option.size()); 176 appendLibraryList(attributes, "opt", option); 177 178 try { 179 final String message = "Generating manifest " + destFile.getAbsoluteFile(); 180 log(message, Project.MSG_INFO); 181 writeManifest(manifest); 182 } catch (final IOException ioe) { 183 throw new BuildException(ioe.getMessage(), ioe); 184 } 185 } 186 187 192 private void validate() throws BuildException { 193 if (null == destFile) { 194 final String message = "Destfile attribute not specified."; 195 throw new BuildException(message); 196 } 197 if (destFile.exists() && !destFile.isFile()) { 198 final String message = destFile + " is not a file."; 199 throw new BuildException(message); 200 } 201 } 202 203 209 private void appendExtraAttributes(final Attributes attributes) { 210 final Iterator iterator = extraAttributes.iterator(); 211 while (iterator.hasNext()) { 212 final ExtraAttribute attribute = 213 (ExtraAttribute) iterator.next(); 214 attributes.putValue(attribute.getName(), 215 attribute.getValue()); 216 } 217 } 218 219 225 private void writeManifest(final Manifest manifest) 226 throws IOException { 227 FileOutputStream output = null; 228 try { 229 output = new FileOutputStream (destFile); 230 manifest.write(output); 231 output.flush(); 232 } finally { 233 if (null != output) { 234 try { 235 output.close(); 236 } catch (IOException e) { 237 } 239 } 240 } 241 } 242 243 254 private void appendLibraryList(final Attributes attributes, 255 final String listPrefix, 256 final ArrayList extensions) 257 throws BuildException { 258 final int size = extensions.size(); 259 for (int i = 0; i < size; i++) { 260 final Extension ext = (Extension) extensions.get(i); 261 final String prefix = listPrefix + i + "-"; 262 Extension.addExtension(ext, prefix, attributes); 263 } 264 } 265 266 277 private void appendExtensionList(final Attributes attributes, 278 final Attributes.Name extensionKey, 279 final String listPrefix, 280 final int size) { 281 final StringBuffer sb = new StringBuffer (); 282 for (int i = 0; i < size; i++) { 283 sb.append(listPrefix); 284 sb.append(i); 285 sb.append(' '); 286 } 287 288 attributes.put(extensionKey, sb.toString()); 291 } 292 293 299 private ArrayList toExtensions(final ArrayList extensionSets) 300 throws BuildException { 301 final ArrayList results = new ArrayList (); 302 303 final int size = extensionSets.size(); 304 for (int i = 0; i < size; i++) { 305 final ExtensionSet set = (ExtensionSet) extensionSets.get(i); 306 final Extension[] extensions = set.toExtensions(getProject()); 307 for (int j = 0; j < extensions.length; j++) { 308 results.add(extensions[ j ]); 309 } 310 } 311 312 return results; 313 } 314 } 315 | Popular Tags |