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.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.Iterator;
  22. import java.util.jar.JarFile;
  23. import java.util.jar.Manifest;
  24. import org.apache.tools.ant.BuildException;
  25. import org.apache.tools.ant.DirectoryScanner;
  26. import org.apache.tools.ant.Project;
  27. import org.apache.tools.ant.types.FileSet;
  28. /**
  29. * A set of useful methods relating to extensions.
  30. *
  31. * @version $Revision: 1.4.2.6 $ $Date: 2004/03/09 17:01:45 $
  32. */
  33. public class ExtensionUtil {
  34. /**
  35. * Class is not meant to be instantiated.
  36. */
  37. private ExtensionUtil() {
  38. }
  39. /**
  40. * Convert a list of extensionAdapter objects to extensions.
  41. *
  42. * @param adapters the list of ExtensionAdapterss to add to convert
  43. * @throws BuildException if an error occurs
  44. */
  45. static ArrayList toExtensions(final ArrayList adapters)
  46. throws BuildException {
  47. final ArrayList results = new ArrayList();
  48. final int size = adapters.size();
  49. for (int i = 0; i < size; i++) {
  50. final ExtensionAdapter adapter =
  51. (ExtensionAdapter) adapters.get(i);
  52. final Extension extension = adapter.toExtension();
  53. results.add(extension);
  54. }
  55. return results;
  56. }
  57. /**
  58. * Generate a list of extensions from a specified fileset.
  59. *
  60. * @param librarys the list to add extensions to
  61. * @param fileset the filesets containing librarys
  62. * @throws BuildException if an error occurs
  63. */
  64. static void extractExtensions(final Project project,
  65. final ArrayList librarys,
  66. final ArrayList fileset)
  67. throws BuildException {
  68. if (!fileset.isEmpty()) {
  69. final Extension[] extensions = getExtensions(project,
  70. fileset);
  71. for (int i = 0; i < extensions.length; i++) {
  72. librarys.add(extensions[ i ]);
  73. }
  74. }
  75. }
  76. /**
  77. * Retrieve extensions from the specified librarys.
  78. *
  79. * @param librarys the filesets for librarys
  80. * @return the extensions contained in librarys
  81. * @throws BuildException if failing to scan librarys
  82. */
  83. private static Extension[] getExtensions(final Project project,
  84. final ArrayList librarys)
  85. throws BuildException {
  86. final ArrayList extensions = new ArrayList();
  87. final Iterator iterator = librarys.iterator();
  88. while (iterator.hasNext()) {
  89. final FileSet fileSet = (FileSet) iterator.next();
  90. boolean includeImpl = true;
  91. boolean includeURL = true;
  92. if (fileSet instanceof LibFileSet) {
  93. LibFileSet libFileSet = (LibFileSet) fileSet;
  94. includeImpl = libFileSet.isIncludeImpl();
  95. includeURL = libFileSet.isIncludeURL();
  96. }
  97. final DirectoryScanner scanner = fileSet.getDirectoryScanner(project);
  98. final File basedir = scanner.getBasedir();
  99. final String[] files = scanner.getIncludedFiles();
  100. for (int i = 0; i < files.length; i++) {
  101. final File file = new File(basedir, files[ i ]);
  102. loadExtensions(file, extensions, includeImpl, includeURL);
  103. }
  104. }
  105. return (Extension[]) extensions.toArray(new Extension[extensions.size()]);
  106. }
  107. /**
  108. * Load list of available extensions from specified file.
  109. *
  110. * @param file the file
  111. * @param extensionList the list to add available extensions to
  112. * @throws BuildException if there is an error
  113. */
  114. private static void loadExtensions(final File file,
  115. final ArrayList extensionList,
  116. final boolean includeImpl,
  117. final boolean includeURL)
  118. throws BuildException {
  119. try {
  120. final JarFile jarFile = new JarFile(file);
  121. final Extension[] extensions =
  122. Extension.getAvailable(jarFile.getManifest());
  123. for (int i = 0; i < extensions.length; i++) {
  124. final Extension extension = extensions[ i ];
  125. addExtension(extensionList, extension, includeImpl, includeURL);
  126. }
  127. } catch (final Exception e) {
  128. throw new BuildException(e.getMessage(), e);
  129. }
  130. }
  131. /**
  132. * Add extension to list.
  133. * If extension should not have implementation details but
  134. * does strip them. If extension should not have url but does
  135. * then strip it.
  136. *
  137. * @param extensionList the list of extensions to add to
  138. * @param originalExtension the extension
  139. * @param includeImpl false to exclude implementation details
  140. * @param includeURL false to exclude implementation URL
  141. */
  142. private static void addExtension(final ArrayList extensionList,
  143. final Extension originalExtension,
  144. final boolean includeImpl,
  145. final boolean includeURL) {
  146. Extension extension = originalExtension;
  147. if (!includeURL
  148. && null != extension.getImplementationURL()) {
  149. extension =
  150. new Extension(extension.getExtensionName(),
  151. extension.getSpecificationVersion().toString(),
  152. extension.getSpecificationVendor(),
  153. extension.getImplementationVersion().toString(),
  154. extension.getImplementationVendor(),
  155. extension.getImplementationVendorID(),
  156. null);
  157. }
  158. final boolean hasImplAttributes =
  159. null != extension.getImplementationURL()
  160. || null != extension.getImplementationVersion()
  161. || null != extension.getImplementationVendorID()
  162. || null != extension.getImplementationVendor();
  163. if (!includeImpl && hasImplAttributes) {
  164. extension =
  165. new Extension(extension.getExtensionName(),
  166. extension.getSpecificationVersion().toString(),
  167. extension.getSpecificationVendor(),
  168. null,
  169. null,
  170. null,
  171. extension.getImplementationURL());
  172. }
  173. extensionList.add(extension);
  174. }
  175. /**
  176. * retrieve manifest for specified file.
  177. *
  178. * @param file the file
  179. * @return the manifest
  180. * @throws BuildException if errror occurs (file not exist,
  181. * file not a jar, manifest not exist in file)
  182. */
  183. static Manifest getManifest(final File file)
  184. throws BuildException {
  185. try {
  186. final JarFile jarFile = new JarFile(file);
  187. Manifest m = jarFile.getManifest();
  188. if (m == null) {
  189. throw new BuildException(file + " doesn't have a MANIFEST");
  190. }
  191. return m;
  192. } catch (final IOException ioe) {
  193. throw new BuildException(ioe.getMessage(), ioe);
  194. }
  195. }
  196. }