Path: blob/master/test/jdk/java/security/Policy/PolicyProvider/CustomPolicy.java
41153 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.security.AccessController;24import java.security.Permission;25import java.security.Policy;26import java.security.PrivilegedAction;27import java.security.ProtectionDomain;2829public class CustomPolicy extends Policy {3031// the ProtectionDomain of CustomPolicy32private final ProtectionDomain policyPd;3334public CustomPolicy() {35policyPd = AccessController.doPrivileged(36(PrivilegedAction<ProtectionDomain>)37() -> this.getClass().getProtectionDomain());38}3940@Override41public boolean implies(ProtectionDomain pd, Permission perm) {42System.out.println("CustomPolicy.implies");4344// If the protection domain is the same as CustomPolicy, then45// we return true. This is to prevent recursive permission checks46// that lead to StackOverflow errors when the policy implementation47// performs a sensitive operation that triggers a permission check,48// for example, as below.49if (pd == policyPd) {50return true;51}5253// Do something that triggers a permission check to make sure that54// we don't cause a StackOverflow error.55String home = AccessController.doPrivileged(56(PrivilegedAction<String>) () -> System.getProperty("user.home"));5758return true;59}60}616263