KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > Privileges


1 /**
2  * com.mckoi.database.Privileges 23 Aug 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import com.mckoi.util.StringListBucket;
28 import java.util.StringTokenizer JavaDoc;
29 import java.util.Collections JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.sql.*;
32
33 /**
34  * A set of privileges to grant a user for an object.
35  *
36  * @author Tobias Downer
37  */

38
39 public class Privileges {
40
41   /**
42    * The number of bits available to set.
43    */

44   final static int BIT_COUNT = 11;
45   
46   /**
47    * The bit mask. There are currently 11 used bits, so this has all 11 bits
48    * set.
49    */

50   final static int BIT_MASK = (1 << BIT_COUNT) - 1;
51   
52   // ---------- Statics ----------
53

54   /**
55    * The priv to allow full access to the database object. If this is used,
56    * it should be the only privilege added.
57    */

58   public final static int ALL = BIT_MASK;
59
60   /**
61    * The priv to SELECT from a database object.
62    */

63   public final static int SELECT = 0x01;
64
65   /**
66    * The priv to DELETE from a database object.
67    */

68   public final static int DELETE = 0x02;
69
70   /**
71    * The priv to UPDATE a database object.
72    */

73   public final static int UPDATE = 0x04;
74
75   /**
76    * The priv to INSERT to a database object.
77    */

78   public final static int INSERT = 0x08;
79
80   /**
81    * The priv to REFERENCE a database object.
82    */

83   public final static int REFERENCES = 0x010;
84
85   /**
86    * The priv to see statistics on a database object.
87    */

88   public final static int USAGE = 0x020;
89
90   /**
91    * The priv to compact a database object.
92    */

93   public final static int COMPACT = 0x040;
94
95   /**
96    * The priv to create objects (only applicable for SCHEMA grant objects).
97    */

98   public final static int CREATE = 0x080;
99   
100   /**
101    * The priv to alter objects (only applicable for SCHEMA grant objects).
102    */

103   public final static int ALTER = 0x0100;
104   
105   /**
106    * The priv to drop objects (only applicable for SCHEMA grant objects).
107    */

108   public final static int DROP = 0x0200;
109
110   /**
111    * The priv to view objects in a schema (only applicable for SCHEMA grant
112    * objects).
113    */

114   public final static int LIST = 0x0400;
115   
116   
117  
118   // ---------- Members ----------
119

120   /**
121    * The priv bit map.
122    */

123   private int privs;
124   
125   /**
126    * Constructor.
127    */

128   private Privileges(int privs) {
129     this.privs = privs & BIT_MASK;
130   }
131
132   public Privileges() {
133     this(0);
134   }
135
136   /**
137    * Adds a privilege and returns a new Privileges object with the new priv
138    * set.
139    */

140   public Privileges add(int priv) {
141     return new Privileges(privs | priv);
142   }
143
144   /**
145    * Removes a privilege with a column list parameter.
146    */

147   public Privileges remove(int priv) {
148     int and_priv = (privs & priv);
149     return new Privileges(privs ^ and_priv);
150   }
151
152   /**
153    * Removes the given privileges from this privileges object and returns the
154    * new privileges object.
155    */

156   public Privileges remove(Privileges privs) {
157     return remove(privs.privs);
158   }
159   
160   /**
161    * Returns true if this privileges permits the given priv.
162    */

163   public boolean permits(int priv) {
164     return (privs & priv) != 0;
165   }
166
167   /**
168    * Merges privs from the given privilege object with this set of privs.
169    * This performs an OR on all the attributes in the set. If the entry
170    * does not exist in this set then it is added.
171    */

172   public Privileges merge(Privileges in_privs) {
173     return add(in_privs.privs);
174   }
175
176   /**
177    * Returns true if this Privileges object contains no priv entries.
178    */

179   public boolean isEmpty() {
180     return privs == 0;
181   }
182
183   /**
184    * Returns a String that represents the given priv bit.
185    */

186   static String JavaDoc formatPriv(int priv) {
187     if ((priv & SELECT) != 0) {
188       return "SELECT";
189     }
190     else if ((priv & DELETE) != 0) {
191       return "DELETE";
192     }
193     else if ((priv & UPDATE) != 0) {
194       return "UPDATE";
195     }
196     else if ((priv & INSERT) != 0) {
197       return "INSERT";
198     }
199     else if ((priv & REFERENCES) != 0) {
200       return "REFERENCES";
201     }
202     else if ((priv & USAGE) != 0) {
203       return "USAGE";
204     }
205     else if ((priv & COMPACT) != 0) {
206       return "COMPACT";
207     }
208     else if ((priv & CREATE) != 0) {
209       return "CREATE";
210     }
211     else if ((priv & ALTER) != 0) {
212       return "ALTER";
213     }
214     else if ((priv & DROP) != 0) {
215       return "DROP";
216     }
217     else if ((priv & LIST) != 0) {
218       return "LIST";
219     }
220     else {
221       throw new Error JavaDoc("Not priv bit set.");
222     }
223   }
224   
225   /**
226    * Given a string, returns the priv bit for it.
227    */

228   public static int parseString(String JavaDoc priv) {
229     if (priv.equals("SELECT")) {
230       return SELECT;
231     }
232     else if (priv.equals("DELETE")) {
233       return DELETE;
234     }
235     else if (priv.equals("UPDATE")) {
236       return UPDATE;
237     }
238     else if (priv.equals("INSERT")) {
239       return INSERT;
240     }
241     else if (priv.equals("REFERENCES")) {
242       return REFERENCES;
243     }
244     else if (priv.equals("USAGE")) {
245       return USAGE;
246     }
247     else if (priv.equals("COMPACT")) {
248       return COMPACT;
249     }
250     else if (priv.equals("CREATE")) {
251       return CREATE;
252     }
253     else if (priv.equals("ALTER")) {
254       return ALTER;
255     }
256     else if (priv.equals("DROP")) {
257       return DROP;
258     }
259     else if (priv.equals("LIST")) {
260       return LIST;
261     }
262     else {
263       throw new Error JavaDoc("Priv not recognised.");
264     }
265   }
266   
267   /**
268    * Returns this Privileges object as an encoded int bit array.
269    */

270   public int toInt() {
271     return privs;
272   }
273   
274   /**
275    * Converts this privilege to an encoded string.
276    */

277   public String JavaDoc toEncodedString() {
278     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
279     buf.append("||");
280     int priv_bit = 1;
281     for (int i = 0; i < 11; ++i) {
282       if ((privs & priv_bit) != 0) {
283         buf.append(formatPriv(priv_bit));
284         buf.append("||");
285       }
286       priv_bit = priv_bit << 1;
287     }
288     return new String JavaDoc(buf);
289   }
290
291   public String JavaDoc toString() {
292     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
293     int priv_bit = 1;
294     for (int i = 0; i < 11; ++i) {
295       if ((privs & priv_bit) != 0) {
296         buf.append(formatPriv(priv_bit));
297         buf.append(' ');
298       }
299       priv_bit = priv_bit << 1;
300     }
301     return new String JavaDoc(buf);
302   }
303   
304   public boolean equals(Object JavaDoc ob) {
305     return privs == ((Privileges) ob).privs;
306   }
307
308   // ---------- More statics ----------
309

310   /**
311    * No privileges.
312    */

313   public final static Privileges EMPTY_PRIVS;
314   
315   /**
316    * Enable all privs for the object.
317    */

318   public final static Privileges TABLE_ALL_PRIVS;
319
320   /**
321    * Read privs for the object.
322    */

323   public final static Privileges TABLE_READ_PRIVS;
324
325   /**
326    * All access privs for a schema object.
327    */

328   public final static Privileges SCHEMA_ALL_PRIVS;
329
330   /**
331    * Read access privs for a schema object.
332    */

333   public final static Privileges SCHEMA_READ_PRIVS;
334   
335   /**
336    * All access (execute/update/delete/etc) privs for a procedure object.
337    */

338   public final static Privileges PROCEDURE_ALL_PRIVS;
339
340   /**
341    * Execute access privs for a procedure object.
342    */

343   public final static Privileges PROCEDURE_EXECUTE_PRIVS;
344   
345   
346   
347   static {
348     Privileges p;
349
350     EMPTY_PRIVS = new Privileges();
351
352     p = EMPTY_PRIVS;
353     p = p.add(SELECT);
354     p = p.add(DELETE);
355     p = p.add(UPDATE);
356     p = p.add(INSERT);
357     p = p.add(REFERENCES);
358     p = p.add(USAGE);
359     p = p.add(COMPACT);
360     TABLE_ALL_PRIVS = p;
361
362     p = EMPTY_PRIVS;
363     p = p.add(SELECT);
364     p = p.add(USAGE);
365     TABLE_READ_PRIVS = p;
366
367     p = EMPTY_PRIVS;
368     p = p.add(CREATE);
369     p = p.add(ALTER);
370     p = p.add(DROP);
371     p = p.add(LIST);
372     SCHEMA_ALL_PRIVS = p;
373
374     p = EMPTY_PRIVS;
375     p = p.add(LIST);
376     SCHEMA_READ_PRIVS = p;
377
378     p = EMPTY_PRIVS;
379     p = p.add(SELECT);
380     p = p.add(DELETE);
381     p = p.add(UPDATE);
382     p = p.add(INSERT);
383     PROCEDURE_ALL_PRIVS = p;
384
385     p = EMPTY_PRIVS;
386     p = p.add(SELECT);
387     PROCEDURE_EXECUTE_PRIVS = p;
388
389   }
390
391 }
392
Popular Tags