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.internal.impl.synccontext.named;
20  
21  import java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  
25  import org.eclipse.aether.RepositorySystemSession;
26  import org.eclipse.aether.artifact.Artifact;
27  import org.eclipse.aether.metadata.Metadata;
28  import org.eclipse.aether.named.NamedLockKey;
29  import org.eclipse.aether.spi.locking.LockingInhibitor;
30  
31  import static java.util.Objects.requireNonNull;
32  
33  /**
34   * Wrapping {@link NameMapper} class that applies discovered {@link org.eclipse.aether.spi.locking.LockingInhibitor}.
35   *
36   * @since 2.0.14
37   */
38  public class InhibitingNameMapper implements NameMapper {
39      private final NameMapper delegate;
40      private final List<LockingInhibitor> lockingInhibitors;
41  
42      public InhibitingNameMapper(NameMapper delegate, List<LockingInhibitor> lockingInhibitors) {
43          this.delegate = requireNonNull(delegate);
44          this.lockingInhibitors = requireNonNull(lockingInhibitors);
45      }
46  
47      @Override
48      public boolean isFileSystemFriendly() {
49          return delegate.isFileSystemFriendly();
50      }
51  
52      @Override
53      public Collection<NamedLockKey> nameLocks(
54              RepositorySystemSession session,
55              Collection<? extends Artifact> artifacts,
56              Collection<? extends Metadata> metadatas) {
57          if (lockingInhibitors.isEmpty()) {
58              return delegate.nameLocks(session, artifacts, metadatas);
59          }
60          if (artifacts != null && !artifacts.isEmpty()) {
61              List<Artifact> filtered = new ArrayList<>(artifacts.size());
62              for (Artifact a : artifacts) {
63                  if (!isArtifactInhibited(a)) {
64                      filtered.add(a);
65                  }
66              }
67              artifacts = filtered;
68          }
69          if (metadatas != null && !metadatas.isEmpty()) {
70              List<Metadata> filtered = new ArrayList<>(metadatas.size());
71              for (Metadata m : metadatas) {
72                  if (!isMetadataInhibited(m)) {
73                      filtered.add(m);
74                  }
75              }
76              metadatas = filtered;
77          }
78          return delegate.nameLocks(session, artifacts, metadatas);
79      }
80  
81      private boolean isArtifactInhibited(Artifact artifact) {
82          for (LockingInhibitor inhibitor : lockingInhibitors) {
83              if (inhibitor.preventArtifactLocking(artifact)) {
84                  return true;
85              }
86          }
87          return false;
88      }
89  
90      private boolean isMetadataInhibited(Metadata metadata) {
91          for (LockingInhibitor inhibitor : lockingInhibitors) {
92              if (inhibitor.preventMetadataLocking(metadata)) {
93                  return true;
94              }
95          }
96          return false;
97      }
98  }