Skip to content

Commit

Permalink
refactor JdbcProperties.java
Browse files Browse the repository at this point in the history
  • Loading branch information
xunliu committed Dec 24, 2024
1 parent f5a7933 commit b52e9e0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,39 @@
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.gravitino.authorization.jdbc;
package org.apache.gravitino.authorization;

import java.util.Map;

/** The properties for JDBC authorization plugin. */
public class JdbcAuthorizationProperties {
public class JdbcAuthorizationProperties extends AuthorizationProperties {
private static final String CONFIG_PREFIX = "authorization.jdbc.";
public static final String JDBC_PASSWORD = CONFIG_PREFIX + "password";
public static final String JDBC_USERNAME = CONFIG_PREFIX + "username";
public static final String JDBC_URL = CONFIG_PREFIX + "url";
public static final String JDBC_DRIVER = CONFIG_PREFIX + "driver";

public static void validate(Map<String, String> properties) {
String errorMsg = "%s is required";
check(properties, JDBC_URL, errorMsg);
check(properties, JDBC_USERNAME, errorMsg);
check(properties, JDBC_PASSWORD, errorMsg);
check(properties, JDBC_DRIVER, errorMsg);
public JdbcAuthorizationProperties(Map<String, String> properties) {
super(properties);
}

private static void check(Map<String, String> properties, String key, String errorMsg) {
private void check(String key, String errorMsg) {
if (!properties.containsKey(key) && properties.get(key) != null) {
throw new IllegalArgumentException(String.format(errorMsg, key));
}
}

@Override
String getPropertiesPrefix() {
return CONFIG_PREFIX;
}

@Override
public void validate() {
String errorMsg = "%s is required";
check(JDBC_URL, errorMsg);
check(JDBC_USERNAME, errorMsg);
check(JDBC_PASSWORD, errorMsg);
check(JDBC_DRIVER, errorMsg);
}
}
4 changes: 3 additions & 1 deletion authorizations/authorization-jdbc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ dependencies {
implementation(project(":core")) {
exclude(group = "*")
}

implementation(project(":authorizations:authorization-common")) {
exclude(group = "*")
}
implementation(libs.bundles.log4j)
implementation(libs.commons.lang3)
implementation(libs.guava)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.gravitino.authorization.AuthorizationPrivilege;
import org.apache.gravitino.authorization.AuthorizationSecurableObject;
import org.apache.gravitino.authorization.Group;
import org.apache.gravitino.authorization.JdbcAuthorizationProperties;
import org.apache.gravitino.authorization.MetadataObjectChange;
import org.apache.gravitino.authorization.Owner;
import org.apache.gravitino.authorization.Role;
Expand Down Expand Up @@ -65,7 +66,8 @@ abstract class JdbcAuthorizationPlugin implements AuthorizationPlugin, JdbcAutho
public JdbcAuthorizationPlugin(Map<String, String> config) {
// Initialize the data source
dataSource = new BasicDataSource();
JdbcAuthorizationProperties.validate(config);
JdbcAuthorizationProperties jdbcAuthProperties = new JdbcAuthorizationProperties(config);
jdbcAuthProperties.validate();

String jdbcUrl = config.get(JdbcAuthorizationProperties.JDBC_URL);
dataSource.setUrl(jdbcUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.gravitino.MetadataObject;
import org.apache.gravitino.MetadataObjects;
import org.apache.gravitino.authorization.Group;
import org.apache.gravitino.authorization.JdbcAuthorizationProperties;
import org.apache.gravitino.authorization.Owner;
import org.apache.gravitino.authorization.Privileges;
import org.apache.gravitino.authorization.Role;
Expand Down

0 comments on commit b52e9e0

Please sign in to comment.