KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > engine > RightOwner


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.engine;
6
7 import java.sql.SQLException JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import org.h2.message.Message;
12 import org.h2.table.Table;
13
14 public abstract class RightOwner extends DbObject {
15
16     // key: role; value: right
17
private HashMap JavaDoc grantedRoles;
18     // key: table; value: right
19
private HashMap JavaDoc grantedRights;
20     
21     protected RightOwner(Database database, int id, String JavaDoc name, String JavaDoc traceModule) {
22         super(database, id, name, traceModule);
23     }
24
25     public boolean isRoleGranted(Role grantedRole) {
26         if(grantedRoles != null) {
27             Iterator JavaDoc it = grantedRoles.keySet().iterator();
28             while(it.hasNext()) {
29                 Role role = (Role) it.next();
30                 if(role == grantedRole) {
31                     return true;
32                 }
33                 if(role.isRoleGranted(grantedRole)) {
34                     return true;
35                 }
36             }
37         }
38         return false;
39     }
40     
41     protected boolean isRightGrantedRecursive(Table table, int rightMask) {
42         Right right;
43         if(grantedRights != null) {
44             right = (Right) grantedRights.get(table);
45             if(right != null) {
46                 if((right.getRightMask() & rightMask) == rightMask) {
47                     return true;
48                 }
49             }
50         }
51         if(grantedRoles != null) {
52             Iterator JavaDoc it = grantedRoles.keySet().iterator();
53             while(it.hasNext()) {
54                 RightOwner role = (RightOwner) it.next();
55                 if(role.isRightGrantedRecursive(table, rightMask)) {
56                     return true;
57                 }
58             }
59         }
60         return false;
61     }
62     
63     public void grantRight(Table table, Right right) {
64         if(grantedRights == null) {
65             grantedRights = new HashMap JavaDoc();
66         }
67         grantedRights.put(table, right);
68     }
69     
70     public void revokeRight(Table table) {
71         if(grantedRights == null) {
72             return;
73         }
74         grantedRights.remove(table);
75         if(grantedRights.size() == 0) {
76             grantedRights = null;
77         }
78     }
79     
80     public void grantRole(Session session, Role role, Right right) {
81         if(grantedRoles == null) {
82             grantedRoles = new HashMap JavaDoc();
83         }
84         grantedRoles.put(role, right);
85     }
86     
87     public void revokeRole(Session session, Role role) throws SQLException JavaDoc {
88         if(grantedRoles == null) {
89             throw Message.getSQLException(Message.RIGHT_NOT_FOUND);
90         }
91         Right right = (Right) grantedRoles.get(role);
92         if(right == null) {
93             throw Message.getSQLException(Message.RIGHT_NOT_FOUND);
94         }
95         grantedRoles.remove(role);
96         if(grantedRoles.size() == 0) {
97             grantedRoles = null;
98         }
99     }
100     
101     public Right getRightForTable(Table table) {
102         if(grantedRights == null) {
103             return null;
104         }
105         return (Right) grantedRights.get(table);
106     }
107     
108     public Right getRightForRole(Role role) {
109         if(grantedRoles == null) {
110             return null;
111         }
112         return (Right) grantedRoles.get(role);
113     }
114     
115 }
116
Popular Tags