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.Collections;
23  import java.util.List;
24  import java.util.concurrent.CopyOnWriteArrayList;
25  import java.util.stream.Collectors;
26  import java.util.stream.Stream;
27  
28  import org.apache.maven.api.model.Model;
29  import org.apache.maven.api.model.Profile;
30  import org.apache.maven.api.services.ModelBuilderResult;
31  import org.apache.maven.api.services.ModelProblem;
32  import org.apache.maven.api.services.ModelSource;
33  
34  /**
35   * Collects the output of the model builder.
36   */
37  class DefaultModelBuilderResult implements ModelBuilderResult {
38      private ModelSource source;
39      private Model fileModel;
40      private Model rawModel;
41      private Model parentModel;
42      private Model effectiveModel;
43      private List<Profile> activePomProfiles;
44      private List<Profile> activeExternalProfiles;
45      private final List<ModelProblem> problems = new CopyOnWriteArrayList<>();
46      private final DefaultModelBuilderResult problemHolder;
47  
48      private final List<DefaultModelBuilderResult> children = new ArrayList<>();
49  
50      DefaultModelBuilderResult() {
51          this(null);
52      }
53  
54      DefaultModelBuilderResult(DefaultModelBuilderResult problemHolder) {
55          this.problemHolder = problemHolder;
56      }
57  
58      public ModelSource getSource() {
59          return source;
60      }
61  
62      public void setSource(ModelSource source) {
63          this.source = source;
64      }
65  
66      @Override
67      public Model getFileModel() {
68          return fileModel;
69      }
70  
71      public void setFileModel(Model fileModel) {
72          this.fileModel = fileModel;
73      }
74  
75      @Override
76      public Model getRawModel() {
77          return rawModel;
78      }
79  
80      public void setRawModel(Model rawModel) {
81          this.rawModel = rawModel;
82      }
83  
84      @Override
85      public Model getParentModel() {
86          return parentModel;
87      }
88  
89      public void setParentModel(Model parentModel) {
90          this.parentModel = parentModel;
91      }
92  
93      @Override
94      public Model getEffectiveModel() {
95          return effectiveModel;
96      }
97  
98      public void setEffectiveModel(Model model) {
99          this.effectiveModel = model;
100     }
101 
102     @Override
103     public List<Profile> getActivePomProfiles() {
104         return activePomProfiles;
105     }
106 
107     public void setActivePomProfiles(List<Profile> activeProfiles) {
108         this.activePomProfiles = activeProfiles;
109     }
110 
111     @Override
112     public List<Profile> getActiveExternalProfiles() {
113         return activeExternalProfiles;
114     }
115 
116     public void setActiveExternalProfiles(List<Profile> activeProfiles) {
117         this.activeExternalProfiles = activeProfiles;
118     }
119 
120     /**
121      * Returns an unmodifiable list of problems encountered during the model building process.
122      *
123      * @return a list of ModelProblem instances representing the encountered problems,
124      *         guaranteed to be non-null but possibly empty.
125      */
126     @Override
127     public List<ModelProblem> getProblems() {
128         return Collections.unmodifiableList(problems);
129     }
130 
131     /**
132      * Adds a given problem to the list of problems and propagates it to the parent result if present.
133      *
134      * @param problem The problem to be added. It must be an instance of ModelProblem.
135      */
136     public void addProblem(ModelProblem problem) {
137         problems.add(problem);
138         if (problemHolder != null) {
139             problemHolder.addProblem(problem);
140         }
141     }
142 
143     @Override
144     public List<DefaultModelBuilderResult> getChildren() {
145         return children;
146     }
147 
148     public String toString() {
149         String modelId;
150         if (effectiveModel != null) {
151             modelId = effectiveModel.getId();
152         } else if (rawModel != null) {
153             modelId = rawModel.getId();
154         } else if (fileModel != null) {
155             modelId = fileModel.getId();
156         } else {
157             modelId = null;
158         }
159         if (!problems.isEmpty()) {
160             StringBuilder sb = new StringBuilder();
161             sb.append(problems.size())
162                     .append(
163                             (problems.size() == 1)
164                                     ? " problem was "
165                                     : " problems were encountered while building the effective model");
166             if (modelId != null && !modelId.isEmpty()) {
167                 sb.append(" for ");
168                 sb.append(modelId);
169             }
170             for (ModelProblem problem : problems) {
171                 sb.append(System.lineSeparator());
172                 sb.append("    - [");
173                 sb.append(problem.getSeverity());
174                 sb.append("] ");
175                 if (problem.getMessage() != null && !problem.getMessage().isEmpty()) {
176                     sb.append(problem.getMessage());
177                 } else if (problem.getException() != null) {
178                     sb.append(problem.getException().toString());
179                 }
180                 String loc = Stream.of(
181                                 problem.getModelId().equals(modelId) ? problem.getModelId() : "",
182                                 problem.getModelId().equals(modelId) ? problem.getSource() : "",
183                                 problem.getLineNumber() > 0 ? "line " + problem.getLineNumber() : "",
184                                 problem.getColumnNumber() > 0 ? "column " + problem.getColumnNumber() : "")
185                         .filter(s -> !s.isEmpty())
186                         .collect(Collectors.joining(", "));
187                 if (!loc.isEmpty()) {
188                     sb.append(" @ ").append(loc);
189                 }
190             }
191             return sb.toString();
192         }
193         return modelId;
194     }
195 }