View Javadoc
1   package org.eclipse.aether.internal.impl.synccontext.named;
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.eclipse.aether.RepositorySystemSession;
23  import org.eclipse.aether.artifact.Artifact;
24  import org.eclipse.aether.metadata.Metadata;
25  import org.eclipse.aether.util.ConfigUtils;
26  import org.eclipse.aether.util.StringDigestUtil;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  import java.io.File;
31  import java.net.InetAddress;
32  import java.net.UnknownHostException;
33  import java.util.Collection;
34  import java.util.Objects;
35  
36  import static java.util.stream.Collectors.toList;
37  
38  /**
39   * Wrapping {@link NameMapper}, that wraps another {@link NameMapper} and adds a "discriminator" as prefix, that
40   * makes lock names unique including the hostname and local repository (by default). The discriminator may be passed
41   * in via {@link RepositorySystemSession} or is automatically calculated based on the local hostname and repository
42   * path. The implementation retains order of collection elements as it got it from
43   * {@link NameMapper#nameLocks(RepositorySystemSession, Collection, Collection)} method.
44   * <p>
45   * The default setup wraps {@link GAVNameMapper}, but manually may be created any instance needed.
46   */
47  public class DiscriminatingNameMapper implements NameMapper
48  {
49      /**
50       * Configuration property to pass in discriminator
51       */
52      private static final String CONFIG_PROP_DISCRIMINATOR = "aether.syncContext.named.discriminating.discriminator";
53  
54      /**
55       * Configuration property to pass in hostname
56       */
57      private static final String CONFIG_PROP_HOSTNAME = "aether.syncContext.named.discriminating.hostname";
58  
59      private static final String DEFAULT_DISCRIMINATOR_DIGEST = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
60  
61      private static final String DEFAULT_HOSTNAME = "localhost";
62  
63      private static final Logger LOGGER = LoggerFactory.getLogger( DiscriminatingNameMapper.class );
64  
65      private final NameMapper delegate;
66  
67      private final String hostname;
68  
69      public DiscriminatingNameMapper( final NameMapper delegate )
70      {
71          this.delegate = Objects.requireNonNull( delegate );
72          this.hostname = getHostname();
73      }
74  
75      @Override
76      public boolean isFileSystemFriendly()
77      {
78          return false; // uses ":" in produced lock names
79      }
80  
81      @Override
82      public Collection<String> nameLocks( final RepositorySystemSession session,
83                                           final Collection<? extends Artifact> artifacts,
84                                           final Collection<? extends Metadata> metadatas )
85      {
86          String discriminator = createDiscriminator( session );
87          return delegate.nameLocks( session, artifacts, metadatas ).stream()
88                  .map( s -> discriminator + ":" + s )
89                  .collect( toList() );
90      }
91  
92      private String getHostname()
93      {
94          try
95          {
96              return InetAddress.getLocalHost().getHostName();
97          }
98          catch ( UnknownHostException e )
99          {
100             LOGGER.warn( "Failed to get hostname, using '{}'", DEFAULT_HOSTNAME, e );
101             return DEFAULT_HOSTNAME;
102         }
103     }
104 
105     private String createDiscriminator( final RepositorySystemSession session )
106     {
107         String discriminator = ConfigUtils.getString( session, null, CONFIG_PROP_DISCRIMINATOR );
108 
109         if ( discriminator == null || discriminator.isEmpty() )
110         {
111             String hostname = ConfigUtils.getString( session, this.hostname, CONFIG_PROP_HOSTNAME );
112             File basedir = session.getLocalRepository().getBasedir();
113             discriminator = hostname + ":" + basedir;
114             try
115             {
116                 return StringDigestUtil.sha1( discriminator );
117             }
118             catch ( Exception e )
119             {
120                 LOGGER.warn( "Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e );
121                 return DEFAULT_DISCRIMINATOR_DIGEST;
122             }
123         }
124         return discriminator;
125     }
126 }