Skip to content

Commit

Permalink
Merge pull request #4079 from dseurotech/ref-simplify_service_configu…
Browse files Browse the repository at this point in the history
…ration

♻️ [Service Configuration] Service Configurations refactoring
  • Loading branch information
Coduz authored Feb 3, 2025
2 parents 3c1f442 + 94d5341 commit 1724c60
Show file tree
Hide file tree
Showing 129 changed files with 3,494 additions and 5,035 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
*******************************************************************************/
package org.eclipse.kapua.commons.configuration;

import java.util.Collections;
import java.util.Map;
import java.util.Optional;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.model.config.metatype.EmptyTocd;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.id.KapuaId;
Expand All @@ -29,10 +31,10 @@
/**
* Base {@link KapuaConfigurableService} implementation, build upon {@link ServiceConfigurationManager}.
* <p>
* Note: at first glance, this might seems like a violation of Composition over Inheritance principle, however:
* - in this case inheritance is an acceptable strategy due to the strong link between {@link ServiceConfigurationManager#isServiceEnabled(org.eclipse.kapua.storage.TxContext, KapuaId)}
* and {@link org.eclipse.kapua.service.KapuaService#isServiceEnabled(KapuaId)} (the latter being dependent from the first for configurable services).
* - this class is nothing more than glue and convenience, demanding all of its logic to the {@link ServiceConfigurationManager}'s instance provided, so no flexibility has been sacrificed
* Note: at first glance, this might seems like a violation of Composition over Inheritance principle, however: - in this case inheritance is an acceptable strategy due to the strong link between
* {@link ServiceConfigurationManager#isServiceEnabled(org.eclipse.kapua.storage.TxContext, KapuaId)} and {@link org.eclipse.kapua.service.KapuaService#isServiceEnabled(KapuaId)} (the latter being
* dependent from the first for configurable services). - this class is nothing more than glue and convenience, demanding all of its logic to the {@link ServiceConfigurationManager}'s instance
* provided, so no flexibility has been sacrificed
*
* @since 2.0.0
*/
Expand Down Expand Up @@ -73,8 +75,11 @@ public KapuaTocd getConfigMetadata(KapuaId scopeId) throws KapuaException {
ArgumentValidator.notNull(scopeId, "scopeId");

// Check access
authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId));
return txManager.execute(tx -> serviceConfigurationManager.getConfigMetadata(tx, scopeId, true));
if (!authorizationService.isPermitted(permissionFactory.newPermission(domain, Actions.read, scopeId))) {
//Temporary, use Optional instead
return new EmptyTocd();
}
return serviceConfigurationManager.getConfigMetadata(scopeId, true).orElse(null);
}

@Override
Expand All @@ -83,9 +88,10 @@ public Map<String, Object> getConfigValues(KapuaId scopeId) throws KapuaExceptio
ArgumentValidator.notNull(scopeId, "scopeId");

// Check access
authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.read, scopeId));

return txManager.execute(tx -> serviceConfigurationManager.getConfigValues(tx, scopeId, true));
if (!authorizationService.isPermitted(permissionFactory.newPermission(domain, Actions.read, scopeId))) {
return Collections.emptyMap();
}
return serviceConfigurationManager.getConfigValues(scopeId, true);
}

