001/* 002 * PermissionsEx 003 * Copyright (C) zml and PermissionsEx contributors 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package ca.stellardrift.permissionsex.impl.logging; 018 019import ca.stellardrift.permissionsex.context.ContextValue; 020import ca.stellardrift.permissionsex.logging.PermissionCheckNotifier; 021import ca.stellardrift.permissionsex.subject.SubjectRef; 022 023import java.util.Collections; 024import java.util.LinkedHashMap; 025import java.util.List; 026import java.util.Map; 027import java.util.Set; 028 029/** 030 * Notification delegate for permissions checks that tracks recent permission checks. 031 * 032 * Allows accessing currently known permissions 033 */ 034public class RecordingPermissionCheckNotifier implements PermissionCheckNotifier { 035 private static final int MAX_SIZE = 500; 036 037 private final Set<String> knownPermissions = sizeLimitedSet(MAX_SIZE); 038 private final Set<String> knownOptions = sizeLimitedSet(MAX_SIZE); 039 040 private static <T> Set<T> sizeLimitedSet(final int maxSize) { 041 return Collections.newSetFromMap(new LinkedHashMap<T, Boolean>() { 042 private static final long serialVersionUID = 9025221898274056636L; 043 044 @Override 045 protected boolean removeEldestEntry(Map.Entry<T, Boolean> eldest) { 046 return size() > maxSize; 047 } 048 }); 049 } 050 @Override 051 public void onPermissionCheck(SubjectRef<?> subject, Set<ContextValue<?>> contexts, String permission, int value) { 052 knownPermissions.add(permission); 053 054 } 055 056 @Override 057 public void onOptionCheck(SubjectRef<?> subject, Set<ContextValue<?>> contexts, String option, String value) { 058 knownOptions.add(option); 059 } 060 061 @Override 062 public void onParentCheck(SubjectRef<?> subject, Set<ContextValue<?>> contexts, List<SubjectRef<?>> parents) { 063 } 064 065 public Set<String> getKnownPermissions() { 066 return Collections.unmodifiableSet(knownPermissions); 067 } 068 069 public Set<String> getKnownOptions() { 070 return Collections.unmodifiableSet(knownOptions); 071 } 072}