View Javadoc
1   package org.eclipse.aether.util.repository;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.File;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.LinkedHashSet;
27  import java.util.List;
28  
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.repository.WorkspaceReader;
31  import org.eclipse.aether.repository.WorkspaceRepository;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   * A workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
37   */
38  public final class ChainedWorkspaceReader
39      implements WorkspaceReader
40  {
41  
42      private final List<WorkspaceReader> readers = new ArrayList<>();
43  
44      private WorkspaceRepository repository;
45  
46      /**
47       * Creates a new workspace reader by chaining the specified readers.
48       * 
49       * @param readers The readers to chain, may be {@code null}.
50       * @see #newInstance(WorkspaceReader, WorkspaceReader)
51       */
52      public ChainedWorkspaceReader( WorkspaceReader... readers )
53      {
54          if ( readers != null )
55          {
56              Collections.addAll( this.readers, readers );
57          }
58  
59          StringBuilder buffer = new StringBuilder();
60          for ( WorkspaceReader reader : this.readers )
61          {
62              if ( buffer.length() > 0 )
63              {
64                  buffer.append( '+' );
65              }
66              buffer.append( reader.getRepository().getContentType() );
67          }
68  
69          repository = new WorkspaceRepository( buffer.toString(), new Key( this.readers ) );
70      }
71  
72      /**
73       * Creates a new workspace reader by chaining the specified readers. In contrast to the constructor, this factory
74       * method will avoid creating an actual chained reader if one of the specified readers is actually {@code null}.
75       * 
76       * @param reader1 The first workspace reader, may be {@code null}.
77       * @param reader2 The second workspace reader, may be {@code null}.
78       * @return The chained reader or {@code null} if no workspace reader was supplied.
79       */
80      public static WorkspaceReader newInstance( WorkspaceReader reader1, WorkspaceReader reader2 )
81      {
82          if ( reader1 == null )
83          {
84              return reader2;
85          }
86          else if ( reader2 == null )
87          {
88              return reader1;
89          }
90          return new ChainedWorkspaceReader( reader1, reader2 );
91      }
92  
93      public File findArtifact( Artifact artifact )
94      {
95          requireNonNull( artifact, "artifact cannot be null" );
96          File file = null;
97  
98          for ( WorkspaceReader reader : readers )
99          {
100             file = reader.findArtifact( artifact );
101             if ( file != null )
102             {
103                 break;
104             }
105         }
106 
107         return file;
108     }
109 
110     public List<String> findVersions( Artifact artifact )
111     {
112         requireNonNull( artifact, "artifact cannot be null" );
113         Collection<String> versions = new LinkedHashSet<>();
114 
115         for ( WorkspaceReader reader : readers )
116         {
117             versions.addAll( reader.findVersions( artifact ) );
118         }
119 
120         return Collections.unmodifiableList( new ArrayList<>( versions ) );
121     }
122 
123     public WorkspaceRepository getRepository()
124     {
125         Key key = new Key( readers );
126         if ( !key.equals( repository.getKey() ) )
127         {
128             repository = new WorkspaceRepository( repository.getContentType(), key );
129         }
130         return repository;
131     }
132 
133     private static class Key
134     {
135 
136         private final List<Object> keys = new ArrayList<>();
137 
138         Key( List<WorkspaceReader> readers )
139         {
140             for ( WorkspaceReader reader : readers )
141             {
142                 keys.add( reader.getRepository().getKey() );
143             }
144         }
145 
146         @Override
147         public boolean equals( Object obj )
148         {
149             if ( this == obj )
150             {
151                 return true;
152             }
153             if ( obj == null || !getClass().equals( obj.getClass() ) )
154             {
155                 return false;
156             }
157             return keys.equals( ( (Key) obj ).keys );
158         }
159 
160         @Override
161         public int hashCode()
162         {
163             return keys.hashCode();
164         }
165 
166     }
167 
168 }