PCSC4Java
0.2
Library PCSC for Java language.
|
00001 /* 00002 * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved. 00003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 00004 * 00005 * This code is free software; you can redistribute it and/or modify it 00006 * under the terms of the GNU General Public License version 2 only, as 00007 * published by the Free Software Foundation. Oracle designates this 00008 * particular file as subject to the "Classpath" exception as provided 00009 * by Oracle in the LICENSE file that accompanied this code. 00010 * 00011 * This code is distributed in the hope that it will be useful, but WITHOUT 00012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 00014 * version 2 for more details (a copy is included in the LICENSE file that 00015 * accompanied this code). 00016 * 00017 * You should have received a copy of the GNU General Public License version 00018 * 2 along with this work; if not, write to the Free Software Foundation, 00019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 00020 * 00021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 00022 * or visit www.oracle.com if you need additional information or have any 00023 * questions. 00024 */ 00025 00026 package fr.redbilled.pcscforjava; 00027 00028 import java.io.*; 00029 00030 import java.security.Permission; 00031 00073 public class CardPermission extends Permission { 00074 00075 private static final long serialVersionUID = 7146787880530705613L; 00076 00077 private final static int A_CONNECT = 0x01; 00078 private final static int A_EXCLUSIVE = 0x02; 00079 private final static int A_GET_BASIC_CHANNEL = 0x04; 00080 private final static int A_OPEN_LOGICAL_CHANNEL = 0x08; 00081 private final static int A_RESET = 0x10; 00082 private final static int A_TRANSMIT_CONTROL = 0x20; 00083 private final static int A_RECONNECT = 0x40; 00084 private final static int A_DISCONNECT = 0x80; 00085 private final static int A_GET_ATTRIB = 0x100; 00086 private final static int A_SET_ATTRIB = 0x200; 00087 00088 // sum of all the actions above 00089 private final static int A_ALL = 0x3ff; 00090 00091 private final static int[] ARRAY_MASKS = { 00092 A_ALL, 00093 A_CONNECT, 00094 A_EXCLUSIVE, 00095 A_GET_BASIC_CHANNEL, 00096 A_OPEN_LOGICAL_CHANNEL, 00097 A_RESET, 00098 A_TRANSMIT_CONTROL, 00099 A_RECONNECT, 00100 A_DISCONNECT, 00101 A_GET_ATTRIB, 00102 A_SET_ATTRIB 00103 }; 00104 00105 private final static String S_CONNECT = "connect"; 00106 private final static String S_EXCLUSIVE = "exclusive"; 00107 private final static String S_GET_BASIC_CHANNEL = "getBasicChannel"; 00108 private final static String S_OPEN_LOGICAL_CHANNEL = "openLogicalChannel"; 00109 private final static String S_RESET = "reset"; 00110 private final static String S_TRANSMIT_CONTROL = "transmitControl"; 00111 private final static String S_RECONNECT = "reconnect"; 00112 private final static String S_DISCONNECT = "disconnect"; 00113 private final static String S_GET_ATTRIB = "getAttrib"; 00114 private final static String S_SET_ATTRIB = "setAttrib"; 00115 00116 private final static String S_ALL = "*"; 00117 00118 private final static String[] ARRAY_STRINGS = { 00119 S_ALL, 00120 S_CONNECT, 00121 S_EXCLUSIVE, 00122 S_GET_BASIC_CHANNEL, 00123 S_OPEN_LOGICAL_CHANNEL, 00124 S_RESET, 00125 S_TRANSMIT_CONTROL, 00126 S_RECONNECT, 00127 S_DISCONNECT, 00128 S_GET_ATTRIB, 00129 S_SET_ATTRIB, 00130 }; 00131 00132 private transient int mask; 00133 00137 private volatile String actions; 00138 00156 public CardPermission(String terminalName, String actions) { 00157 super(terminalName); 00158 if (terminalName == null) { 00159 throw new NullPointerException(); 00160 } 00161 mask = getMask(actions); 00162 } 00163 00164 private static int getMask(String actions) { 00165 if ((actions == null) || (actions.length() == 0)) { 00166 throw new IllegalArgumentException("actions must not be empty"); 00167 } 00168 00169 // try exact matches for simple actions first 00170 for (int i = 0; i < ARRAY_STRINGS.length; i++) { 00171 if (actions.equals(ARRAY_STRINGS[i])) { 00172 return ARRAY_MASKS[i]; 00173 } 00174 } 00175 00176 if (actions.endsWith(",")) { 00177 throw new IllegalArgumentException("Invalid actions: '" + actions + "'"); 00178 } 00179 int mask = 0; 00180 String[] split = actions.split(","); 00181 outer: 00182 for (String s : split) { 00183 for (int i = 0; i < ARRAY_STRINGS.length; i++) { 00184 if (ARRAY_STRINGS[i].equalsIgnoreCase(s)) { 00185 mask |= ARRAY_MASKS[i]; 00186 continue outer; 00187 } 00188 } 00189 throw new IllegalArgumentException("Invalid action: '" + s + "'"); 00190 } 00191 00192 return mask; 00193 } 00194 00195 private static String getActions(int mask) { 00196 if (mask == A_ALL) { 00197 return S_ALL; 00198 } 00199 boolean first = true; 00200 StringBuilder sb = new StringBuilder(); 00201 for (int i = 0; i < ARRAY_MASKS.length; i++) { 00202 int action = ARRAY_MASKS[i]; 00203 if ((mask & action) == action) { 00204 if (first == false) { 00205 sb.append(","); 00206 } else { 00207 first = false; 00208 } 00209 sb.append(ARRAY_STRINGS[i]); 00210 } 00211 } 00212 return sb.toString(); 00213 } 00214 00215 00224 public String getActions() { 00225 if (actions == null) { 00226 actions = getActions(mask); 00227 } 00228 return actions; 00229 } 00230 00247 public boolean implies(Permission permission) { 00248 if (permission instanceof CardPermission == false) { 00249 return false; 00250 } 00251 CardPermission other = (CardPermission)permission; 00252 if ((this.mask & other.mask) != other.mask) { 00253 return false; 00254 } 00255 String thisName = getName(); 00256 if (thisName.equals("*")) { 00257 return true; 00258 } 00259 if (thisName.equals(other.getName())) { 00260 return true; 00261 } 00262 return false; 00263 } 00264 00281 public boolean equals(Object obj) { 00282 if (this == obj) { 00283 return true; 00284 } 00285 if (obj instanceof CardPermission == false) { 00286 return false; 00287 } 00288 CardPermission other = (CardPermission)obj; 00289 return this.getName().equals(other.getName()) && (this.mask == other.mask); 00290 } 00291 00297 public int hashCode() { 00298 return getName().hashCode() + 31 * mask; 00299 } 00300 00301 private void writeObject(ObjectOutputStream s) throws IOException { 00302 // Write out the actions. The superclass takes care of the name. 00303 // Call getActions to make sure actions field is initialized 00304 if (actions == null) { 00305 getActions(); 00306 } 00307 s.defaultWriteObject(); 00308 } 00309 00310 private void readObject(ObjectInputStream s) 00311 throws IOException, ClassNotFoundException { 00312 // Read in the actions, then restore the mask. 00313 s.defaultReadObject(); 00314 mask = getMask(actions); 00315 } 00316 00317 }