KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > compile > TablePrivilegesNode


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.TablePrivilegesNode
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.impl.sql.compile;
23
24 import org.apache.derby.iapi.error.StandardException;
25 import org.apache.derby.iapi.reference.SQLState;
26
27 import org.apache.derby.impl.sql.execute.PrivilegeInfo;
28 import org.apache.derby.impl.sql.execute.TablePrivilegeInfo;
29 import org.apache.derby.iapi.services.io.FormatableBitSet;
30 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
31
32 import org.apache.derby.iapi.sql.depend.DependencyManager;
33 import org.apache.derby.iapi.sql.depend.Provider;
34 import org.apache.derby.iapi.sql.depend.ProviderInfo;
35 import org.apache.derby.iapi.sql.depend.ProviderList;
36 import org.apache.derby.iapi.sql.conn.ConnectionUtil;
37 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext;
38 import org.apache.derby.iapi.sql.dictionary.AliasDescriptor;
39 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
40 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
41 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor;
42 import org.apache.derby.iapi.sql.dictionary.ViewDescriptor;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.List JavaDoc;
46
47 /**
48  * This class represents a set of privileges on one table.
49  */

50 public class TablePrivilegesNode extends QueryTreeNode
51 {
52     private boolean[] actionAllowed = new boolean[ TablePrivilegeInfo.ACTION_COUNT];
53     private ResultColumnList[] columnLists = new ResultColumnList[ TablePrivilegeInfo.ACTION_COUNT];
54     private FormatableBitSet[] columnBitSets = new FormatableBitSet[ TablePrivilegeInfo.ACTION_COUNT];
55     private TableDescriptor td;
56     private List descriptorList;
57     
58     /**
59      * Add all actions
60      */

61     public void addAll()
62     {
63         for( int i = 0; i < TablePrivilegeInfo.ACTION_COUNT; i++)
64         {
65             actionAllowed[i] = true;
66             columnLists[i] = null;
67         }
68     } // end of addAll
69

70     /**
71      * Add one action to the privileges for this table
72      *
73      * @param action The action type
74      * @param privilegeColumnList The set of privilege columns. Null for all columns
75      *
76      * @exception StandardException standard error policy.
77      */

78     public void addAction( int action, ResultColumnList privilegeColumnList)
79     {
80         actionAllowed[ action] = true;
81         if( privilegeColumnList == null)
82             columnLists[ action] = null;
83         else if( columnLists[ action] == null)
84             columnLists[ action] = privilegeColumnList;
85         else
86             columnLists[ action].appendResultColumns( privilegeColumnList, false);
87     } // end of addAction
88

89     /**
90      * Bind.
91      *
92      * @param td The table descriptor
93      * @param isGrant grant if true; revoke if false
94      */

95     public void bind( TableDescriptor td, boolean isGrant) throws StandardException
96     {
97         this.td = td;
98             
99         for( int action = 0; action < TablePrivilegeInfo.ACTION_COUNT; action++)
100         {
101             if( columnLists[ action] != null)
102                 columnBitSets[action] = columnLists[ action].bindResultColumnsByName( td, (DMLStatementNode) null);
103
104             // Prevent granting non-SELECT privileges to views
105
if (td.getTableType() == TableDescriptor.VIEW_TYPE && action != TablePrivilegeInfo.SELECT_ACTION)
106                 if (actionAllowed[action])
107                     throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED,
108                                     td.getQualifiedName());
109         }
110         
111         if (isGrant && td.getTableType() == TableDescriptor.VIEW_TYPE)
112         {
113             bindPrivilegesForView(td);
114         }
115     }
116     
117     /**
118      * @return PrivilegeInfo for this node
119      */

120     public PrivilegeInfo makePrivilegeInfo()
121     {
122         return new TablePrivilegeInfo( td, actionAllowed, columnBitSets,
123                 descriptorList);
124     }
125     
126     /**
127      * Retrieve all the underlying stored dependencies such as table(s),
128      * view(s) and routine(s) descriptors which the view depends on.
129      * This information is then passed to the runtime to determine if
130      * the privilege is grantable to the grantees by this grantor at
131      * execution time.
132      *
133      * Go through the providers regardless who the grantor is since
134      * the statement cache may be in effect.
135      *
136      * @param td the TableDescriptor to check
137      *
138      * @exception StandardException standard error policy.
139      */

140     private void bindPrivilegesForView ( TableDescriptor td)
141         throws StandardException
142     {
143         LanguageConnectionContext lcc = getLanguageConnectionContext();
144         DataDictionary dd = lcc.getDataDictionary();
145         ViewDescriptor vd = dd.getViewDescriptor(td);
146         DependencyManager dm = dd.getDependencyManager();
147         ProviderInfo[] pis = dm.getPersistentProviderInfos(vd);
148         this.descriptorList = new ArrayList JavaDoc();
149                     
150         int siz = pis.length;
151         for (int i=0; i < siz; i++)
152         {
153             try
154             {
155                 Provider provider = (Provider) pis[i].getDependableFinder().getDependable(pis[i].getObjectId());
156                 if (provider == null)
157                 {
158                     throw StandardException.newException(
159                             SQLState.LANG_OBJECT_NOT_FOUND,
160                             "OBJECT",
161                             pis[i].getObjectId());
162                 }
163                             
164                 if (provider instanceof TableDescriptor ||
165                     provider instanceof ViewDescriptor ||
166                     provider instanceof AliasDescriptor)
167                 {
168                     descriptorList.add(provider);
169                 }
170             }
171             catch(java.sql.SQLException JavaDoc ex)
172             {
173                 throw StandardException.plainWrapException(ex);
174             }
175         }
176     }
177     
178 }
179     
180
Popular Tags