001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.impl.synccontext.named; 020 021import java.util.Collection; 022import java.util.stream.Collectors; 023 024import org.eclipse.aether.RepositorySystemSession; 025import org.eclipse.aether.artifact.Artifact; 026import org.eclipse.aether.metadata.Metadata; 027import org.eclipse.aether.util.ConfigUtils; 028import org.eclipse.aether.util.StringDigestUtil; 029 030import static java.util.Objects.requireNonNull; 031 032/** 033 * Wrapping {@link NameMapper}, that wraps another {@link NameMapper} and hashes resulting strings. It makes use of 034 * fact that (proper) Hash will create unique fixed length string for each different input string (so injection still 035 * stands). This mapper produces file system friendly names. Supports different "depths" (0-4 inclusive) where the 036 * name will contain 0 to 4 level deep directories. 037 * <p> 038 * This mapper is usable in any scenario, but intent was to produce more "compact" name mapper for file locking. 039 * 040 * @since 1.9.0 041 */ 042public class HashingNameMapper implements NameMapper { 043 /** 044 * The depth how many levels should adapter create. Acceptable values are 0-4 (inclusive). 045 * 046 * @configurationSource {@link RepositorySystemSession#getConfigProperties()} 047 * @configurationType {@link java.lang.Integer} 048 * @configurationDefaultValue {@link #DEFAULT_DEPTH} 049 */ 050 public static final String CONFIG_PROP_DEPTH = NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "hashing.depth"; 051 052 public static final int DEFAULT_DEPTH = 2; 053 054 private final NameMapper delegate; 055 056 public HashingNameMapper(final NameMapper delegate) { 057 this.delegate = requireNonNull(delegate); 058 } 059 060 @Override 061 public boolean isFileSystemFriendly() { 062 return true; // hashes delegated strings, so whatever it wrapped, it does not come through 063 } 064 065 @Override 066 public Collection<String> nameLocks( 067 RepositorySystemSession session, 068 Collection<? extends Artifact> artifacts, 069 Collection<? extends Metadata> metadatas) { 070 final int depth = ConfigUtils.getInteger(session, DEFAULT_DEPTH, CONFIG_PROP_DEPTH); 071 if (depth < 0 || depth > 4) { 072 throw new IllegalArgumentException("allowed depth value is between 0 and 4 (inclusive)"); 073 } 074 return delegate.nameLocks(session, artifacts, metadatas).stream() 075 .map(n -> hashName(n, depth)) 076 .collect(Collectors.toList()); 077 } 078 079 private String hashName(final String name, final int depth) { 080 String hashedName = StringDigestUtil.sha1(name); 081 if (depth == 0) { 082 return hashedName; 083 } 084 StringBuilder prefix = new StringBuilder(); 085 int i = 0; 086 while (i < hashedName.length() && i / 2 < depth) { 087 prefix.append(hashedName, i, i + 2).append("/"); 088 i += 2; 089 } 090 return prefix.append(hashedName).toString(); 091 } 092}