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 java.util.jar.Manifest;
  22. import org.apache.tools.ant.BuildException;
  23. import org.apache.tools.ant.Task;
  24. /**
  25. * Checks whether an extension is present in a fileset or an extensionSet.
  26. *
  27. * @ant.task name="jarlib-available"
  28. */
  29. public class JarLibAvailableTask extends Task {
  30. /**
  31. * The library to display information about.
  32. */
  33. private File libraryFile;
  34. /**
  35. * Filesets specifying all the librarys
  36. * to display information about.
  37. */
  38. private final Vector extensionFileSets = new Vector();
  39. /**
  40. * The name of the property to set if extension is available.
  41. */
  42. private String propertyName;
  43. /**
  44. * The extension that is required.
  45. */
  46. private ExtensionAdapter requiredExtension;
  47. /**
  48. * The name of property to set if extensions are available.
  49. *
  50. * @param property The name of property to set if extensions is available.
  51. */
  52. public void setProperty(final String property) {
  53. this.propertyName = property;
  54. }
  55. /**
  56. * The JAR library to check.
  57. *
  58. * @param file The jar library to check.
  59. */
  60. public void setFile(final File file) {
  61. this.libraryFile = file;
  62. }
  63. /**
  64. * Set the Extension looking for.
  65. *
  66. * @param extension Set the Extension looking for.
  67. */
  68. public void addConfiguredExtension(final ExtensionAdapter extension) {
  69. if (null != requiredExtension) {
  70. final String message = "Can not specify extension to "
  71. + "search for multiple times.";
  72. throw new BuildException(message);
  73. }
  74. requiredExtension = extension;
  75. }
  76. /**
  77. * Adds a set of extensions to search in.
  78. *
  79. * @param extensionSet a set of extensions to search in.
  80. */
  81. public void addConfiguredExtensionSet(final ExtensionSet extensionSet) {
  82. extensionFileSets.addElement(extensionSet);
  83. }
  84. /**
  85. * Execute the task.
  86. *
  87. * @throws BuildException if somethign goes wrong.
  88. */
  89. public void execute() throws BuildException {
  90. validate();
  91. final Extension test = requiredExtension.toExtension();
  92. // Check if list of files to check has been specified
  93. if (!extensionFileSets.isEmpty()) {
  94. final Iterator iterator = extensionFileSets.iterator();
  95. while (iterator.hasNext()) {
  96. final ExtensionSet extensionSet
  97. = (ExtensionSet) iterator.next();
  98. final Extension[] extensions =
  99. extensionSet.toExtensions(getProject());
  100. for (int i = 0; i < extensions.length; i++) {
  101. final Extension extension = extensions[ i ];
  102. if (extension.isCompatibleWith(test)) {
  103. getProject().setNewProperty(propertyName, "true");
  104. }
  105. }
  106. }
  107. } else {
  108. final Manifest manifest = ExtensionUtil.getManifest(libraryFile);
  109. final Extension[] extensions = Extension.getAvailable(manifest);
  110. for (int i = 0; i < extensions.length; i++) {
  111. final Extension extension = extensions[ i ];
  112. if (extension.isCompatibleWith(test)) {
  113. getProject().setNewProperty(propertyName, "true");
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. * Validate the tasks parameters.
  120. *
  121. * @throws BuildException if invalid parameters found
  122. */
  123. private void validate() throws BuildException {
  124. if (null == requiredExtension) {
  125. final String message = "Extension element must be specified.";
  126. throw new BuildException(message);
  127. }
  128. if (null == libraryFile && extensionFileSets.isEmpty()) {
  129. final String message = "File attribute not specified.";
  130. throw new BuildException(message);
  131. }
  132. if (null != libraryFile && !libraryFile.exists()) {
  133. final String message = "File '" + libraryFile + "' does not exist.";
  134. throw new BuildException(message);
  135. }
  136. if (null != libraryFile && !libraryFile.isFile()) {
  137. final String message = "\'" + libraryFile + "\' is not a file.";
  138. throw new BuildException(message);
  139. }
  140. }
  141. }