jdk/test/java/security/Policy/Dynamic/DynamicPolicy.java
author mullan
Mon, 14 Apr 2008 10:25:05 -0400
changeset 476 b23646c026ed
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6631361: Spec of AccessControlContext constructor is not complete Summary: Add NullPointerException to @throws clause and treat empty array and array of nulls as equivalent Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2000 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.PropertyPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
public class DynamicPolicy extends Policy{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
    static int refresher = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    public DynamicPolicy() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    public PermissionCollection getPermissions(CodeSource cs) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        Permissions perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        initStaticPolicy(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        // Defalut policy in the beginning...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        // toggle from refresh to refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        if (refresher == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
            perms.add(new PropertyPermission("user.name","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        System.err.println("perms=[" + perms + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public boolean implies(ProtectionDomain pd, Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        return getPermissions(pd).implies(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    public PermissionCollection getPermissions(ProtectionDomain pd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Permissions perms = new Permissions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        initStaticPolicy(perms);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        // Defalut policy in the beginning...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        // toggle from refresh to refresh
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        if (refresher == 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            perms.add(new PropertyPermission("user.name","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        return perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public void refresh() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        refresher++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private void initStaticPolicy(PermissionCollection perms) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        perms.add(new java.security.SecurityPermission("getPolicy"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        perms.add(new java.security.SecurityPermission("setPolicy"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        perms.add(new java.lang.RuntimePermission("stopThread"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        perms.add(new java.net.SocketPermission("localhost:1024-", "listen"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        perms.add(new PropertyPermission("java.version","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        perms.add(new PropertyPermission("java.vendor","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        perms.add(new PropertyPermission("java.vendor.url","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        perms.add(new PropertyPermission("java.class.version","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        perms.add(new PropertyPermission("os.name","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        perms.add(new PropertyPermission("os.version","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        perms.add(new PropertyPermission("os.arch","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        perms.add(new PropertyPermission("file.separator","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        perms.add(new PropertyPermission("path.separator","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        perms.add(new PropertyPermission("line.separator","read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        perms.add(new PropertyPermission("java.specification.version", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        perms.add(new PropertyPermission("java.specification.vendor", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        perms.add(new PropertyPermission("java.specification.name", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        perms.add(new PropertyPermission("java.vm.specification.version", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        perms.add(new PropertyPermission("java.vm.specification.vendor", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        perms.add(new PropertyPermission("java.vm.specification.name", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        perms.add(new PropertyPermission("java.vm.version", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        perms.add(new PropertyPermission("java.vm.vendor", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        perms.add(new PropertyPermission("java.vm.name", "read"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
}