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.internal.impl.model;
20  
21  import java.util.ArrayList;
22  import java.util.HashMap;
23  import java.util.List;
24  import java.util.Map;
25  import java.util.Objects;
26  import java.util.Optional;
27  import java.util.stream.Collectors;
28  import java.util.stream.Stream;
29  
30  import org.apache.maven.api.model.Model;
31  import org.apache.maven.api.model.Profile;
32  import org.apache.maven.api.services.ModelBuilderResult;
33  import org.apache.maven.api.services.ModelProblem;
34  
35  /**
36   * Collects the output of the model builder.
37   *
38   */
39  class DefaultModelBuilderResult implements ModelBuilderResult {
40      private Model fileModel;
41      private Model activatedFileModel;
42  
43      private Model effectiveModel;
44  
45      private List<String> modelIds;
46  
47      private Map<String, Model> rawModels;
48  
49      private Map<String, List<Profile>> activePomProfiles;
50  
51      private List<Profile> activeExternalProfiles;
52  
53      private List<ModelProblem> problems;
54  
55      DefaultModelBuilderResult() {
56          modelIds = new ArrayList<>();
57          rawModels = new HashMap<>();
58          activePomProfiles = new HashMap<>();
59          activeExternalProfiles = new ArrayList<>();
60          problems = new ArrayList<>();
61      }
62  
63      DefaultModelBuilderResult(ModelBuilderResult result) {
64          this();
65          this.activeExternalProfiles.addAll(result.getActiveExternalProfiles());
66          this.effectiveModel = result.getEffectiveModel();
67          this.fileModel = result.getFileModel();
68          this.problems.addAll(result.getProblems());
69  
70          for (String modelId : result.getModelIds()) {
71              this.modelIds.add(modelId);
72              this.rawModels.put(modelId, result.getRawModel(modelId).orElseThrow());
73              this.activePomProfiles.put(modelId, result.getActivePomProfiles(modelId));
74          }
75      }
76  
77      @Override
78      public Model getFileModel() {
79          return fileModel;
80      }
81  
82      public DefaultModelBuilderResult setFileModel(Model fileModel) {
83          this.fileModel = fileModel;
84          return this;
85      }
86  
87      public Model getActivatedFileModel() {
88          return activatedFileModel;
89      }
90  
91      public DefaultModelBuilderResult setActivatedFileModel(Model activatedFileModel) {
92          this.activatedFileModel = activatedFileModel;
93          return this;
94      }
95  
96      @Override
97      public Model getEffectiveModel() {
98          return effectiveModel;
99      }
100 
101     public DefaultModelBuilderResult setEffectiveModel(Model model) {
102         this.effectiveModel = model;
103         return this;
104     }
105 
106     @Override
107     public List<String> getModelIds() {
108         return modelIds;
109     }
110 
111     public DefaultModelBuilderResult addModelId(String modelId) {
112         // Intentionally notNull because Super POM may not contain a modelId
113         Objects.requireNonNull(modelId, "modelId cannot be null");
114 
115         modelIds.add(modelId);
116 
117         return this;
118     }
119 
120     @Override
121     public Model getRawModel() {
122         return rawModels.get(modelIds.get(0));
123     }
124 
125     @Override
126     public Optional<Model> getRawModel(String modelId) {
127         return Optional.ofNullable(rawModels.get(modelId));
128     }
129 
130     public DefaultModelBuilderResult setRawModel(String modelId, Model rawModel) {
131         // Intentionally notNull because Super POM may not contain a modelId
132         Objects.requireNonNull(modelId, "modelId cannot be null");
133 
134         rawModels.put(modelId, rawModel);
135 
136         return this;
137     }
138 
139     @Override
140     public List<Profile> getActivePomProfiles(String modelId) {
141         List<Profile> profiles = activePomProfiles.get(modelId);
142         return profiles != null ? profiles : List.of();
143     }
144 
145     public DefaultModelBuilderResult setActivePomProfiles(String modelId, List<Profile> activeProfiles) {
146         // Intentionally notNull because Super POM may not contain a modelId
147         Objects.requireNonNull(modelId, "modelId cannot be null");
148 
149         if (activeProfiles != null) {
150             this.activePomProfiles.put(modelId, new ArrayList<>(activeProfiles));
151         } else {
152             this.activePomProfiles.remove(modelId);
153         }
154 
155         return this;
156     }
157 
158     @Override
159     public List<Profile> getActiveExternalProfiles() {
160         return activeExternalProfiles;
161     }
162 
163     public DefaultModelBuilderResult setActiveExternalProfiles(List<Profile> activeProfiles) {
164         if (activeProfiles != null) {
165             this.activeExternalProfiles = new ArrayList<>(activeProfiles);
166         } else {
167             this.activeExternalProfiles.clear();
168         }
169 
170         return this;
171     }
172 
173     @Override
174     public List<ModelProblem> getProblems() {
175         return problems;
176     }
177 
178     public DefaultModelBuilderResult setProblems(List<ModelProblem> problems) {
179         if (problems != null) {
180             this.problems = new ArrayList<>(problems);
181         } else {
182             this.problems.clear();
183         }
184 
185         return this;
186     }
187 
188     public String toString() {
189         if (!modelIds.isEmpty()) {
190             String modelId = modelIds.get(0);
191             StringBuilder sb = new StringBuilder();
192             sb.append(problems.size())
193                     .append(
194                             (problems.size() == 1)
195                                     ? " problem was "
196                                     : " problems were encountered while building the effective model");
197             if (modelId != null && !modelId.isEmpty()) {
198                 sb.append(" for ");
199                 sb.append(modelId);
200             }
201             for (ModelProblem problem : problems) {
202                 sb.append(System.lineSeparator());
203                 sb.append("    - [");
204                 sb.append(problem.getSeverity());
205                 sb.append("] ");
206                 sb.append(problem.getMessage());
207                 String loc = Stream.of(
208                                 problem.getModelId().equals(modelId) ? problem.getModelId() : "",
209                                 problem.getModelId().equals(modelId) ? problem.getSource() : "",
210                                 problem.getLineNumber() > 0 ? "line " + problem.getLineNumber() : "",
211                                 problem.getColumnNumber() > 0 ? "column " + problem.getColumnNumber() : "")
212                         .filter(s -> !s.isEmpty())
213                         .collect(Collectors.joining(", "));
214                 if (!loc.isEmpty()) {
215                     sb.append(" @ ").append(loc);
216                 }
217             }
218             return sb.toString();
219         }
220         return null;
221     }
222 }