1
10package org.jstk.example.bank.server;
11
12import java.security.Permission;
13import java.util.StringTokenizer;
14
15public class BankPermission extends Permission {
16 protected int mask = 0;
17 private static int OPEN = 0x01;
18 private static int CLOSE = 0x02;
19 private static int GET = 0x04;
20 private static int LIST = 0x08;
21
22 private String actions = null;
23
24 public BankPermission(String name){
25 super(name);
26 }
27
28 public BankPermission(String name, String action){
29 super(name);
30 parse(action);
31 }
32
33 private void parse(String action){
34 StringTokenizer st = new StringTokenizer(action, ",\t ");
35 while (st.hasMoreTokens()){
36 String tok = st.nextToken();
37 if (tok.equals("open"))
38 mask |= OPEN;
39 else if (tok.equals("close"))
40 mask |= CLOSE;
41 else if (tok.equals("get"))
42 mask |= GET;
43 else if (tok.equals("list"))
44 mask |= LIST;
45 else
46 throw new IllegalArgumentException("Unknown action: " + tok);
47 }
48 }
49 public boolean implies(Permission p) {
50 if ((p == null) || (p.getClass() != getClass()))
51 return false;
52 BankPermission that = (BankPermission) p;
53 if (getName().equals(that.getName()) || getName().equals("*")){
54 if ((mask & that.mask) == that.mask)
55 return true;
56 }
57 return false;
58 }
59
60 public boolean equals(Object o){
61 if (o == this)
62 return true;
63
64 if ((o == null) || (o.getClass() != getClass()))
65 return false;
66
67 BankPermission that = (BankPermission) o;
68 if (getName().equals(that.getName()) && (mask == that.mask))
69 return true;
70 return false;
71 }
72
73 public int hashCode(){
74 return (getName().hashCode() ^ mask);
75 }
76
77 public String getActions(){
78 StringBuffer sb = new StringBuffer();
79 if ((mask & OPEN) == OPEN)
80 sb.append(" open");
81 if ((mask & CLOSE) == CLOSE)
82 sb.append(" close");
83 if ((mask & GET) == GET)
84 sb.append(" get");
85 if ((mask & LIST) == LIST)
86 sb.append(" list");
87
88 return sb.toString();
89 }
90}