View Javadoc
1   package org.eclipse.aether.synccontext;
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.util.Collection;
23  import java.util.concurrent.locks.Lock;
24  import java.util.concurrent.locks.ReentrantReadWriteLock;
25  
26  import javax.annotation.Priority;
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  
30  import org.eclipse.aether.RepositorySystemSession;
31  import org.eclipse.aether.SyncContext;
32  import org.eclipse.aether.artifact.Artifact;
33  import org.eclipse.aether.impl.SyncContextFactory;
34  import org.eclipse.aether.metadata.Metadata;
35  import org.slf4j.Logger;
36  import org.slf4j.LoggerFactory;
37  
38  /**
39   * A singleton factory to create synchronization contexts using a global lock based on
40   * {@link ReentrantReadWriteLock}. Explicit artifacts and metadata passed are ignored.
41   * <p>
42   * <strong>Note: This component is still considered to be experimental, use with caution!</strong>
43   */
44  @Named
45  @Priority( Integer.MAX_VALUE )
46  @Singleton
47  public class GlobalSyncContextFactory
48      implements SyncContextFactory
49  {
50      private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
51  
52      public SyncContext newInstance( RepositorySystemSession session, boolean shared )
53      {
54          return new GlobalSyncContext( shared ? lock.readLock() : lock.writeLock(), shared );
55      }
56  
57      static class GlobalSyncContext
58          implements SyncContext
59      {
60          private static final Logger LOGGER = LoggerFactory.getLogger( GlobalSyncContext.class );
61  
62          private final Lock lock;
63          private final boolean shared;
64          private int lockHoldCount;
65  
66          private GlobalSyncContext( Lock lock, boolean shared )
67          {
68              this.lock = lock;
69              this.shared = shared;
70          }
71  
72          public void acquire( Collection<? extends Artifact> artifact, Collection<? extends Metadata> metadata )
73          {
74              LOGGER.trace( "Acquiring global {} lock (currently held: {})",
75                            shared ? "read" : "write", lockHoldCount );
76              lock.lock();
77              lockHoldCount++;
78          }
79  
80          public void close()
81          {
82              while ( lockHoldCount > 0 )
83              {
84                  LOGGER.trace( "Releasing global {} lock (currently held: {})",
85                                shared ? "read" : "write", lockHoldCount );
86                  lock.unlock();
87                  lockHoldCount--;
88              }
89          }
90  
91      }
92  
93  }