View Javadoc

1   package org.apache.maven.lifecycle.internal;
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 org.apache.maven.artifact.Artifact;
23  import org.apache.maven.artifact.handler.ArtifactHandler;
24  import org.apache.maven.artifact.metadata.ArtifactMetadata;
25  import org.apache.maven.artifact.repository.ArtifactRepository;
26  import org.apache.maven.artifact.resolver.filter.ArtifactFilter;
27  import org.apache.maven.artifact.versioning.ArtifactVersion;
28  import org.apache.maven.artifact.versioning.OverConstrainedVersionException;
29  import org.apache.maven.artifact.versioning.VersionRange;
30  
31  import java.io.File;
32  import java.util.Collection;
33  import java.util.List;
34  import java.util.concurrent.CountDownLatch;
35  
36  /**
37   * An artifact that conditionally suspends on getFile for anything but the thread it is locked to.
38   * 
39   * @since 3.0
40   */
41  class ThreadLockedArtifact
42      implements Artifact
43  {
44      private final Artifact real;
45  
46      private final CountDownLatch artifactLocked = new CountDownLatch( 1 );
47  
48      ThreadLockedArtifact( Artifact real )
49      {
50          this.real = real;
51      }
52  
53      public boolean hasReal()
54      {
55          return real != null
56              && ( !( real instanceof ThreadLockedArtifact ) || ( (ThreadLockedArtifact) real ).hasReal() );
57      }
58  
59      public String getGroupId()
60      {
61          return real.getGroupId();
62      }
63  
64      public String getArtifactId()
65      {
66          return real.getArtifactId();
67      }
68  
69      public String getVersion()
70      {
71          return real.getVersion();
72      }
73  
74      public void setVersion( String version )
75      {
76          real.setVersion( version );
77      }
78  
79      public String getScope()
80      {
81          return real.getScope();
82      }
83  
84      public String getType()
85      {
86          return real.getType();
87      }
88  
89      public String getClassifier()
90      {
91          return real.getClassifier();
92      }
93  
94      public boolean hasClassifier()
95      {
96          return real.hasClassifier();
97      }
98  
99      private static final InheritableThreadLocal<ThreadLockedArtifact> THREAD_ARTIFACT =
100         new InheritableThreadLocal<ThreadLockedArtifact>();
101 
102     public void attachToThread()
103     {
104         THREAD_ARTIFACT.set( this );
105     }
106 
107     public File getFile()
108     {
109         final ThreadLockedArtifact lockedArtifact = THREAD_ARTIFACT.get();
110         if ( lockedArtifact != null && this != lockedArtifact && mustLock() )
111         {
112             try
113             {
114                 artifactLocked.await();
115             }
116             catch ( InterruptedException e )
117             {
118                 // Ignore and go on to real.getFile();
119             }
120         }
121         return real.getFile();
122     }
123 
124     private boolean mustLock()
125     {
126         boolean dontNeedLock = CurrentPhaseForThread.isPhase( "compile" ) || CurrentPhaseForThread.isPhase( "test" );
127         return !dontNeedLock;
128     }
129 
130     public void setFile( File destination )
131     {
132         if ( destination != null && destination.exists() && destination.isFile() )
133         {
134             artifactLocked.countDown();
135         }
136         real.setFile( destination );
137     }
138 
139     public String getBaseVersion()
140     {
141         return real.getBaseVersion();
142     }
143 
144     public void setBaseVersion( String baseVersion )
145     {
146         real.setBaseVersion( baseVersion );
147     }
148 
149     public String getId()
150     {
151         return real.getId();
152     }
153 
154     public String getDependencyConflictId()
155     {
156         return real.getDependencyConflictId();
157     }
158 
159     public void addMetadata( ArtifactMetadata metadata )
160     {
161         real.addMetadata( metadata );
162     }
163 
164     public Collection<ArtifactMetadata> getMetadataList()
165     {
166         return real.getMetadataList();
167     }
168 
169     public void setRepository( ArtifactRepository remoteRepository )
170     {
171         real.setRepository( remoteRepository );
172     }
173 
174     public ArtifactRepository getRepository()
175     {
176         return real.getRepository();
177     }
178 
179     public void updateVersion( String version, ArtifactRepository localRepository )
180     {
181         real.updateVersion( version, localRepository );
182     }
183 
184     public String getDownloadUrl()
185     {
186         return real.getDownloadUrl();
187     }
188 
189     public void setDownloadUrl( String downloadUrl )
190     {
191         real.setDownloadUrl( downloadUrl );
192     }
193 
194     public ArtifactFilter getDependencyFilter()
195     {
196         return real.getDependencyFilter();
197     }
198 
199     public void setDependencyFilter( ArtifactFilter artifactFilter )
200     {
201         real.setDependencyFilter( artifactFilter );
202     }
203 
204     public ArtifactHandler getArtifactHandler()
205     {
206         return real.getArtifactHandler();
207     }
208 
209     public List<String> getDependencyTrail()
210     {
211         return real.getDependencyTrail();
212     }
213 
214     public void setDependencyTrail( List<String> dependencyTrail )
215     {
216         real.setDependencyTrail( dependencyTrail );
217     }
218 
219     public void setScope( String scope )
220     {
221         real.setScope( scope );
222     }
223 
224     public VersionRange getVersionRange()
225     {
226         return real.getVersionRange();
227     }
228 
229     public void setVersionRange( VersionRange newRange )
230     {
231         real.setVersionRange( newRange );
232     }
233 
234     public void selectVersion( String version )
235     {
236         real.selectVersion( version );
237     }
238 
239     public void setGroupId( String groupId )
240     {
241         real.setGroupId( groupId );
242     }
243 
244     public void setArtifactId( String artifactId )
245     {
246         real.setArtifactId( artifactId );
247     }
248 
249     public boolean isSnapshot()
250     {
251         return real.isSnapshot();
252     }
253 
254     public void setResolved( boolean resolved )
255     {
256         real.setResolved( resolved );
257     }
258 
259     public boolean isResolved()
260     {
261         return real.isResolved();
262     }
263 
264     public void setResolvedVersion( String version )
265     {
266         real.setResolvedVersion( version );
267     }
268 
269     public void setArtifactHandler( ArtifactHandler handler )
270     {
271         real.setArtifactHandler( handler );
272     }
273 
274     public boolean isRelease()
275     {
276         return real.isRelease();
277     }
278 
279     public void setRelease( boolean release )
280     {
281         real.setRelease( release );
282     }
283 
284     public List<ArtifactVersion> getAvailableVersions()
285     {
286         return real.getAvailableVersions();
287     }
288 
289     public void setAvailableVersions( List<ArtifactVersion> versions )
290     {
291         real.setAvailableVersions( versions );
292     }
293 
294     public boolean isOptional()
295     {
296         return real.isOptional();
297     }
298 
299     public void setOptional( boolean optional )
300     {
301         real.setOptional( optional );
302     }
303 
304     public ArtifactVersion getSelectedVersion()
305         throws OverConstrainedVersionException
306     {
307         return real.getSelectedVersion();
308     }
309 
310     public boolean isSelectedVersionKnown()
311         throws OverConstrainedVersionException
312     {
313         return real.isSelectedVersionKnown();
314     }
315 
316     public int compareTo( Artifact o )
317     {
318         return real.compareTo( o );
319     }
320 }