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.eclipse.aether.util.repository;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.LinkedHashSet;
26  import java.util.List;
27  
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.repository.WorkspaceReader;
30  import org.eclipse.aether.repository.WorkspaceRepository;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   * A workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
36   */
37  public final class ChainedWorkspaceReader implements WorkspaceReader {
38  
39      private final List<WorkspaceReader> readers = new ArrayList<>();
40  
41      private WorkspaceRepository repository;
42  
43      /**
44       * Creates a new workspace reader by chaining the specified readers.
45       *
46       * @param readers The readers to chain, may be {@code null}.
47       * @see #newInstance(WorkspaceReader, WorkspaceReader)
48       */
49      public ChainedWorkspaceReader(WorkspaceReader... readers) {
50          if (readers != null) {
51              Collections.addAll(this.readers, readers);
52          }
53  
54          StringBuilder buffer = new StringBuilder();
55          for (WorkspaceReader reader : this.readers) {
56              if (buffer.length() > 0) {
57                  buffer.append('+');
58              }
59              buffer.append(reader.getRepository().getContentType());
60          }
61  
62          repository = new WorkspaceRepository(buffer.toString(), new Key(this.readers));
63      }
64  
65      /**
66       * Creates a new workspace reader by chaining the specified readers. In contrast to the constructor, this factory
67       * method will avoid creating an actual chained reader if one of the specified readers is actually {@code null}.
68       *
69       * @param reader1 The first workspace reader, may be {@code null}.
70       * @param reader2 The second workspace reader, may be {@code null}.
71       * @return The chained reader or {@code null} if no workspace reader was supplied.
72       */
73      public static WorkspaceReader newInstance(WorkspaceReader reader1, WorkspaceReader reader2) {
74          if (reader1 == null) {
75              return reader2;
76          } else if (reader2 == null) {
77              return reader1;
78          }
79          return new ChainedWorkspaceReader(reader1, reader2);
80      }
81  
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      public List<String> findVersions(Artifact artifact) {
97          requireNonNull(artifact, "artifact cannot be null");
98          Collection<String> versions = new LinkedHashSet<>();
99  
100         for (WorkspaceReader reader : readers) {
101             versions.addAll(reader.findVersions(artifact));
102         }
103 
104         return Collections.unmodifiableList(new ArrayList<>(versions));
105     }
106 
107     public WorkspaceRepository getRepository() {
108         Key key = new Key(readers);
109         if (!key.equals(repository.getKey())) {
110             repository = new WorkspaceRepository(repository.getContentType(), key);
111         }
112         return repository;
113     }
114 
115     private static class Key {
116 
117         private final List<Object> keys = new ArrayList<>();
118 
119         Key(List<WorkspaceReader> readers) {
120             for (WorkspaceReader reader : readers) {
121                 keys.add(reader.getRepository().getKey());
122             }
123         }
124 
125         @Override
126         public boolean equals(Object obj) {
127             if (this == obj) {
128                 return true;
129             }
130             if (obj == null || !getClass().equals(obj.getClass())) {
131                 return false;
132             }
133             return keys.equals(((Key) obj).keys);
134         }
135 
136         @Override
137         public int hashCode() {
138             return keys.hashCode();
139         }
140     }
141 }