lasdf1234 commented on code in PR #11209: URL: https://github.com/apache/gravitino/pull/11209#discussion_r3295908271
########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/storage/relational/IdpJDBCBackend.java: ########## @@ -0,0 +1,220 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.idp.storage.relational; + +import static org.apache.gravitino.Configs.GARBAGE_COLLECTOR_SINGLE_DELETION_LIMIT; + +import com.google.common.collect.ImmutableMap; +import java.io.Closeable; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import org.apache.gravitino.Config; +import org.apache.gravitino.Configs; +import org.apache.gravitino.idp.exception.AlreadyExistsException; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.meta.IdpEntity; +import org.apache.gravitino.idp.meta.IdpEntityType; +import org.apache.gravitino.idp.meta.IdpGroupEntity; +import org.apache.gravitino.idp.meta.IdpUserEntity; +import org.apache.gravitino.idp.storage.converter.IdpPOConverters; +import org.apache.gravitino.idp.storage.po.IdpGroupPO; +import org.apache.gravitino.idp.storage.po.IdpUserPO; +import org.apache.gravitino.idp.storage.service.IdpGroupMetaService; +import org.apache.gravitino.idp.storage.service.IdpUserMetaService; +import org.apache.gravitino.storage.relational.JDBCBackend.JDBCBackendType; +import org.apache.gravitino.storage.relational.JDBCDatabase; +import org.apache.gravitino.storage.relational.converters.SQLExceptionConverterFactory; +import org.apache.gravitino.storage.relational.database.H2Database; +import org.apache.gravitino.storage.relational.session.SqlSessionFactoryHelper; + +/** JDBC backend for built-in IdP entities. */ +public class IdpJDBCBackend implements Closeable { + + private static final Map<JDBCBackendType, String> EMBEDDED_JDBC_DATABASE_MAP = + ImmutableMap.of(JDBCBackendType.H2, H2Database.class.getCanonicalName()); + + private JDBCDatabase jdbcDatabase; + + /** Initializes the JDBC backend and MyBatis session factory. */ + public void initialize(Config config) { + jdbcDatabase = startEmbeddedDatabaseIfNecessary(config); + SqlSessionFactoryHelper.getInstance().init(config); + SQLExceptionConverterFactory.initConverter(config); + } + + public boolean exists(String name, IdpEntityType entityType) throws IOException { + return entityExists(name, entityType); + } + + public <E extends IdpEntity> void insert(E entity, boolean overwritten) + throws AlreadyExistsException, IOException { + if (entity instanceof IdpUserEntity) { + insertIdpUser((IdpUserEntity) entity, overwritten); + } else if (entity instanceof IdpGroupEntity) { + insertIdpGroup((IdpGroupEntity) entity, overwritten); + } else { + throw new IllegalArgumentException( + String.format("Unsupported entity type: %s for insert operation", entity.type())); + } + } + + public <E extends IdpEntity> E get(String name, IdpEntityType entityType) throws IOException { + switch (entityType) { + case IDP_USER: + return (E) getIdpUser(name); + case IDP_GROUP: + return (E) getIdpGroup(name); + default: + throw new IllegalArgumentException( + String.format("Unsupported entity type: %s for get operation", entityType)); + } + } + + public void changePassword(String username, String passwordHash) throws NotFoundException { + if (!IdpUserMetaService.getInstance().updateIdpUserPassword(username, passwordHash)) { + throw new NotFoundException("IdP user %s does not exist", username); + } + } + + public void addUsersToGroup(String groupName, List<String> usernames) { + IdpGroupMetaService.getInstance().addUsersToGroup(groupName, usernames); + } + + public void removeUsersFromGroup(String groupName, List<String> usernames) { + IdpGroupMetaService.getInstance().removeUsersFromGroup(groupName, usernames); + } + + public boolean delete(String name, IdpEntityType entityType, boolean cascade) throws IOException { Review Comment: Got resolved ########## plugins/idp-basic/src/main/java/org/apache/gravitino/idp/storage/relational/IdpEntityStore.java: ########## @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.gravitino.idp.storage.relational; + +import java.io.IOException; +import java.util.List; +import org.apache.gravitino.Config; +import org.apache.gravitino.idp.exception.AlreadyExistsException; +import org.apache.gravitino.idp.exception.NotFoundException; +import org.apache.gravitino.idp.meta.IdpEntity; +import org.apache.gravitino.idp.meta.IdpEntityType; + +/** Entity store for built-in IdP entities backed by JDBC. */ +public class IdpEntityStore implements IdpStore { + + private IdpJDBCBackend backend; + private IdpGarbageCollector garbageCollector; + + @Override + public void initialize(Config config) throws RuntimeException { + this.backend = new IdpJDBCBackend(); + this.backend.initialize(config); + this.garbageCollector = new IdpGarbageCollector(backend, config); + this.garbageCollector.start(); + } + + @Override + public boolean exists(String name, IdpEntityType entityType) throws IOException { + return backend.exists(name, entityType); + } + + @Override + public <E extends IdpEntity> void put(E entity, boolean overwritten) + throws IOException, AlreadyExistsException { + backend.insert(entity, overwritten); + } + + @Override + public <E extends IdpEntity> E get(String name, IdpEntityType entityType, Class<E> clazz) + throws NotFoundException, IOException { + return clazz.cast(backend.get(name, entityType)); + } + + @Override + public void changePassword(String username, String passwordHash) throws NotFoundException { + backend.changePassword(username, passwordHash); + } + + @Override + public void addUsersToGroup(String groupName, List<String> usernames) { + backend.addUsersToGroup(groupName, usernames); + } + + @Override + public void removeUsersFromGroup(String groupName, List<String> usernames) { + backend.removeUsersFromGroup(groupName, usernames); + } + + @Override + public boolean delete(String name, IdpEntityType entityType) throws IOException { + return backend.delete(name, entityType, false); + } + + @Override + public void close() throws IOException { + garbageCollector.close(); + backend.close(); + } Review Comment: Got resolved -- 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]
