1 /*
2  * @(#) $Id: AccountPermission.java,v 1.2 2003/07/08 08:13:53 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10package org.jstk.example.bank.server;
11
12import java.security.Permission;
13import java.util.StringTokenizer;
14
15public class AccountPermission extends Permission {
16    protected int mask = 0;
17    private static int OPEN = 0x01;
18    private static int CLOSE = 0x02;
19    private static int DEPOSIT = 0x04;
20    private static int WITHDRAW = 0x08;
21    private static int READ = 0x10; // for getAcctNo, getBalance and getStatement.
22
23    private String actions = "";
24
25    public AccountPermission(String name){
26        super(name);
27    }
28
29    public AccountPermission(String name, String action){
30        super(name);
31        parse(action);
32    }
33
34    private void parse(String action){
35        StringTokenizer st = new StringTokenizer(action, ",\t ");
36        while (st.hasMoreTokens()){
37            String tok = st.nextToken();
38            if (tok.equals("open"))
39                mask |= OPEN;
40            else if (tok.equals("close"))
41                mask |= CLOSE;
42            else if (tok.equals("deposit"))
43                mask |= DEPOSIT;
44            else if (tok.equals("withdraw"))
45                mask |= WITHDRAW;
46            else if (tok.equals("read"))
47                mask |= READ;
48            else
49                throw new IllegalArgumentException("Unknown action: " + tok);
50        }
51    }
52    public boolean implies(Permission p) {
53        if ((p == null) || (p.getClass() != getClass()))
54            return false;
55        AccountPermission that = (AccountPermission) p;
56        if (getName().equals(that.getName()) || getName().equals("*")){
57            if ((mask & that.mask) == that.mask)
58                return true;
59        }
60        return false;
61    }
62
63    public boolean equals(Object o){
64        if (o == this)
65            return true;
66
67        if ((o == null) || (o.getClass() != getClass()))
68            return false;
69
70        AccountPermission that = (AccountPermission) o;
71        if (getName().equals(that.getName()) && (mask == that.mask))
72            return true;
73        return false;
74    }
75
76    public int hashCode(){
77        return (getName().hashCode() ^ mask);
78    }
79
80    public String getActions(){
81        StringBuffer sb = new StringBuffer();
82        if ((mask & OPEN) == OPEN)
83            sb.append(" open");
84        if ((mask & CLOSE) == CLOSE)
85            sb.append(" close");
86        if ((mask & DEPOSIT) == DEPOSIT)
87            sb.append(" deposit");
88        if ((mask & WITHDRAW) == WITHDRAW)
89            sb.append(" withdraw");
90        if ((mask & READ) == READ)
91            sb.append(" read");
92
93        return sb.toString();
94    }
95}