jerryshao commented on code in PR #5021:
URL: https://github.com/apache/gravitino/pull/5021#discussion_r1778407373


##########
core/src/main/java/org/apache/gravitino/authorization/AuthorizationUtils.java:
##########
@@ -207,6 +208,67 @@ public static boolean 
needApplyAuthorizationPluginAllCatalogs(SecurableObject se
     return false;
   }
 
+  // Check every securable object whether exists and is imported.
+  public static void checkSecurableObject(String metalake, MetadataObject 
object) {
+    NameIdentifier identifier;
+
+    // Securable object ignores the metalake namespace, so we should add it 
back.
+    if (object.type() == MetadataObject.Type.METALAKE) {
+      identifier = NameIdentifier.parse(object.fullName());
+    } else {
+      identifier = NameIdentifier.parse(String.format("%s.%s", metalake, 
object.fullName()));
+    }
+
+    String existErrMsg = "Securable object %s doesn't exist";
+
+    switch (object.type()) {
+      case METALAKE:
+        if 
(!GravitinoEnv.getInstance().metalakeDispatcher().metalakeExists(identifier)) {
+          throw new NoSuchMetadataObjectException(existErrMsg, 
object.fullName());
+        }
+
+        break;
+
+      case CATALOG:
+        if 
(!GravitinoEnv.getInstance().catalogDispatcher().catalogExists(identifier)) {
+          throw new NoSuchMetadataObjectException(existErrMsg, 
object.fullName());
+        }
+
+        break;
+
+      case SCHEMA:
+        if 
(!GravitinoEnv.getInstance().schemaDispatcher().schemaExists(identifier)) {
+          throw new NoSuchMetadataObjectException(existErrMsg, 
object.fullName());
+        }
+
+        break;
+
+      case FILESET:
+        if 
(!GravitinoEnv.getInstance().filesetDispatcher().filesetExists(identifier)) {
+          throw new NoSuchMetadataObjectException(existErrMsg, 
object.fullName());
+        }
+
+        break;
+      case TABLE:
+        if 
(!GravitinoEnv.getInstance().tableDispatcher().tableExists(identifier)) {
+          throw new NoSuchMetadataObjectException(existErrMsg, 
object.fullName());
+        }
+
+        break;
+
+      case TOPIC:
+        if 
(!GravitinoEnv.getInstance().topicDispatcher().topicExists(identifier)) {
+          throw new NoSuchMetadataObjectException(existErrMsg, 
object.fullName());
+        }
+
+        break;
+
+      default:
+        throw new IllegalArgumentException(
+            String.format("Doesn't support the type %s", object.type()));
+    }
+  }
+

Review Comment:
   I think we can create a helper method like this to simplify the code.
   
   ```java
   public static void check(final boolean expression, Supplier<? extends 
RuntimeException> exceptionToThrowSupplier) {
       if (!expression) {
           throw checkNotNull(exceptionToThrowSupplier).get();
       }
   }
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to