jdk/src/share/classes/java/security/UnresolvedPermissionCollection.java
author jjg
Mon, 15 Aug 2011 11:48:20 -0700
changeset 10336 0bb1999251f8
parent 9035 1255eb81cc2f
permissions -rw-r--r--
7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror Reviewed-by: xuelei, mullan Contributed-by: alexandre.boulgakov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 7970
diff changeset
     2
 * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.ObjectStreamField;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.ObjectOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.ObjectInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * A UnresolvedPermissionCollection stores a collection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * of UnresolvedPermission permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see java.security.Permission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see java.security.Permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see java.security.UnresolvedPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Roland Schemers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @serial include
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
final class UnresolvedPermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
extends PermissionCollection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
implements java.io.Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * Key is permission type, value is a list of the UnresolvedPermissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * of the same type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Not serialized; see serialization section at end of class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private transient Map<String, List<UnresolvedPermission>> perms;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Create an empty UnresolvedPermissionCollection object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public UnresolvedPermissionCollection() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        perms = new HashMap<String, List<UnresolvedPermission>>(11);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Adds a permission to this UnresolvedPermissionCollection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * The key for the hash is the unresolved permission's type (class) name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param permission the Permission object to add.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public void add(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        if (! (permission instanceof UnresolvedPermission))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            throw new IllegalArgumentException("invalid permission: "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                                               permission);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        UnresolvedPermission up = (UnresolvedPermission) permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        List<UnresolvedPermission> v;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            v = perms.get(up.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (v == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                v = new ArrayList<UnresolvedPermission>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                perms.put(up.getName(), v);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        synchronized (v) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            v.add(up);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * get any unresolved permissions of the same type as p,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * and return the List containing them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    List<UnresolvedPermission> getUnresolvedPermissions(Permission p) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            return perms.get(p.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * always returns false for unresolved permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public boolean implies(Permission permission)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Returns an enumeration of all the UnresolvedPermission lists in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return an enumeration of all the UnresolvedPermission objects.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    public Enumeration<Permission> elements() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        List<Permission> results =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   122
            new ArrayList<>(); // where results are stored
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // Get iterator of Map values (which are lists of permissions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            for (List<UnresolvedPermission> l : perms.values()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                synchronized (l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    results.addAll(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return Collections.enumeration(results);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private static final long serialVersionUID = -7176153071733132400L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    // Need to maintain serialization interoperability with earlier releases,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    // which had the serializable field:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    // private Hashtable permissions; // keyed on type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @serialField permissions java.util.Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *     A table of the UnresolvedPermissions keyed on type, value is Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *     of permissions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private static final ObjectStreamField[] serialPersistentFields = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        new ObjectStreamField("permissions", Hashtable.class),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @serialData Default field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * Writes the contents of the perms field out as a Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * in which the values are Vectors for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * serialization compatibility with earlier releases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        // Don't call out.defaultWriteObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        // Copy perms into a Hashtable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        Hashtable<String, Vector<UnresolvedPermission>> permissions =
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   164
            new Hashtable<>(perms.size()*2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        // Convert each entry (List) into a Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            Set<Map.Entry<String, List<UnresolvedPermission>>> set = perms.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            for (Map.Entry<String, List<UnresolvedPermission>> e : set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                // Convert list into Vector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                List<UnresolvedPermission> list = e.getValue();
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   172
                Vector<UnresolvedPermission> vec = new Vector<>(list.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                synchronized (list) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    vec.addAll(list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                // Add to Hashtable being serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                permissions.put(e.getKey(), vec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        // Write out serializable fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        ObjectOutputStream.PutField pfields = out.putFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        pfields.put("permissions", permissions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        out.writeFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * Reads in a Hashtable in which the values are Vectors of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * UnresolvedPermissions and saves them in the perms field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private void readObject(ObjectInputStream in) throws IOException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        // Don't call defaultReadObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        // Read in serialized fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        ObjectInputStream.GetField gfields = in.readFields();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        // Get permissions
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   200
        @SuppressWarnings("unchecked")
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   201
        // writeObject writes a Hashtable<String, Vector<UnresolvedPermission>>
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   202
        // for the permissions key, so this cast is safe, unless the data is corrupt.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        Hashtable<String, Vector<UnresolvedPermission>> permissions =
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   204
                (Hashtable<String, Vector<UnresolvedPermission>>)
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   205
                gfields.get("permissions", null);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        perms = new HashMap<String, List<UnresolvedPermission>>(permissions.size()*2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        // Convert each entry (Vector) into a List
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        Set<Map.Entry<String, Vector<UnresolvedPermission>>> set = permissions.entrySet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        for (Map.Entry<String, Vector<UnresolvedPermission>> e : set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // Convert Vector into ArrayList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            Vector<UnresolvedPermission> vec = e.getValue();
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 5506
diff changeset
   213
            List<UnresolvedPermission> list = new ArrayList<>(vec.size());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            list.addAll(vec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            // Add to Hashtable being serialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            perms.put(e.getKey(), list);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
}