/* * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package javax.smartcardio; import java.io.*; import java.security.Permission; /** * A permission for Smart Card operations. A CardPermission consists of the * name of the card terminal the permission applies to and a set of actions * that are valid for that terminal. * *
A CardPermission with a name of *
applies to all
* card terminals. The actions string is a comma separated list of the actions
* listed below, or *
to signify "all actions."
*
*
Individual actions are: *
terminalName
is the name of a CardTerminal or *
* if this permission applies to all terminals. actions
* contains a comma-separated list of the individual actions
* or *
to signify all actions. For more information,
* see the documentation at the top of this {@linkplain CardPermission
* class}.
*
* @param terminalName the name of the card terminal, or *
* @param actions the action string (or null if the set of permitted
* actions is empty)
*
* @throws NullPointerException if terminalName is null
* @throws IllegalArgumentException if actions is an invalid actions
* specification
*/
public CardPermission(String terminalName, String actions) {
super(terminalName);
if (terminalName == null) {
throw new NullPointerException();
}
mask = getMask(actions);
}
private static int getMask(String actions) {
if ((actions == null) || (actions.length() == 0)) {
throw new IllegalArgumentException("actions must not be empty");
}
// try exact matches for simple actions first
for (int i = 0; i < ARRAY_STRINGS.length; i++) {
if (actions == ARRAY_STRINGS[i]) {
return ARRAY_MASKS[i];
}
}
if (actions.endsWith(",")) {
throw new IllegalArgumentException("Invalid actions: '" + actions + "'");
}
int mask = 0;
String[] split = actions.split(",");
outer:
for (String s : split) {
for (int i = 0; i < ARRAY_STRINGS.length; i++) {
if (ARRAY_STRINGS[i].equalsIgnoreCase(s)) {
mask |= ARRAY_MASKS[i];
continue outer;
}
}
throw new IllegalArgumentException("Invalid action: '" + s + "'");
}
return mask;
}
private static String getActions(int mask) {
if (mask == A_ALL) {
return S_ALL;
}
boolean first = true;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ARRAY_MASKS.length; i++) {
int action = ARRAY_MASKS[i];
if ((mask & action) == action) {
if (first == false) {
sb.append(",");
} else {
first = false;
}
sb.append(ARRAY_STRINGS[i]);
}
}
return sb.toString();
}
/**
* Returns the canonical string representation of the actions.
* It is *
to signify all actions defined by this class or
* the string concatenation of the comma-separated,
* lexicographically sorted list of individual actions.
*
* @return the canonical string representation of the actions.
*/
public String getActions() {
if (actions == null) {
actions = getActions(mask);
}
return actions;
}
/**
* Checks if this CardPermission object implies the specified permission.
* That is the case, if and only if
* permission
is an instance of CardPermission,
permission
's actions are a proper subset of this
* object's actions, and
this object's getName()
method is either
* *
or equal to permission
's name
.
*
object
, if
* and only if
* object
is an instance of CardPermission,
this.getName()
is equal to
* ((CardPermission)object).getName()
, and
this.getActions()
is equal to
* ((CardPermission)object).getActions()
.