@Override
Expand All @@ -96,9 +102,6 @@ public void setConfigValues(KapuaId scopeId, KapuaId parentId, Map<String, Objec

authorizationService.checkPermission(permissionFactory.newPermission(domain, Actions.write, scopeId));

txManager.<Void>execute(tx -> {
serviceConfigurationManager.setConfigValues(tx, scopeId, Optional.ofNullable(parentId), values);
return null;
});
serviceConfigurationManager.setConfigValues(scopeId, Optional.ofNullable(parentId), values);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*******************************************************************************
* Copyright (c) 2016, 2022 Eurotech and/or its affiliates and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.commons.configuration;

import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Optional;

import org.eclipse.kapua.commons.util.ResourceUtils;
import org.eclipse.kapua.commons.util.xml.XmlUtil;
import org.eclipse.kapua.model.config.metatype.KapuaTmetadata;
import org.eclipse.kapua.storage.TxContext;

public class ResourceBasedServiceConfigurationMetadataProvider implements ServiceConfigurationMetadataProvider {

private final XmlUtil xmlUtil;

public ResourceBasedServiceConfigurationMetadataProvider(XmlUtil xmlUtil) {
this.xmlUtil = xmlUtil;
}

@Override
public Optional<KapuaTmetadata> fetchMetadata(TxContext txContext, String pid) {
URL url = ResourceUtils.getResource(String.format("META-INF/metatypes/%s.xml", pid));

if (url == null) {
return Optional.empty();
}

try {
return Optional.ofNullable(xmlUtil.unmarshal(ResourceUtils.openAsReader(url, StandardCharsets.UTF_8), KapuaTmetadata.class))
.filter(v -> v.getOCD() != null && !v.getOCD().isEmpty());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import org.eclipse.kapua.commons.configuration.exception.ServiceConfigurationLimitExceededException;
import org.eclipse.kapua.commons.configuration.exception.ServiceConfigurationParentLimitExceededException;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.util.xml.XmlUtil;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.account.Account;
import org.eclipse.kapua.service.account.AccountListResult;
import org.eclipse.kapua.service.config.KapuaConfigurableService;
import org.eclipse.kapua.storage.TxContext;
import org.eclipse.kapua.storage.TxManager;

public class ResourceLimitedServiceConfigurationManagerImpl
extends ServiceConfigurationManagerImpl
Expand All @@ -38,12 +38,14 @@ public class ResourceLimitedServiceConfigurationManagerImpl

public ResourceLimitedServiceConfigurationManagerImpl(
String pid,
String domain,
TxManager txManager,
ServiceConfigRepository serviceConfigRepository,
RootUserTester rootUserTester,
AccountRelativeFinder accountRelativeFinder,
UsedEntitiesCounter usedEntitiesCounter,
XmlUtil xmlUtil) {
super(pid, serviceConfigRepository, rootUserTester, xmlUtil);
ServiceConfigurationMetadataProvider serviceConfigurationMetadataProvider) {
super(pid, domain, txManager, serviceConfigRepository, rootUserTester, serviceConfigurationMetadataProvider);
this.accountRelativeFinder = accountRelativeFinder;
this.usedEntitiesCounter = usedEntitiesCounter;
}
Expand Down Expand Up @@ -134,7 +136,7 @@ private long allowedChildEntities(TxContext txContext, KapuaId scopeId, Optional
if (configuration.isPresent()) { // Checked exceptions be damned, could have been .orElseGet(()->...)
finalConfig = configuration.get();
} else {
finalConfig = getConfigValues(txContext, scopeId, false);
finalConfig = doGetConfigValues(txContext, scopeId, false);
}
boolean allowInfiniteChildEntities = (boolean) finalConfig.getOrDefault("infiniteChildEntities", false);
if (allowInfiniteChildEntities) {
Expand All @@ -148,7 +150,7 @@ private long allowedChildEntities(TxContext txContext, KapuaId scopeId, Optional
// Resources assigned to children
long childCount = 0;
for (Account childAccount : childAccounts.getItems()) {
Map<String, Object> childConfigValues = getConfigValues(txContext, childAccount.getId(), true);
Map<String, Object> childConfigValues = doGetConfigValues(txContext, childAccount.getId(), true);
// maxNumberChildEntities can be null if such property is disabled via the
// isPropertyEnabled() method in the service implementation. In such case,
// it makes sense to treat the service as it had 0 available entities
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,41 @@
*******************************************************************************/
package org.eclipse.kapua.commons.configuration;

import java.util.Map;
import java.util.Optional;

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.service.KapuaService;
import org.eclipse.kapua.service.config.ServiceComponentConfiguration;
import org.eclipse.kapua.storage.TxContext;

import java.util.Map;
import java.util.Optional;

public interface ServiceConfigurationManager {

/**
* Whether this {@link KapuaService} is enabled for the given scope {@link KapuaId}.
*
* @param scopeId The scope {@link KapuaId} for which to check.
* @param scopeId
* The scope {@link KapuaId} for which to check.
* @return {@code true} if the {@link KapuaService} is enabled, {@code false} otherwise.
* @since 1.2.0
*/
default boolean isServiceEnabled(TxContext txContext, KapuaId scopeId) {
return true;
}

String getDomain();

void checkAllowedEntities(TxContext txContext, KapuaId scopeId, String entityType) throws KapuaException;

void setConfigValues(TxContext txContext, KapuaId scopeId, Optional<KapuaId> parentId, Map<String, Object> values) throws KapuaException;
void setConfigValues(KapuaId scopeId, Optional<KapuaId> parentId, Map<String, Object> values) throws KapuaException;

Map<String, Object> getConfigValues(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException;

KapuaTocd getConfigMetadata(TxContext txContext, KapuaId scopeId, boolean excludeDisabled) throws KapuaException;
Map<String, Object> getConfigValues(KapuaId scopeId, boolean excludeDisabled) throws KapuaException;

Optional<KapuaTocd> getConfigMetadata(KapuaId scopeId, boolean excludeDisabled) throws KapuaException;

Optional<ServiceComponentConfiguration> extractServiceComponentConfiguration(KapuaId scopeId) throws KapuaException;
}
Loading

0 comments on commit 1724c60

Please sign in to comment.