This is an automated email from the ASF dual-hosted git repository. abhay pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/ranger.git
The following commit(s) were added to refs/heads/master by this push: new ba917a6 RANGER-3578: Simplify code for policy label creation ba917a6 is described below commit ba917a683dae46e534e4da388128d3eb9ab97af9 Author: Abhay Kulkarni <ab...@apache.org> AuthorDate: Sat Jan 8 11:45:50 2022 -0800 RANGER-3578: Simplify code for policy label creation --- .../java/org/apache/ranger/biz/ServiceDBStore.java | 63 ++++++++++++++------ .../ranger/service/RangerPolicyLabelHelper.java | 68 ---------------------- 2 files changed, 46 insertions(+), 85 deletions(-) diff --git a/security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java b/security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java index 85adda5..6ed0800 100644 --- a/security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java +++ b/security-admin/src/main/java/org/apache/ranger/biz/ServiceDBStore.java @@ -62,7 +62,6 @@ import org.apache.poi.ss.usermodel.Workbook; import org.apache.ranger.audit.provider.MiscUtil; import org.apache.ranger.authorization.hadoop.config.RangerAdminConfig; import org.apache.ranger.authorization.utils.JsonUtils; -import org.apache.ranger.biz.ServiceDBStore.METRIC_TYPE; import org.apache.ranger.common.AppConstants; import org.apache.ranger.common.ContextUtil; import org.apache.ranger.common.GUIDUtil; @@ -170,7 +169,6 @@ import org.apache.ranger.rest.ServiceREST; import org.apache.ranger.rest.TagREST; import org.apache.ranger.service.RangerAuditFields; import org.apache.ranger.service.RangerDataHistService; -import org.apache.ranger.service.RangerPolicyLabelHelper; import org.apache.ranger.service.RangerPolicyLabelsService; import org.apache.ranger.service.RangerPolicyService; import org.apache.ranger.service.RangerPolicyWithAssignedIdService; @@ -285,9 +283,6 @@ public class ServiceDBStore extends AbstractServiceStore { RangerPolicyLabelsService<XXPolicyLabel, ?> policyLabelsService; @Autowired - RangerPolicyLabelHelper policyLabelsHelper; - - @Autowired XUserService xUserService; @Autowired @@ -2072,24 +2067,58 @@ public class ServiceDBStore extends AbstractServiceStore { for (String policyLabel : uniquePolicyLabels) { //check and create new label If does not exist + if (StringUtils.isNotEmpty(policyLabel)) { + transactionSynchronizationAdapter.executeOnTransactionCommit(new AssociatePolicyLabel(policyLabel, xPolicy)); + } + } + + if (LOG.isDebugEnabled()) { + LOG.debug("<== ServiceDBStore.createOrMapLabels()"); + } + } + + private class AssociatePolicyLabel implements Runnable { + private String policyLabel; + private XXPolicy xPolicy; + + AssociatePolicyLabel(String policyLabel, XXPolicy xPolicy) { + this.policyLabel = policyLabel; + this.xPolicy = xPolicy; + } + + @Override + public void run() { + getOrCreateLabel(); + } + + private void getOrCreateLabel() { + if (LOG.isDebugEnabled()) { + LOG.debug("==> AssociatePolicyLabel.getOrCreateLabel(policyId=" + xPolicy.getId() + ", label=" + policyLabel + ")"); + } + XXPolicyLabel xxPolicyLabel = daoMgr.getXXPolicyLabels().findByName(policyLabel); - if(xxPolicyLabel == null) { - synchronized(this) { - xxPolicyLabel = policyLabelsHelper.createNewOrGetLabel(policyLabel, xPolicy); + + if (xxPolicyLabel == null) { + xxPolicyLabel = daoMgr.getXXPolicyLabels().findByName(policyLabel); + + if (xxPolicyLabel == null) { + xxPolicyLabel = new XXPolicyLabel(); + xxPolicyLabel.setPolicyLabel(policyLabel); + xxPolicyLabel = rangerAuditFields.populateAuditFieldsForCreate(xxPolicyLabel); + xxPolicyLabel = daoMgr.getXXPolicyLabels().create(xxPolicyLabel); } } - //label mapping with policy - if (xxPolicyLabel.getId() != null) { + + if (xxPolicyLabel != null) { XXPolicyLabelMap xxPolicyLabelMap = new XXPolicyLabelMap(); xxPolicyLabelMap.setPolicyId(xPolicy.getId()); - xxPolicyLabelMap.setPolicyLabelId(xxPolicyLabel.getId()); xxPolicyLabelMap = - rangerAuditFields.populateAuditFieldsForCreate(xxPolicyLabelMap); - xxPolicyLabelMap = daoMgr.getXXPolicyLabelMap().create(xxPolicyLabelMap); + xxPolicyLabelMap.setPolicyLabelId(xxPolicyLabel.getId()); + xxPolicyLabelMap = rangerAuditFields.populateAuditFieldsForCreate(xxPolicyLabelMap); + daoMgr.getXXPolicyLabelMap().create(xxPolicyLabelMap); + } + if (LOG.isDebugEnabled()) { + LOG.debug("<== AssociatePolicyLabel.getOrCreateLabel(policyId=" + xPolicy.getId() + ", label=" + policyLabel + ")"); } - } - - if (LOG.isDebugEnabled()) { - LOG.debug("<== ServiceDBStore.createOrMapLabels()"); } } diff --git a/security-admin/src/main/java/org/apache/ranger/service/RangerPolicyLabelHelper.java b/security-admin/src/main/java/org/apache/ranger/service/RangerPolicyLabelHelper.java deleted file mode 100644 index 2e17092..0000000 --- a/security-admin/src/main/java/org/apache/ranger/service/RangerPolicyLabelHelper.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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.ranger.service; - -import org.apache.commons.lang.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.apache.ranger.biz.ServiceDBStore; -import org.apache.ranger.db.RangerDaoManager; -import org.apache.ranger.entity.XXPolicy; -import org.apache.ranger.entity.XXPolicyLabel; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Scope; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Propagation; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Scope("singleton") -public class RangerPolicyLabelHelper { - private static final Log LOG = LogFactory.getLog(ServiceDBStore.class); - - @Autowired - protected RangerDaoManager daoMgr; - - @Autowired - RangerAuditFields<?> rangerAuditFields; - - @Transactional(propagation = Propagation.REQUIRES_NEW) - public XXPolicyLabel createNewOrGetLabel(String policyLabel, XXPolicy xPolicy) { - - if (LOG.isDebugEnabled()) { - LOG.debug("==> RangerPolicyLabelHelper.createNewOrGetLabel()"); - } - - XXPolicyLabel xxPolicyLabel = daoMgr.getXXPolicyLabels().findByName(policyLabel); - if (xxPolicyLabel == null) { - xxPolicyLabel = new XXPolicyLabel(); - if (StringUtils.isNotEmpty(policyLabel)) { - xxPolicyLabel.setPolicyLabel(policyLabel); - xxPolicyLabel = rangerAuditFields.populateAuditFieldsForCreate(xxPolicyLabel); - xxPolicyLabel = daoMgr.getXXPolicyLabels().create(xxPolicyLabel); - } - } - - if (LOG.isDebugEnabled()) { - LOG.debug("<== RangerPolicyLabelHelper.createNewOrGetLabel(), xxPolicyLabel = " + xxPolicyLabel); - } - - return xxPolicyLabel; - } - -}