jdk/src/java.smartcardio/share/classes/javax/smartcardio/CardPermission.java
author valeriep
Fri, 12 Aug 2016 00:10:07 +0000
changeset 40266 d198987d85e1
parent 26219 1a19360ff122
permissions -rw-r--r--
6469513: (smartcardio) CardPermission(String termName, String actions) violates specification Summary: Changed to allow null actions value Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
40266
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
     2
 * Copyright (c) 2005, 2016, 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 javax.smartcardio;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
    29
import java.util.StringJoiner;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * A permission for Smart Card operations. A CardPermission consists of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * name of the card terminal the permission applies to and a set of actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * that are valid for that terminal.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>A CardPermission with a name of <code>*</code> applies to all
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * card terminals. The actions string is a comma separated list of the actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * listed below, or <code>*</code> to signify "all actions."
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>Individual actions are:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <dt>connect
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <dd>connect to a card using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * {@linkplain CardTerminal#connect CardTerminal.connect()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <dt>reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * <dd>reset the card using {@linkplain Card#disconnect Card.disconnect(true)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <dt>exclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * <dd>establish exclusive access to a card using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * {@linkplain Card#beginExclusive} and {@linkplain Card#endExclusive
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * endExclusive()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * <dt>transmitControl
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * <dd>transmit a control command using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@linkplain Card#transmitControlCommand Card.transmitControlCommand()}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <dt>getBasicChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * <dd>obtain the basic logical channel using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * {@linkplain Card#getBasicChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <dt>openLogicalChannel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <dd>open a new logical channel using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * {@linkplain Card#openLogicalChannel}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @since   1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * @author  Andreas Sterbenz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * @author  JSR 268 Expert Group
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
public class CardPermission extends Permission {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static final long serialVersionUID = 7146787880530705613L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private final static int A_CONNECT              = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private final static int A_EXCLUSIVE            = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private final static int A_GET_BASIC_CHANNEL    = 0x04;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private final static int A_OPEN_LOGICAL_CHANNEL = 0x08;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private final static int A_RESET                = 0x10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private final static int A_TRANSMIT_CONTROL     = 0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    // sum of all the actions above
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private final static int A_ALL                  = 0x3f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    private final static int[] ARRAY_MASKS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        A_ALL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        A_CONNECT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        A_EXCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        A_GET_BASIC_CHANNEL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        A_OPEN_LOGICAL_CHANNEL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        A_RESET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        A_TRANSMIT_CONTROL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private final static String S_CONNECT              = "connect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private final static String S_EXCLUSIVE            = "exclusive";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private final static String S_GET_BASIC_CHANNEL    = "getBasicChannel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private final static String S_OPEN_LOGICAL_CHANNEL = "openLogicalChannel";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    private final static String S_RESET                = "reset";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    private final static String S_TRANSMIT_CONTROL     = "transmitControl";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private final static String S_ALL                  = "*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private final static String[] ARRAY_STRINGS = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        S_ALL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        S_CONNECT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        S_EXCLUSIVE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        S_GET_BASIC_CHANNEL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        S_OPEN_LOGICAL_CHANNEL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        S_RESET,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        S_TRANSMIT_CONTROL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    private transient int mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
40266
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   121
    private final String actions;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Constructs a new CardPermission with the specified actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>terminalName</code> is the name of a CardTerminal or <code>*</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * if this permission applies to all terminals. <code>actions</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * contains a comma-separated list of the individual actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * or <code>*</code> to signify all actions. For more information,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * see the documentation at the top of this {@linkplain CardPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * class}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * @param terminalName the name of the card terminal, or <code>*</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * @param actions the action string (or null if the set of permitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     *   actions is empty)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @throws NullPointerException if terminalName is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @throws IllegalArgumentException if actions is an invalid actions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *   specification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    public CardPermission(String terminalName, String actions) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        super(terminalName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        if (terminalName == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        mask = getMask(actions);
40266
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   146
        this.actions = getActions(mask);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private static int getMask(String actions) {
40266
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   150
        if (actions == null) {
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   151
            return 0;
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   152
        }
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   153
        if (actions.length() == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new IllegalArgumentException("actions must not be empty");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // try exact matches for simple actions first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (int i = 0; i < ARRAY_STRINGS.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            if (actions == ARRAY_STRINGS[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                return ARRAY_MASKS[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (actions.endsWith(",")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IllegalArgumentException("Invalid actions: '" + actions + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        int mask = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        String[] split = actions.split(",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    outer:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        for (String s : split) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            for (int i = 0; i < ARRAY_STRINGS.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (ARRAY_STRINGS[i].equalsIgnoreCase(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    mask |= ARRAY_MASKS[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    continue outer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            throw new IllegalArgumentException("Invalid action: '" + s + "'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    private static String getActions(int mask) {
40266
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   184
        if (mask == 0) {
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   185
            return null;
d198987d85e1 6469513: (smartcardio) CardPermission(String termName, String actions) violates specification
valeriep
parents: 26219
diff changeset
   186
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        if (mask == A_ALL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            return S_ALL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   190
        StringJoiner sj = new StringJoiner(",");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        for (int i = 0; i < ARRAY_MASKS.length; i++) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   192
            final int action = ARRAY_MASKS[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            if ((mask & action) == action) {
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   194
                sj.add(ARRAY_STRINGS[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
26219
1a19360ff122 8054714: Use StringJoiner where it makes the code cleaner
igerasim
parents: 25859
diff changeset
   197
        return sj.toString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Returns the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * It is <code>*</code> to signify all actions defined by this class or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * the string concatenation of the comma-separated,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * lexicographically sorted list of individual actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @return the canonical string representation of the actions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    public String getActions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return actions;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * Checks if this CardPermission object implies the specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * That is the case, if and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <li><p><code>permission</code> is an instance of CardPermission,</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <li><p><code>permission</code>'s actions are a proper subset of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *   object's actions, and</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * <li><p>this object's <code>getName()</code> method is either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *   <code>*</code> or equal to <code>permission</code>'s <code>name</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *   </p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @param permission the permission to check against
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @return true if and only if this CardPermission object implies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *   specified permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public boolean implies(Permission permission) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        if (permission instanceof CardPermission == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        CardPermission other = (CardPermission)permission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        if ((this.mask & other.mask) != other.mask) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        String thisName = getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        if (thisName.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        if (thisName.equals(other.getName())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * Compares the specified object with this CardPermission for equality.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * This CardPermission is equal to another Object <code>object</code>, if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * and only if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <li><p><code>object</code> is an instance of CardPermission,</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <li><p><code>this.getName()</code> is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * <code>((CardPermission)object).getName()</code>, and</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * <li><p><code>this.getActions()</code> is equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <code>((CardPermission)object).getActions()</code>.</p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @param obj the object to be compared for equality with this CardPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @return true if and only if the specified object is equal to this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *   CardPermission
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        if (this == obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (obj instanceof CardPermission == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        CardPermission other = (CardPermission)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        return this.getName().equals(other.getName()) && (this.mask == other.mask);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Returns the hash code value for this CardPermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * @return the hash code value for this CardPermission object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return getName().hashCode() + 31 * mask;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private void writeObject(ObjectOutputStream s) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        // Write out the actions. The superclass takes care of the name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        s.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private void readObject(ObjectInputStream s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            throws IOException, ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        // Read in the actions, then restore the mask.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        s.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        mask = getMask(actions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
}