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.model.building;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.io.File;
26  import java.io.IOException;
27  import java.io.InputStream;
28  import java.io.Reader;
29  import java.nio.file.Path;
30  import java.util.ArrayList;
31  import java.util.Collection;
32  import java.util.List;
33  import java.util.Map;
34  import java.util.Objects;
35  import java.util.Optional;
36  
37  import org.apache.maven.api.model.Model;
38  import org.apache.maven.api.spi.ModelParser;
39  import org.apache.maven.api.spi.ModelParserException;
40  import org.apache.maven.building.Source;
41  import org.apache.maven.model.io.ModelParseException;
42  import org.apache.maven.model.io.ModelReader;
43  import org.apache.maven.model.locator.ModelLocator;
44  import org.eclipse.sisu.Typed;
45  
46  /**
47   *
48   * Note: uses @Typed to limit the types it is available for injection to just ModelProcessor.
49   *
50   * This is because the ModelProcessor interface extends ModelLocator and ModelReader. If we
51   * made this component available under all its interfaces then it could end up being injected
52   * into itself leading to a stack overflow.
53   *
54   * A side effect of using @Typed is that it translates to explicit bindings in the container.
55   * So instead of binding the component under a 'wildcard' key it is now bound with an explicit
56   * key. Since this is a default component this will be a plain binding of ModelProcessor to
57   * this implementation type, ie. no hint/name.
58   *
59   * This leads to a second side effect in that any @Inject request for just ModelProcessor in
60   * the same injector is immediately matched to this explicit binding, which means extensions
61   * cannot override this binding. This is because the lookup is always short-circuited in this
62   * specific situation (plain @Inject request, and plain explicit binding for the same type.)
63   *
64   * The simplest solution is to use a custom @Named here so it isn't bound under the plain key.
65   * This is only necessary for default components using @Typed that want to support overriding.
66   *
67   * As a non-default component this now gets a negative priority relative to other implementations
68   * of the same interface. Since we want to allow overriding this doesn't matter in this case.
69   * (if it did we could add @Priority of 0 to match the priority given to default components.)
70   */
71  @Named("core-default")
72  @Singleton
73  @Typed(ModelProcessor.class)
74  public class DefaultModelProcessor implements ModelProcessor {
75  
76      private final Collection<ModelParser> modelParsers;
77      private final ModelLocator modelLocator;
78      private final ModelReader modelReader;
79  
80      @Inject
81      public DefaultModelProcessor(
82              Collection<ModelParser> modelParsers, ModelLocator modelLocator, ModelReader modelReader) {
83          this.modelParsers = modelParsers;
84          this.modelLocator = modelLocator;
85          this.modelReader = modelReader;
86      }
87  
88      @Override
89      public File locatePom(File projectDirectory) {
90          return locatePom(projectDirectory.toPath()).toFile();
91      }
92  
93      public Path locatePom(Path projectDirectory) {
94          // Note that the ModelProcessor#locatePom never returns null
95          // while the ModelParser#locatePom needs to return an existing path!
96          Path pom = modelParsers.stream()
97                  .map(m -> m.locate(projectDirectory)
98                          .map(org.apache.maven.api.services.Source::getPath)
99                          .orElse(null))
100                 .filter(Objects::nonNull)
101                 .findFirst()
102                 .orElseGet(
103                         () -> modelLocator.locatePom(projectDirectory.toFile()).toPath());
104         if (!pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
105             throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
106         }
107         return pom;
108     }
109 
110     public File locateExistingPom(File projectDirectory) {
111         Path path = locateExistingPom(projectDirectory.toPath());
112         return path != null ? path.toFile() : null;
113     }
114 
115     public Path locateExistingPom(Path projectDirectory) {
116         // Note that the ModelProcessor#locatePom never returns null
117         // while the ModelParser#locatePom needs to return an existing path!
118         Path pom = modelParsers.stream()
119                 .map(m -> m.locate(projectDirectory).map(s -> s.getPath()).orElse(null))
120                 .filter(Objects::nonNull)
121                 .findFirst()
122                 .orElseGet(() -> {
123                     File f = modelLocator.locateExistingPom(projectDirectory.toFile());
124                     return f != null ? f.toPath() : null;
125                 });
126         if (pom != null && !pom.equals(projectDirectory) && !pom.getParent().equals(projectDirectory)) {
127             throw new IllegalArgumentException("The POM found does not belong to the given directory: " + pom);
128         }
129         return pom;
130     }
131 
132     protected org.apache.maven.api.model.Model read(
133             Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
134         Source source = (Source) options.get(ModelProcessor.SOURCE);
135         if (pomFile == null && source instanceof org.apache.maven.building.FileSource) {
136             pomFile = ((org.apache.maven.building.FileSource) source).getFile().toPath();
137         }
138         if (pomFile != null) {
139             Path projectDirectory = pomFile.getParent();
140             List<ModelParserException> exceptions = new ArrayList<>();
141             for (ModelParser parser : modelParsers) {
142                 try {
143                     Optional<Model> model = parser.locateAndParse(projectDirectory, options);
144                     if (model.isPresent()) {
145                         return model.get().withPomFile(pomFile);
146                     }
147                 } catch (ModelParserException e) {
148                     exceptions.add(e);
149                 }
150             }
151             try {
152                 return readXmlModel(pomFile, null, null, options);
153             } catch (IOException e) {
154                 exceptions.forEach(e::addSuppressed);
155                 throw e;
156             }
157         } else {
158             return readXmlModel(pomFile, input, reader, options);
159         }
160     }
161 
162     private org.apache.maven.api.model.Model readXmlModel(
163             Path pomFile, InputStream input, Reader reader, Map<String, ?> options) throws IOException {
164         if (pomFile != null) {
165             return modelReader.read(pomFile.toFile(), options).getDelegate();
166         } else if (input != null) {
167             return modelReader.read(input, options).getDelegate();
168         } else {
169             return modelReader.read(reader, options).getDelegate();
170         }
171     }
172 
173     @Override
174     public org.apache.maven.model.Model read(File file, Map<String, ?> options) throws IOException {
175         Objects.requireNonNull(file, "file cannot be null");
176         Path path = file.toPath();
177         org.apache.maven.api.model.Model model = read(path, null, null, options);
178         return new org.apache.maven.model.Model(model);
179     }
180 
181     @Override
182     public org.apache.maven.model.Model read(InputStream input, Map<String, ?> options) throws IOException {
183         Objects.requireNonNull(input, "input cannot be null");
184         try (InputStream in = input) {
185             org.apache.maven.api.model.Model model = read(null, in, null, options);
186             return new org.apache.maven.model.Model(model);
187         } catch (ModelParserException e) {
188             throw new ModelParseException("Unable to read model: " + e, e.getLineNumber(), e.getColumnNumber(), e);
189         }
190     }
191 
192     @Override
193     public org.apache.maven.model.Model read(Reader reader, Map<String, ?> options) throws IOException {
194         Objects.requireNonNull(reader, "reader cannot be null");
195         try (Reader r = reader) {
196             org.apache.maven.api.model.Model model = read(null, null, r, options);
197             return new org.apache.maven.model.Model(model);
198         }
199     }
200 }