View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.project;
20  
21  import javax.xml.stream.XMLStreamException;
22  import javax.xml.stream.XMLStreamReader;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.nio.file.Files;
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.jar.JarFile;
31  import java.util.zip.ZipEntry;
32  
33  import com.ctc.wstx.stax.WstxInputFactory;
34  import org.apache.maven.api.xml.XmlNode;
35  import org.apache.maven.internal.xml.XmlNodeStaxBuilder;
36  
37  /**
38   * Creates an extension descriptor from some XML stream.
39   *
40   */
41  public class ExtensionDescriptorBuilder {
42  
43      /**
44       * @since 3.3.0
45       */
46      public String getExtensionDescriptorLocation() {
47          return "META-INF/maven/extension.xml";
48      }
49  
50      /**
51       * Extracts the extension descriptor (if any) from the specified JAR file.
52       *
53       * @param extensionJar The JAR file or directory to extract the descriptor from, must not be {@code null}.
54       * @return The extracted descriptor or {@code null} if no descriptor was found.
55       * @throws IOException If the descriptor is present but could not be parsed.
56       */
57      public ExtensionDescriptor build(File extensionJar) throws IOException {
58          ExtensionDescriptor extensionDescriptor = null;
59  
60          if (extensionJar.isFile()) {
61              try (JarFile pluginJar = new JarFile(extensionJar, false)) {
62                  ZipEntry pluginDescriptorEntry = pluginJar.getEntry(getExtensionDescriptorLocation());
63  
64                  if (pluginDescriptorEntry != null) {
65                      try (InputStream is = pluginJar.getInputStream(pluginDescriptorEntry)) {
66                          extensionDescriptor = build(is);
67                      }
68                  }
69              }
70          } else {
71              File pluginXml = new File(extensionJar, getExtensionDescriptorLocation());
72  
73              if (pluginXml.canRead()) {
74                  try (InputStream is = Files.newInputStream(pluginXml.toPath())) {
75                      extensionDescriptor = build(is);
76                  }
77              }
78          }
79  
80          return extensionDescriptor;
81      }
82  
83      /**
84       * @since 3.3.0
85       */
86      public ExtensionDescriptor build(InputStream is) throws IOException {
87          ExtensionDescriptor extensionDescriptor = new ExtensionDescriptor();
88  
89          XmlNode dom;
90          try {
91              XMLStreamReader reader = WstxInputFactory.newFactory().createXMLStreamReader(is);
92              dom = XmlNodeStaxBuilder.build(reader);
93          } catch (XMLStreamException e) {
94              throw new IOException(e.getMessage(), e);
95          }
96  
97          if (!"extension".equals(dom.getName())) {
98              throw new IOException("Unexpected root element \"" + dom.getName() + "\", expected \"extension\"");
99          }
100 
101         extensionDescriptor.setExportedPackages(parseStrings(dom.getChild("exportedPackages")));
102 
103         extensionDescriptor.setExportedArtifacts(parseStrings(dom.getChild("exportedArtifacts")));
104 
105         return extensionDescriptor;
106     }
107 
108     private List<String> parseStrings(XmlNode dom) {
109         List<String> strings = null;
110 
111         if (dom != null) {
112             strings = new ArrayList<>();
113 
114             for (XmlNode child : dom.getChildren()) {
115                 String string = child.getValue();
116                 if (string != null) {
117                     string = string.trim();
118                     if (!string.isEmpty()) {
119                         strings.add(string);
120                     }
121                 }
122             }
123         }
124 
125         return strings;
126     }
127 }