1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.extension;
  18. import java.io.File;
  19. import java.util.Iterator;
  20. import java.util.Vector;
  21. import org.apache.tools.ant.BuildException;
  22. import org.apache.tools.ant.DirectoryScanner;
  23. import org.apache.tools.ant.Task;
  24. import org.apache.tools.ant.types.FileSet;
  25. /**
  26. * Displays the "Optional Package" and "Package Specification" information
  27. * contained within the specified JARs.
  28. *
  29. * <p>Prior to JDK1.3, an "Optional Package" was known as an Extension.
  30. * The specification for this mechanism is available in the JDK1.3
  31. * documentation in the directory
  32. * $JDK_HOME/docs/guide/extensions/versioning.html. Alternatively it is
  33. * available online at <a href="http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html">
  34. * http://java.sun.com/j2se/1.3/docs/guide/extensions/versioning.html</a>.</p>
  35. *
  36. * @ant.task name="jarlib-display"
  37. */
  38. public class JarLibDisplayTask extends Task {
  39. /**
  40. * The library to display information about.
  41. */
  42. private File libraryFile;
  43. /**
  44. * Filesets specifying all the librarys
  45. * to display information about.
  46. */
  47. private final Vector libraryFileSets = new Vector();
  48. /**
  49. * The JAR library to display information for.
  50. *
  51. * @param file The jar library to display information for.
  52. */
  53. public void setFile(final File file) {
  54. this.libraryFile = file;
  55. }
  56. /**
  57. * Adds a set of files about which library data will be displayed.
  58. *
  59. * @param fileSet a set of files about which library data will be displayed.
  60. */
  61. public void addFileset(final FileSet fileSet) {
  62. libraryFileSets.addElement(fileSet);
  63. }
  64. /**
  65. * Execute the task.
  66. *
  67. * @throws BuildException if the task fails.
  68. */
  69. public void execute() throws BuildException {
  70. validate();
  71. final LibraryDisplayer displayer = new LibraryDisplayer();
  72. // Check if list of files to check has been specified
  73. if (!libraryFileSets.isEmpty()) {
  74. final Iterator iterator = libraryFileSets.iterator();
  75. while (iterator.hasNext()) {
  76. final FileSet fileSet = (FileSet) iterator.next();
  77. final DirectoryScanner scanner
  78. = fileSet.getDirectoryScanner(getProject());
  79. final File basedir = scanner.getBasedir();
  80. final String[] files = scanner.getIncludedFiles();
  81. for (int i = 0; i < files.length; i++) {
  82. final File file = new File(basedir, files[ i ]);
  83. displayer.displayLibrary(file);
  84. }
  85. }
  86. } else {
  87. displayer.displayLibrary(libraryFile);
  88. }
  89. }
  90. /**
  91. * Validate the tasks parameters.
  92. *
  93. * @throws BuildException if invalid parameters found
  94. */
  95. private void validate() throws BuildException {
  96. if (null == libraryFile && libraryFileSets.isEmpty()) {
  97. final String message = "File attribute not specified.";
  98. throw new BuildException(message);
  99. }
  100. if (null != libraryFile && !libraryFile.exists()) {
  101. final String message = "File '" + libraryFile + "' does not exist.";
  102. throw new BuildException(message);
  103. }
  104. if (null != libraryFile && !libraryFile.isFile()) {
  105. final String message = "\'" + libraryFile + "\' is not a file.";
  106. throw new BuildException(message);
  107. }
  108. }
  109. }