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.resolver;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  import java.util.Objects;
29  import java.util.stream.Collectors;
30  
31  import org.apache.maven.api.model.Model;
32  import org.apache.maven.internal.impl.resolver.MavenWorkspaceReader;
33  import org.eclipse.aether.artifact.Artifact;
34  import org.eclipse.aether.repository.WorkspaceReader;
35  import org.eclipse.aether.repository.WorkspaceRepository;
36  
37  import static java.util.Objects.requireNonNull;
38  
39  /**
40   * A maven workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
41   * <p>
42   * This class, while technically is not immutable, should be considered as such once set up. If not mutated, it is also
43   * thread-safe. <em>The mutation of this class instances should happen beforehand their use in session</em>.
44   */
45  public class MavenChainedWorkspaceReader implements MavenWorkspaceReader {
46  
47      protected List<WorkspaceReader> readers;
48      protected WorkspaceRepository repository;
49  
50      /**
51       * Creates a new workspace reader by chaining the specified readers.
52       *
53       * @param readers The readers to chain must not be {@code null}.
54       */
55      public MavenChainedWorkspaceReader(WorkspaceReader... readers) {
56          setReaders(Arrays.asList(readers));
57      }
58  
59      @Override
60      public WorkspaceRepository getRepository() {
61          return this.repository;
62      }
63  
64      @Override
65      public Model findModel(Artifact artifact) {
66          requireNonNull(artifact, "artifact cannot be null");
67          Model model = null;
68  
69          for (WorkspaceReader workspaceReader : readers) {
70              if (workspaceReader instanceof MavenWorkspaceReader) {
71                  model = ((MavenWorkspaceReader) workspaceReader).findModel(artifact);
72                  if (model != null) {
73                      break;
74                  }
75              }
76          }
77  
78          return model;
79      }
80  
81      @Override
82      public File findArtifact(Artifact artifact) {
83          requireNonNull(artifact, "artifact cannot be null");
84          File file = null;
85  
86          for (WorkspaceReader reader : readers) {
87              file = reader.findArtifact(artifact);
88              if (file != null) {
89                  break;
90              }
91          }
92  
93          return file;
94      }
95  
96      @Override
97      public List<String> findVersions(Artifact artifact) {
98          requireNonNull(artifact, "artifact cannot be null");
99          Collection<String> versions = new LinkedHashSet<>();
100 
101         for (WorkspaceReader reader : readers) {
102             versions.addAll(reader.findVersions(artifact));
103         }
104 
105         return Collections.unmodifiableList(new ArrayList<>(versions));
106     }
107 
108     public void setReaders(Collection<WorkspaceReader> readers) {
109         requireNonNull(readers, "readers");
110         // skip possible null entries
111         this.readers = Collections.unmodifiableList(
112                 new ArrayList<>(readers.stream().filter(Objects::nonNull).collect(Collectors.toList())));
113         Key key = new Key(this.readers);
114         this.repository = new WorkspaceRepository(key.getContentType(), key);
115     }
116 
117     public List<WorkspaceReader> getReaders() {
118         return readers;
119     }
120 
121     public void addReader(WorkspaceReader workspaceReader) {
122         requireNonNull(workspaceReader, "workspaceReader");
123         ArrayList<WorkspaceReader> newReaders = new ArrayList<>(this.readers);
124         newReaders.add(workspaceReader);
125         setReaders(newReaders);
126     }
127 
128     private static class Key {
129         private final List<Object> keys;
130         private final String type;
131 
132         Key(Collection<WorkspaceReader> readers) {
133             keys = readers.stream().map(r -> r.getRepository().getKey()).collect(Collectors.toList());
134             type = readers.stream().map(r -> r.getRepository().getContentType()).collect(Collectors.joining("+"));
135         }
136 
137         public String getContentType() {
138             return type;
139         }
140 
141         public boolean equals(Object obj) {
142             if (this == obj) {
143                 return true;
144             } else {
145                 return obj != null && this.getClass().equals(obj.getClass()) && this.keys.equals(((Key) obj).keys);
146             }
147         }
148 
149         public int hashCode() {
150             return this.keys.hashCode();
151         }
152     }
153 
154     /**
155      * chains a collection of {@link WorkspaceReader}s
156      * @param workspaceReaderCollection the collection of readers, might be empty but never <code>null</code>
157      * @return if the collection contains only one item returns the single item, otherwise creates a new
158      *         {@link MavenChainedWorkspaceReader} chaining all readers in the order of the given collection.
159      */
160     public static WorkspaceReader of(Collection<WorkspaceReader> workspaceReaderCollection) {
161         WorkspaceReader[] readers = workspaceReaderCollection.toArray(new WorkspaceReader[0]);
162         if (readers.length == 1) {
163             return readers[0];
164         }
165         return new MavenChainedWorkspaceReader(readers);
166     }
167 }