1 30 31 32 package org.hsqldb; 33 34 import java.lang.reflect.Method ; 35 import java.lang.reflect.Modifier ; 36 37 import org.hsqldb.lib.HashMap; 38 import org.hsqldb.lib.HashSet; 39 import org.hsqldb.lib.HsqlArrayList; 40 import org.hsqldb.lib.Iterator; 41 import org.hsqldb.lib.WrapperIterator; 42 43 49 61 final class DINameSpace { 62 63 64 private Database database; 65 66 67 private String catalogName; 68 69 77 private static HashSet builtin = new HashSet(); 78 79 static { 83 builtin.add("org.hsqldb.Library"); 84 builtin.add("java.lang.Math"); 85 } 86 87 95 public DINameSpace(Database database) throws HsqlException { 96 97 try { 98 this.database = database; 99 this.catalogName = database.getURI(); 100 } catch (Exception e) { 101 Trace.throwerror(Trace.GENERAL_ERROR, e.toString()); 102 } 103 } 104 105 115 Class classForMethodFQN(String fqn) { 116 117 try { 118 return classForName(fqn.substring(0, fqn.lastIndexOf('.'))); 119 } catch (Exception e) { 120 return null; 121 } 122 } 123 124 136 Class classForName(String name) throws ClassNotFoundException { 137 138 try { 139 if (database.classLoader == null) { 140 return Class.forName(name); 141 } else { 142 if (name != null) { 143 return database.classLoader.loadClass(name); 144 } else { 145 throw new ClassNotFoundException (); 146 } 147 } 148 } catch (NoClassDefFoundError err) { 149 throw new ClassNotFoundException (err.toString()); 150 } 151 } 152 153 170 Iterator iterateCatalogNames() throws HsqlException { 171 return isReportCatalogs() ? new WrapperIterator(catalogName) 172 : new WrapperIterator(); 173 } 174 175 191 String getCatalogName(Object o) { 192 return isReportCatalogs() ? catalogName 193 : null; 194 } 195 196 203 HashMap getInverseAliasMap() { 204 205 HashMap mapIn; 206 HashMap mapOut; 207 Iterator keys; 208 Object key; 209 Object value; 210 HsqlArrayList keyList; 211 212 mapIn = database.getAliasMap(); 218 mapOut = new HashMap(); 219 keys = mapIn.keySet().iterator(); 220 221 while (keys.hasNext()) { 222 key = keys.next(); 223 value = mapIn.get(key); 224 keyList = (HsqlArrayList) mapOut.get(value); 225 226 if (keyList == null) { 227 keyList = new HsqlArrayList(); 228 229 mapOut.put(value, keyList); 230 } 231 232 keyList.add(key); 233 } 234 235 return mapOut; 236 } 237 238 245 static String getMethodFQN(Method m) { 246 247 return m == null ? null 248 : m.getDeclaringClass().getName() + '.' 249 + m.getName(); 250 } 251 252 258 static String getMethodSpecificName(Method m) { 259 260 return m == null ? null 261 : m.getDeclaringClass().getName() + '.' 262 + getSignature(m); 263 } 264 265 static String getSignature(Method method) { 266 267 StringBuffer sb; 268 String signature; 269 Class [] parmTypes; 270 int len; 271 int last; 272 273 sb = new StringBuffer (); 274 parmTypes = method.getParameterTypes(); 275 len = parmTypes.length; 276 last = len - 1; 277 278 sb.append(method.getName()).append('('); 279 280 for (int i = 0; i < len; i++) { 281 sb.append(parmTypes[i].getName()); 282 283 if (i < last) { 284 sb.append(','); 285 } 286 } 287 288 sb.append(')'); 289 290 signature = sb.toString(); 291 292 return signature; 293 } 294 295 298 String getSchemaName(Object o) { 299 return database.schemaManager.PUBLIC_SCHEMA; 300 } 301 302 310 void addBuiltinToSet(HashSet set) { 311 set.addAll(builtin.toArray(new String [builtin.size()])); 312 } 313 314 322 boolean isBuiltin(Class clazz) { 323 return clazz == null ? false 324 : builtin.contains(clazz.getName()); 325 } 326 327 335 boolean isBuiltin(String name) { 336 return (name == null) ? false 337 : builtin.contains(name); 338 } 339 340 384 Iterator iterateRoutineMethods(String className, 385 boolean andAliases) throws HsqlException { 386 387 Class clazz; 388 Method [] methods; 389 Method method; 390 int mods; 391 Object [] info; 392 HsqlArrayList aliasList; 393 HsqlArrayList methodList; 394 HashMap invAliasMap; 395 396 try { 397 clazz = classForName(className); 398 } catch (ClassNotFoundException e) { 399 return new WrapperIterator(); 400 } 401 402 invAliasMap = andAliases ? getInverseAliasMap() 403 : null; 404 405 try { 412 methods = clazz.getDeclaredMethods(); 413 } catch (Exception e) { 414 methods = clazz.getMethods(); 415 } 416 417 methodList = new HsqlArrayList(methods.length); 418 419 for (int i = 0; i < methods.length; i++) { 421 method = methods[i]; 422 mods = method.getModifiers(); 423 424 if (!(Modifier.isPublic(mods) && Modifier.isStatic(mods))) { 425 continue; 426 } 427 428 info = new Object [] { 429 method, null, "ROUTINE" 430 }; 431 432 if (andAliases) { 433 info[1] = invAliasMap.get(getMethodFQN(method)); 434 } 435 436 methodList.add(info); 437 } 438 439 return methodList.iterator(); 441 } 442 443 460 Iterator iterateAccessibleTriggerClassNames(User user) 461 throws HsqlException { 462 463 Table table; 464 Class clazz; 465 HashSet classSet; 466 TriggerDef triggerDef; 467 HsqlArrayList[] triggerLists; 468 HsqlArrayList triggerList; 469 HsqlArrayList tableList; 470 int listSize; 471 472 classSet = new HashSet(); 473 474 Iterator schemas = database.schemaManager.userSchemaNameIterator(); 475 476 while (schemas.hasNext()) { 477 String schema = (String ) schemas.next(); 478 Iterator tables = database.schemaManager.tablesIterator(schema); 479 480 while (tables.hasNext()) { 481 table = (Table) tables.next(); 482 483 if (!user.isAccessible(table.getName())) { 484 continue; 485 } 486 487 triggerLists = table.triggerLists; 488 489 if (triggerLists == null) { 490 continue; 491 } 492 493 for (int j = 0; j < triggerLists.length; j++) { 494 triggerList = triggerLists[j]; 495 496 if (triggerList == null) { 497 continue; 498 } 499 500 listSize = triggerList.size(); 501 502 for (int k = 0; k < listSize; k++) { 503 triggerDef = (TriggerDef) triggerList.get(k); 504 505 if (triggerDef == null ||!triggerDef.valid 506 || triggerDef.trigger == null 507 ||!user.isAccessible( 508 table.getName(), 509 TriggerDef.indexToRight(k))) { 510 continue; 511 } 512 513 classSet.add(triggerDef.trigger.getClass().getName()); 514 } 515 } 516 } 517 } 518 519 return classSet.iterator(); 520 } 521 522 535 Iterator iterateAllAccessibleMethods(Session session, 536 boolean andAliases) 537 throws HsqlException { 538 539 Iterator out; 540 HashSet classNameSet; 541 Iterator classNames; 542 Iterator methods; 543 String className; 544 545 out = new WrapperIterator(); 546 classNameSet = session.getUser().getGrantedClassNames(true); 547 548 addBuiltinToSet(classNameSet); 549 550 classNames = classNameSet.iterator(); 551 552 while (classNames.hasNext()) { 553 className = (String ) classNames.next(); 554 methods = iterateRoutineMethods(className, andAliases); 555 out = new WrapperIterator(out, methods); 556 } 557 558 return out; 559 } 560 561 569 Session[] listVisibleSessions(Session session) { 570 return database.sessionManager.getVisibleSessions(session); 571 } 572 573 577 boolean isReportCatalogs() { 578 return database.getProperties().isPropertyTrue("hsqldb.catalogs"); 579 } 580 } 581 | Popular Tags |