Path: blob/master/test/jdk/java/security/Policy/Dynamic/DynamicPolicy.java
41154 views
/*1* Copyright (c) 2000, 2019, 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.*;24import java.util.PropertyPermission;25import java.net.SocketPermission;26import java.lang.*;2728public class DynamicPolicy extends Policy{2930static final Policy DEFAULT_POLICY = Policy.getPolicy(); // do this early before setPolicy is called31static int refresher = 0;323334public DynamicPolicy() {35}3637public PermissionCollection getPermissions(CodeSource cs) {3839Permissions perms = new Permissions();40initStaticPolicy(perms);41// Defalut policy in the beginning...42// toggle from refresh to refresh43if (refresher == 1)44perms.add(new PropertyPermission("user.name","read"));4546System.err.println("perms=[" + perms + "]");47return perms;48}4950public boolean implies(ProtectionDomain pd, Permission p) {51return getPermissions(pd).implies(p) || DEFAULT_POLICY.implies(pd, p);52}5354public PermissionCollection getPermissions(ProtectionDomain pd) {5556Permissions perms = new Permissions();57initStaticPolicy(perms);58// Defalut policy in the beginning...59// toggle from refresh to refresh60if (refresher == 1)61perms.add(new PropertyPermission("user.name","read"));6263return perms;64}6566public void refresh() {67refresher++;68}6970private void initStaticPolicy(PermissionCollection perms) {7172perms.add(new java.security.SecurityPermission("getPolicy"));73perms.add(new java.security.SecurityPermission("setPolicy"));74perms.add(new java.lang.RuntimePermission("stopThread"));75perms.add(new java.net.SocketPermission("localhost:1024-", "listen"));76perms.add(new PropertyPermission("java.version","read"));77perms.add(new PropertyPermission("java.vendor","read"));78perms.add(new PropertyPermission("java.vendor.url","read"));79perms.add(new PropertyPermission("java.class.version","read"));80perms.add(new PropertyPermission("os.name","read"));81perms.add(new PropertyPermission("os.version","read"));82perms.add(new PropertyPermission("os.arch","read"));83perms.add(new PropertyPermission("file.separator","read"));84perms.add(new PropertyPermission("path.separator","read"));85perms.add(new PropertyPermission("line.separator","read"));86perms.add(new PropertyPermission("java.specification.version", "read"));87perms.add(new PropertyPermission("java.specification.vendor", "read"));88perms.add(new PropertyPermission("java.specification.name", "read"));89perms.add(new PropertyPermission("java.vm.specification.version", "read"));90perms.add(new PropertyPermission("java.vm.specification.vendor", "read"));91perms.add(new PropertyPermission("java.vm.specification.name", "read"));92perms.add(new PropertyPermission("java.vm.version", "read"));93perms.add(new PropertyPermission("java.vm.vendor", "read"));94perms.add(new PropertyPermission("java.vm.name", "read"));95return;96}97}9899100