KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.compile.PrivilegeNode
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.services.sanity.SanityManager;
26 import org.apache.derby.iapi.sql.depend.Provider;
27 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor;
28 import org.apache.derby.iapi.sql.dictionary.DataDictionary;
29 import org.apache.derby.iapi.sql.dictionary.TableDescriptor;
30 import org.apache.derby.iapi.sql.dictionary.AliasDescriptor;
31 import org.apache.derby.catalog.types.RoutineAliasInfo;
32 import org.apache.derby.catalog.AliasInfo;
33 import org.apache.derby.iapi.reference.SQLState;
34 import org.apache.derby.impl.sql.execute.PrivilegeInfo;
35 import org.apache.derby.catalog.TypeDescriptor;
36
37 import java.util.HashMap JavaDoc;
38 import java.util.List JavaDoc;
39
40 /**
41  * This node represents a set of privileges that are granted or revoked on one object.
42  */

43 public class PrivilegeNode extends QueryTreeNode
44 {
45     // Privilege object type
46
public static final int TABLE_PRIVILEGES = 0;
47     public static final int ROUTINE_PRIVILEGES = 1;
48
49     private int objectType;
50     private Object JavaDoc objectOfPrivilege;
51     private TablePrivilegesNode specificPrivileges; // Null for routines
52

53     /**
54      * initialize a PrivilegesNode
55      *
56      * @param objectType (an Integer)
57      * @param objectOfPrivilege (a TableName or RoutineDesignator)
58      * @param specificPrivileges null for routines
59      */

60     public void init( Object JavaDoc objectType, Object JavaDoc objectOfPrivilege, Object JavaDoc specificPrivileges)
61     {
62         this.objectType = ((Integer JavaDoc) objectType).intValue();
63         this.objectOfPrivilege = objectOfPrivilege;
64         this.specificPrivileges = (TablePrivilegesNode) specificPrivileges;
65         if( SanityManager.DEBUG)
66         {
67             SanityManager.ASSERT( objectOfPrivilege != null,
68                                   "null privilge object");
69             switch( this.objectType)
70             {
71             case TABLE_PRIVILEGES:
72                 SanityManager.ASSERT( objectOfPrivilege instanceof TableName,
73                                       "incorrect name type, " + objectOfPrivilege.getClass().getName()
74                                       + ", used with table privilege");
75                 SanityManager.ASSERT( specificPrivileges != null,
76                                       "null specific privileges used with table privilege");
77                 break;
78
79             case ROUTINE_PRIVILEGES:
80                 SanityManager.ASSERT( objectOfPrivilege instanceof RoutineDesignator,
81                                       "incorrect name type, " + objectOfPrivilege.getClass().getName()
82                                       + ", used with table privilege");
83                 SanityManager.ASSERT( specificPrivileges == null,
84                                       "non-null specific privileges used with execute privilege");
85                 break;
86
87             default:
88                 SanityManager.THROWASSERT( "Invalid privilege objectType: " + this.objectType);
89             }
90         }
91     } // end of init
92

93     /**
94      * Bind this GrantNode. Resolve all table, column, and routine references. Register
95      * a dependency on the object of the privilege if it has not already been done
96      *
97      * @param dependencies The list of privilege objects that this statement has already seen.
98      * If the object of this privilege is not in the list then this statement is registered
99      * as dependent on the object.
100      * @param grantees The list of grantees
101      * @param isGrant grant if true; revoke if false
102      * @return the bound node
103      *
104      * @exception StandardException Standard error policy.
105      */

106     public QueryTreeNode bind( HashMap JavaDoc dependencies, List JavaDoc grantees, boolean isGrant ) throws StandardException
107     {
108         Provider dependencyProvider = null;
109         SchemaDescriptor sd = null;
110         
111         switch( objectType)
112         {
113         case TABLE_PRIVILEGES:
114             TableName tableName = (TableName) objectOfPrivilege;
115             sd = getSchemaDescriptor( tableName.getSchemaName(), true);
116             if (sd.isSystemSchema())
117                 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED, tableName.getFullTableName());
118                 
119             TableDescriptor td = getTableDescriptor( tableName.getTableName(), sd);
120             if( td == null)
121                 throw StandardException.newException( SQLState.LANG_TABLE_NOT_FOUND, tableName);
122
123             // Don't allow authorization on SESSION schema tables. Causes confusion if
124
// a temporary table is created later with same name.
125
if (isSessionSchema(sd.getSchemaName()))
126                 throw StandardException.newException(SQLState.LANG_OPERATION_NOT_ALLOWED_ON_SESSION_SCHEMA_TABLES);
127
128             if (td.getTableType() != TableDescriptor.BASE_TABLE_TYPE &&
129                     td.getTableType() != TableDescriptor.VIEW_TYPE)
130                 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED, tableName.getFullTableName());
131
132             // Can not grant/revoke permissions from self
133
if (grantees.contains(sd.getAuthorizationId()))
134                 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED,
135                          td.getQualifiedName());
136
137             specificPrivileges.bind( td, isGrant);
138             dependencyProvider = td;
139             break;
140
141         case ROUTINE_PRIVILEGES:
142             RoutineDesignator rd = (RoutineDesignator) objectOfPrivilege;
143             sd = getSchemaDescriptor( rd.name.getSchemaName(), true);
144
145             if (!sd.isSchemaWithGrantableRoutines())
146                 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED, rd.name.getFullTableName());
147                 
148             AliasDescriptor proc = null;
149             RoutineAliasInfo routineInfo = null;
150             java.util.List JavaDoc list = getDataDictionary().getRoutineList(
151                 sd.getUUID().toString(), rd.name.getTableName(),
152                 rd.isFunction ? AliasInfo.ALIAS_NAME_SPACE_FUNCTION_AS_CHAR : AliasInfo.ALIAS_NAME_SPACE_PROCEDURE_AS_CHAR
153                 );
154
155             // Can not grant/revoke permissions from self
156
if (grantees.contains(sd.getAuthorizationId()))
157                 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED,
158                          rd.name.getFullTableName());
159
160             if( rd.paramTypeList == null)
161             {
162                 // No signature was specified. Make sure that there is exactly one routine with that name.
163
if( list.size() > 1)
164                     throw StandardException.newException( ( rd.isFunction ? SQLState.LANG_AMBIGUOUS_FUNCTION_NAME
165                                                             : SQLState.LANG_AMBIGUOUS_PROCEDURE_NAME),
166                                                           rd.name.getFullTableName());
167                 if( list.size() != 1)
168                     throw StandardException.newException(SQLState.LANG_NO_SUCH_METHOD_ALIAS, rd.name.getFullTableName());
169                 proc = (AliasDescriptor) list.get(0);
170             }
171             else
172             {
173                 // The full signature was specified
174
boolean found = false;
175                 for (int i = list.size() - 1; (!found) && i >= 0; i--)
176                 {
177                     proc = (AliasDescriptor) list.get(i);
178
179                     routineInfo = (RoutineAliasInfo) proc.getAliasInfo();
180                     int parameterCount = routineInfo.getParameterCount();
181                     if (parameterCount != rd.paramTypeList.size())
182                         continue;
183                     TypeDescriptor[] parameterTypes = routineInfo.getParameterTypes();
184                     found = true;
185                     for( int parmIdx = 0; parmIdx < parameterCount; parmIdx++)
186                     {
187                         if( ! parameterTypes[parmIdx].equals( rd.paramTypeList.get( parmIdx)))
188                         {
189                             found = false;
190                             break;
191                         }
192                     }
193                 }
194                 if( ! found)
195                 {
196                     // reconstruct the signature for the error message
197
StringBuffer JavaDoc sb = new StringBuffer JavaDoc( rd.name.getFullTableName());
198                     sb.append( "(");
199                     for( int i = 0; i < rd.paramTypeList.size(); i++)
200                     {
201                         if( i > 0)
202                             sb.append(",");
203                         sb.append( rd.paramTypeList.get(i).toString());
204                     }
205                     throw StandardException.newException(SQLState.LANG_NO_SUCH_METHOD_ALIAS, sb.toString());
206                 }
207             }
208             rd.setAliasDescriptor( proc);
209             dependencyProvider = proc;
210             break;
211         }
212
213         if( dependencyProvider != null)
214         {
215             if( dependencies.get( dependencyProvider) == null)
216             {
217                 getCompilerContext().createDependency( dependencyProvider);
218                 dependencies.put( dependencyProvider, dependencyProvider);
219             }
220         }
221         return this;
222     } // end of bind
223

224     /**
225      * @return PrivilegeInfo for this node
226      */

227     PrivilegeInfo makePrivilegeInfo()
228     {
229         switch( objectType)
230         {
231         case TABLE_PRIVILEGES:
232             return specificPrivileges.makePrivilegeInfo();
233
234         case ROUTINE_PRIVILEGES:
235             return ((RoutineDesignator) objectOfPrivilege).makePrivilegeInfo();
236         }
237         return null;
238     }
239 }
240
Popular Tags