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.aether;
20
21 import java.io.File;
22 import java.util.Collection;
23 import java.util.List;
24 import org.apache.maven.model.Model;
25 import org.apache.maven.repository.internal.MavenWorkspaceReader;
26 import org.eclipse.aether.artifact.Artifact;
27 import org.eclipse.aether.repository.WorkspaceReader;
28 import org.eclipse.aether.repository.WorkspaceRepository;
29 import org.eclipse.aether.util.repository.ChainedWorkspaceReader;
30
31 /**
32 * A maven workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
33 */
34 public final class MavenChainedWorkspaceReader implements MavenWorkspaceReader {
35
36 private ChainedWorkspaceReader delegate;
37
38 private WorkspaceReader[] readers;
39
40 /**
41 * Creates a new workspace reader by chaining the specified readers.
42 *
43 * @param readers The readers to chain must not be {@code null}.
44 */
45 private MavenChainedWorkspaceReader(WorkspaceReader... readers) {
46 this.delegate = new ChainedWorkspaceReader(readers);
47 this.readers = readers;
48 }
49
50 @Override
51 public Model findModel(Artifact artifact) {
52 for (WorkspaceReader workspaceReader : readers) {
53 if (workspaceReader instanceof MavenWorkspaceReader) {
54 Model model = ((MavenWorkspaceReader) workspaceReader).findModel(artifact);
55 if (model != null) {
56 return model;
57 }
58 }
59 }
60 return null;
61 }
62
63 @Override
64 public WorkspaceRepository getRepository() {
65 return delegate.getRepository();
66 }
67
68 @Override
69 public File findArtifact(Artifact artifact) {
70 return delegate.findArtifact(artifact);
71 }
72
73 @Override
74 public List<String> findVersions(Artifact artifact) {
75 return delegate.findVersions(artifact);
76 }
77
78 /**
79 * chains a collection of {@link WorkspaceReader}s
80 * @param workspaceReaderCollection the collection of readers, might be empty but never <code>null</code>
81 * @return if the collection contains only one item returns the single item, otherwise creates a new
82 * {@link MavenChainedWorkspaceReader} chaining all readers in the order of the given collection.
83 */
84 public static WorkspaceReader of(Collection<WorkspaceReader> workspaceReaderCollection) {
85 WorkspaceReader[] readers = workspaceReaderCollection.toArray(new WorkspaceReader[0]);
86 if (readers.length == 1) {
87 return readers[0];
88 }
89 return new MavenChainedWorkspaceReader(readers);
90 }
91 }