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.filter.ruletree; 020 021import java.util.ArrayList; 022import java.util.Arrays; 023import java.util.List; 024import java.util.concurrent.ConcurrentHashMap; 025import java.util.concurrent.atomic.AtomicInteger; 026import java.util.stream.Stream; 027 028import static java.util.stream.Collectors.toList; 029 030/** 031 * Group tree for Maven groupIDs. 032 * This class parses a text file that has a directive on each line. Directive examples: 033 * <ul> 034 * <li>ignored/formatting - each line starting with {@code '#'} (hash) or being empty/blank is ignored.</li> 035 * <li>modifier {@code !} is negation (disallow; by def entry allows). If present must be first character.</li> 036 * <li>modifier {@code =} is limiter (to given G; by def is "G and below"). If present, must be first character. If negation present, must be second character.</li> 037 * <li>a valid Maven groupID ie "org.apache.maven".</li> 038 * </ul> 039 * By default, a G entry ie {@code org.apache.maven} means "allow {@code org.apache.maven} G and all Gs below 040 * (so {@code org.apache.maven.plugins} etc. are all allowed). There is one special entry {@code "*"} (asterisk) 041 * that means "root" and defines the default acceptance: {@code "*"} means "by default accept" and {@code "!*"} 042 * means "by default deny" (same effect as when this character is not present in file). Use of limiter modifier 043 * on "root" like {@code "=*"} has no effect, is simply ignored. 044 * 045 * <p> 046 * Examples: 047 * <pre> 048 * {@code 049 * # this is my group filter list 050 * 051 * org.apache.maven 052 * !=org.apache.maven.foo 053 * !org.apache.maven.indexer 054 * =org.apache.bar 055 * } 056 * </pre> 057 * 058 * File meaning: "allow all {@code org.apache.maven} and below", "disallow {@code org.apache.maven.foo} groupId ONLY" 059 * (hence {@code org.apache.maven.foo.bar} is allowed due first line), "disallow {@code org.apache.maven.indexer} and below" 060 * and "allow {@code org.apache.bar} groupID ONLY". 061 * 062 * <p> 063 * In case of conflicting rules, parsing happens by "last wins", so line closer to last line in file "wins", and conflicting 064 * line value is lost. 065 */ 066public class GroupTree { 067 /** 068 * Creates root, that is special: accept is never null. 069 */ 070 public static GroupTree create(String name) { 071 GroupTree result = new GroupTree(name); 072 result.accept = false; 073 return result; 074 } 075 076 private static final String ROOT = "*"; 077 private static final String MOD_EXCLUSION = "!"; 078 private static final String MOD_STOP = "="; 079 080 private static List<String> elementsOfGroup(final String groupId) { 081 return Arrays.stream(groupId.split("\\.")).filter(e -> !e.isEmpty()).collect(toList()); 082 } 083 084 private final String name; 085 private final ConcurrentHashMap<String, GroupTree> children; 086 private boolean stop; 087 private Boolean accept; 088 089 private GroupTree(String name) { 090 this.name = name; 091 this.children = new ConcurrentHashMap<>(); 092 } 093 094 public int loadNodes(Stream<String> linesStream) { 095 AtomicInteger counter = new AtomicInteger(0); 096 linesStream.forEach(line -> { 097 if (loadNode(line)) { 098 counter.incrementAndGet(); 099 } 100 }); 101 return counter.get(); 102 } 103 104 public boolean loadNode(String line) { 105 if (!line.startsWith("#") && !line.trim().isEmpty()) { 106 GroupTree currentNode = this; 107 boolean accept = true; 108 if (line.startsWith(MOD_EXCLUSION)) { 109 accept = false; 110 line = line.substring(MOD_EXCLUSION.length()); 111 } 112 boolean stop = false; 113 if (line.startsWith(MOD_STOP)) { 114 stop = true; 115 line = line.substring(MOD_STOP.length()); 116 } 117 if (ROOT.equals(line)) { 118 this.accept = accept; 119 return true; 120 } 121 List<String> groupElements = elementsOfGroup(line); 122 for (String groupElement : groupElements.subList(0, groupElements.size() - 1)) { 123 currentNode = currentNode.children.computeIfAbsent(groupElement, GroupTree::new); 124 } 125 String lastElement = groupElements.get(groupElements.size() - 1); 126 currentNode = currentNode.children.computeIfAbsent(lastElement, GroupTree::new); 127 currentNode.stop = stop; 128 currentNode.accept = accept; 129 return true; 130 } 131 return false; 132 } 133 134 public boolean acceptedGroupId(String groupId) { 135 final List<String> current = new ArrayList<>(); 136 final List<String> groupElements = elementsOfGroup(groupId); 137 Boolean accepted = null; 138 GroupTree currentNode = this; 139 for (String groupElement : groupElements) { 140 current.add(groupElement); 141 currentNode = currentNode.children.get(groupElement); 142 if (currentNode == null) { 143 // we stepped off the tree; use value we got so far 144 break; 145 } else if (currentNode.stop && groupElements.equals(current)) { 146 // exact match 147 accepted = currentNode.accept; 148 break; 149 } else if (!currentNode.stop && currentNode.accept != null) { 150 // "inherit" if not STOP and allow is set; and most probably we loop more 151 accepted = currentNode.accept; 152 } 153 } 154 // use 'accepted', if defined; otherwise fallback to root (it always has 'allow' set) 155 return accepted != null ? accepted : this.accept; 156 } 157}