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  /**
34   * A workspace reader that delegates to a chain of other readers, effectively aggregating their contents.
35   */
36  public final class ChainedWorkspaceReader
37      implements WorkspaceReader
38  {
39  
40      private List<WorkspaceReader> readers = new ArrayList<WorkspaceReader>();
41  
42      private WorkspaceRepository repository;
43  
44      /**
45       * Creates a new workspace reader by chaining the specified readers.
46       * 
47       * @param readers The readers to chain, may be {@code null}.
48       * @see #newInstance(WorkspaceReader, WorkspaceReader)
49       */
50      public ChainedWorkspaceReader( WorkspaceReader... readers )
51      {
52          if ( readers != null )
53          {
54              Collections.addAll( this.readers, readers );
55          }
56  
57          StringBuilder buffer = new StringBuilder();
58          for ( WorkspaceReader reader : this.readers )
59          {
60              if ( buffer.length() > 0 )
61              {
62                  buffer.append( '+' );
63              }
64              buffer.append( reader.getRepository().getContentType() );
65          }
66  
67          repository = new WorkspaceRepository( buffer.toString(), new Key( this.readers ) );
68      }
69  
70      /**
71       * Creates a new workspace reader by chaining the specified readers. In contrast to the constructor, this factory
72       * method will avoid creating an actual chained reader if one of the specified readers is actually {@code null}.
73       * 
74       * @param reader1 The first workspace reader, may be {@code null}.
75       * @param reader2 The second workspace reader, may be {@code null}.
76       * @return The chained reader or {@code null} if no workspace reader was supplied.
77       */
78      public static WorkspaceReader newInstance( WorkspaceReader reader1, WorkspaceReader reader2 )
79      {
80          if ( reader1 == null )
81          {
82              return reader2;
83          }
84          else if ( reader2 == null )
85          {
86              return reader1;
87          }
88          return new ChainedWorkspaceReader( reader1, reader2 );
89      }
90  
91      public File findArtifact( Artifact artifact )
92      {
93          File file = null;
94  
95          for ( WorkspaceReader reader : readers )
96          {
97              file = reader.findArtifact( artifact );
98              if ( file != null )
99              {
100                 break;
101             }
102         }
103 
104         return file;
105     }
106 
107     public List<String> findVersions( Artifact artifact )
108     {
109         Collection<String> versions = new LinkedHashSet<String>();
110 
111         for ( WorkspaceReader reader : readers )
112         {
113             versions.addAll( reader.findVersions( artifact ) );
114         }
115 
116         return Collections.unmodifiableList( new ArrayList<String>( versions ) );
117     }
118 
119     public WorkspaceRepository getRepository()
120     {
121         Key key = new Key( readers );
122         if ( !key.equals( repository.getKey() ) )
123         {
124             repository = new WorkspaceRepository( repository.getContentType(), key );
125         }
126         return repository;
127     }
128 
129     private static class Key
130     {
131 
132         private final List<Object> keys = new ArrayList<Object>();
133 
134         Key( List<WorkspaceReader> readers )
135         {
136             for ( WorkspaceReader reader : readers )
137             {
138                 keys.add( reader.getRepository().getKey() );
139             }
140         }
141 
142         @Override
143         public boolean equals( Object obj )
144         {
145             if ( this == obj )
146             {
147                 return true;
148             }
149             if ( obj == null || !getClass().equals( obj.getClass() ) )
150             {
151                 return false;
152             }
153             return keys.equals( ( (Key) obj ).keys );
154         }
155 
156         @Override
157         public int hashCode()
158         {
159             return keys.hashCode();
160         }
161 
162     }
163 
164 }