KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datadictionarysystem > PrivilegeTable


1 package com.daffodilwoods.daffodildb.server.datadictionarysystem;
2
3 import java.util.*;
4
5 import com.daffodilwoods.daffodildb.server.sql99.*;
6 import com.daffodilwoods.daffodildb.server.sql99.common.*;
7 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*;
8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*;
9 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*;
10 import com.daffodilwoods.daffodildb.server.sql99.utils.*;
11 import com.daffodilwoods.database.general.*;
12 import com.daffodilwoods.database.resource.*;
13
14 public class PrivilegeTable implements _PrivilegeTable {
15    int[] rights;
16    ArrayList column_rights;
17    QualifiedIdentifier tableName;
18    PreparedStatementGetter preparedStatementGetter;
19    _ColumnCharacteristics columnCharacterisitics;
20    booleanvalueexpression condition;
21    String JavaDoc authorizationIdentifier;
22    Object JavaDoc[] applicableRoles;
23    Object JavaDoc[] allGrantees;
24
25    public PrivilegeTable(_DataDictionary data_dictionary,
26                          PreparedStatementGetter preparedStatementGetter0,
27                          QualifiedIdentifier table_name) throws DException {
28       preparedStatementGetter = preparedStatementGetter0;
29       condition = QueryGetter.getPrivilegeCondition();
30       tableName = table_name;
31       if (table_name != null) {
32          try {
33             columnCharacterisitics = data_dictionary.getColumnCharacteristics(table_name, true);
34             int columnCount = columnCharacterisitics.getColumnCount();
35             column_rights = new ArrayList(columnCount);
36             for (int i = columnCount; i-- > 0; )
37                column_rights.add(null);
38          } catch (TableException te) {
39             InitializeException ie = new InitializeException("DSE1108", new Object JavaDoc[] {
40                 table_name
41             });
42             ie.setException(te);
43             throw ie;
44          }
45       }
46    }
47
48    public void setAuthorization(String JavaDoc authId) throws DException {
49       authorizationIdentifier = authId;
50    }
51
52    public void setApplicableRole(Object JavaDoc[] applicableRoles0) throws DException {
53       applicableRoles = applicableRoles0;
54    }
55
56    public synchronized boolean hasTablePrivileges(int operationType) throws DException {
57       if (rights == null)
58          setTableRights(operationType);
59       return rights[operationType] != NOACCESS;
60    }
61
62    public synchronized int getTablePrivilegesType(int operationType) throws DException {
63       if (rights == null)
64          setTableRights(operationType);
65       return rights[operationType];
66    }
67
68    private synchronized void setTableRights(int operationType) throws DException {
69       setAllGrantees();
70       _SelectQueryIterator table_iterator = getIterator(
71           preparedStatementGetter.getTablePrivilegesForTableExecuter(),
72           new Object JavaDoc[] {tableName.catalog, tableName.schema, tableName.name, allGrantees});
73       int[] temp = new int[] {NOACCESS, NOACCESS, NOACCESS, NOACCESS, NOACCESS, NOACCESS, NOACCESS};
74
75       if (tableName.getIdentifier().equalsIgnoreCase(SystemTables.dualSystemTable.toString()))
76          temp = new int[] {NOACCESS, NOACCESS, NOACCESS, RIGHTWITHGRANTOPTION, NOACCESS, NOACCESS, NOACCESS};
77
78       if (table_iterator.first()) {
79          Object JavaDoc[] record = null;
80          do {
81             record = (Object JavaDoc[]) table_iterator.getObject();
82             boolean isGrantable = "YES".equalsIgnoreCase( (String JavaDoc) record[1]);
83             temp[getOperationType( (String JavaDoc) record[0])] = isGrantable
84                 ? RIGHTWITHGRANTOPTION : ONLYRIGHT;
85          } while (table_iterator.next());
86       }
87       rights = temp;
88    }
89
90    private void setAllGrantees() {
91       if (allGrantees == null) {
92          int len = applicableRoles == null ? 0 : applicableRoles.length;
93          allGrantees = new Object JavaDoc[2 + len];
94          allGrantees[0] = authorizationIdentifier;
95          allGrantees[1] = SqlKeywords.PUBLIC;
96          if (len != 0)
97             System.arraycopy(applicableRoles, 0, allGrantees, 2, len);
98       }
99    }
100
101    public boolean hasColumnPrivileges(int operationType, int[] columnIndexes) throws DException {
102       for (int i = 0; i < columnIndexes.length; i++) {
103          boolean be = hasColumnRights(columnIndexes[i], operationType);
104          if (!be)
105             return false;
106       }
107       return true;
108    }
109
110    public boolean hasColumnPrivilegesWithGrantOption(int columnIndex,
111        int operationType) throws DException {
112       Object JavaDoc obj = column_rights.get(columnIndex);
113       if (obj == null)
114          obj = setColumnRights(columnIndex, operationType);
115       int[] col_rig = (int[]) obj;
116       return col_rig[operationType] == RIGHTWITHGRANTOPTION;
117    }
118
119    public int getColumnPrivilegesType(int columnIndex,
120                                       int operationType) throws DException {
121       Object JavaDoc obj = column_rights.get(columnIndex);
122       if (obj == null)
123          obj = setColumnRights(columnIndex, operationType);
124       int[] col_rig = (int[]) obj;
125       return col_rig[operationType];
126    }
127
128    private boolean hasColumnRights(int column_index, int operationType) throws DException {
129       Object JavaDoc obj = column_rights.get(column_index);
130       if (obj == null)
131          obj = setColumnRights(column_index, operationType);
132       int[] col_rig = (int[]) obj;
133       return col_rig[operationType] != NOACCESS;
134    }
135
136    private synchronized int[] setColumnRights(int columnIndex,
137                                               int operationType) throws DException {
138       setAllGrantees();
139       _SelectQueryIterator table_iterator = getIterator(
140           preparedStatementGetter.getColumnPrivilegesForColumnExecuter(),
141           new Object JavaDoc[] {tableName.catalog, tableName.schema, tableName.name, columnCharacterisitics.getColumnName(columnIndex), allGrantees});
142
143       int[] rights = new int[] {NOACCESS, NOACCESS, NOACCESS, NOACCESS, NOACCESS, NOACCESS, NOACCESS};
144
145       if (tableName.getIdentifier().equalsIgnoreCase(SystemTables.dualSystemTable.toString()))
146          rights = new int[] {NOACCESS, NOACCESS, NOACCESS, RIGHTWITHGRANTOPTION, NOACCESS, NOACCESS, NOACCESS};
147
148       if (table_iterator.first()) {
149          Object JavaDoc[] record = null;
150          do {
151             record = (Object JavaDoc[]) table_iterator.getObject();
152             boolean isGrantable = "YES".equalsIgnoreCase( (String JavaDoc) record[1]);
153             rights[getOperationType( (String JavaDoc) record[0])] = isGrantable
154                 ? RIGHTWITHGRANTOPTION : ONLYRIGHT;
155          } while (table_iterator.next());
156       }
157       column_rights.set(columnIndex, rights);
158       return rights;
159    }
160
161    public ParameterisedCondition getPrivilegeCondition() throws DException {
162       ParameterisedCondition pc = new ParameterisedCondition();
163       pc.setBVE(condition);
164       /** @todo set runTimevariables */
165
166       VariableValues vv = new VariableValues(preparedStatementGetter.serverSession);
167       pc.setVariableValues(vv);
168       return pc;
169    }
170
171    public boolean hasExecutionRights(String JavaDoc specific_catalog,
172                                      String JavaDoc specific_schema,
173                                      String JavaDoc specific_name) throws DException {
174       setAllGrantees();
175       _Iterator iter = getIterator(
176           preparedStatementGetter.getRoutinePrivilegesExecuter(),
177           new Object JavaDoc[] {specific_catalog, specific_schema, specific_name, allGrantees});
178       return iter.first();
179    }
180
181    private int getOperationType(String JavaDoc privilege) {
182       if (privilege.equalsIgnoreCase(SqlKeywords.INSERT))
183          return _PrivilegeTable.INSERT;
184       if (privilege.equalsIgnoreCase(SqlKeywords.UPDATE))
185          return _PrivilegeTable.UPDATE;
186       if (privilege.equalsIgnoreCase(SqlKeywords.DELETE))
187          return _PrivilegeTable.DELETE;
188       if (privilege.equalsIgnoreCase(SqlKeywords.SELECT))
189          return _PrivilegeTable.SELECT;
190       if (privilege.equalsIgnoreCase(SqlKeywords.TRIGGER))
191          return _PrivilegeTable.TRIGGER;
192       if (privilege.equalsIgnoreCase(SqlKeywords.REFERENCES))
193          return _PrivilegeTable.REFERENCES;
194       return -1;
195    }
196
197
198    private _SelectQueryIterator getIterator(_Executer executer, Object JavaDoc[] parameters) throws DException {
199       return (_SelectQueryIterator) executer.executeForFresh(parameters);
200    }
201 }
202
Popular Tags