Skip to content

Commit

Permalink
♻️ isolating TMetadata retrieval logic for ServiceConfigurations in i…
Browse files Browse the repository at this point in the history
…ts own class

Signed-off-by: dseurotech <[email protected]>
  • Loading branch information
dseurotech committed Aug 1, 2024
1 parent 7ece681 commit 68817c4
Show file tree
Hide file tree
Showing 19 changed files with 126 additions and 59 deletions.
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,7 +21,6 @@
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;
Expand All @@ -45,8 +44,8 @@ public ResourceLimitedServiceConfigurationManagerImpl(
RootUserTester rootUserTester,
AccountRelativeFinder accountRelativeFinder,
UsedEntitiesCounter usedEntitiesCounter,
XmlUtil xmlUtil) {
super(pid, domain, txManager, serviceConfigRepository, rootUserTester, xmlUtil);
ServiceConfigurationMetadataProvider serviceConfigurationMetadataProvider) {
super(pid, domain, txManager, serviceConfigRepository, rootUserTester, serviceConfigurationMetadataProvider);
this.accountRelativeFinder = accountRelativeFinder;
this.usedEntitiesCounter = usedEntitiesCounter;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
*******************************************************************************/
package org.eclipse.kapua.commons.configuration;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
Expand All @@ -25,7 +22,6 @@
import java.util.stream.Collectors;

import javax.validation.constraints.NotNull;
import javax.xml.bind.JAXBException;
import javax.xml.namespace.QName;

import org.eclipse.kapua.KapuaEntityNotFoundException;
Expand All @@ -34,9 +30,7 @@
import org.eclipse.kapua.KapuaIllegalNullArgumentException;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.commons.util.ResourceUtils;
import org.eclipse.kapua.commons.util.StringUtil;
import org.eclipse.kapua.commons.util.xml.XmlUtil;
import org.eclipse.kapua.model.KapuaEntityAttributes;
import org.eclipse.kapua.model.config.metatype.KapuaTad;
import org.eclipse.kapua.model.config.metatype.KapuaTmetadata;
Expand All @@ -46,7 +40,6 @@
import org.eclipse.kapua.service.config.ServiceComponentConfiguration;
import org.eclipse.kapua.storage.TxContext;
import org.eclipse.kapua.storage.TxManager;
import org.xml.sax.SAXException;

public class ServiceConfigurationManagerImpl implements ServiceConfigurationManager {

Expand All @@ -55,20 +48,20 @@ public class ServiceConfigurationManagerImpl implements ServiceConfigurationMana
private final String domain;
private final ServiceConfigRepository serviceConfigRepository;
private final RootUserTester rootUserTester;
private final XmlUtil xmlUtil;
private final ServiceConfigurationMetadataProvider serviceConfigurationMetadataProvider;

public ServiceConfigurationManagerImpl(
String pid,
String domain, TxManager txManager,
ServiceConfigRepository serviceConfigRepository,
RootUserTester rootUserTester,
XmlUtil xmlUtil) {
ServiceConfigurationMetadataProvider serviceConfigurationMetadataProvider) {
this.pid = pid;
this.txManager = txManager;
this.domain = domain;
this.serviceConfigRepository = serviceConfigRepository;
this.rootUserTester = rootUserTester;
this.xmlUtil = xmlUtil;
this.serviceConfigurationMetadataProvider = serviceConfigurationMetadataProvider;
}

/**
Expand Down Expand Up @@ -443,7 +436,7 @@ protected Optional<KapuaTocd> doGetConfigMetadata(TxContext txContext, KapuaId s
}
// Get the Tocd
try {
return processMetadata(txContext, readMetadata(pid), scopeId, excludeDisabled);
return processMetadata(txContext, serviceConfigurationMetadataProvider.fetchMetadata(txContext, pid), scopeId, excludeDisabled);
} catch (Exception e) {
throw KapuaException.internalError(e);
}
Expand Down Expand Up @@ -476,36 +469,15 @@ public Optional<ServiceComponentConfiguration> extractServiceComponentConfigurat
* @return The processed {@link KapuaTocd}.
* @since 1.3.0
*/
private Optional<KapuaTocd> processMetadata(TxContext txContext, KapuaTmetadata metadata, KapuaId scopeId, boolean excludeDisabled) {
if (metadata != null && metadata.getOCD() != null && !metadata.getOCD().isEmpty()) {
for (KapuaTocd ocd : metadata.getOCD()) {
private Optional<KapuaTocd> processMetadata(TxContext txContext, Optional<KapuaTmetadata> metadata, KapuaId scopeId, boolean excludeDisabled) {
return metadata.flatMap(meta -> {
for (KapuaTocd ocd : meta.getOCD()) {
if (ocd.getId() != null && ocd.getId().equals(pid) && isServiceEnabled(txContext, scopeId)) {
ocd.getAD().removeIf(ad -> excludeDisabled && !isPropertyEnabled(ad, scopeId));
return Optional.of(ocd);
}
}
}
return Optional.empty();
}

/**
* Reads the {@link KapuaTmetadata} for the given {@link KapuaConfigurableService} pid.
*
* @param pid
* The {@link KapuaConfigurableService} pid
* @return The {@link KapuaTmetadata} for the given {@link KapuaConfigurableService} pid.
* @throws Exception
* @since 1.0.0
*/
//TODO: this must be moved in its own provider, as the logic of "where the metadata comes from" does not belong here
private KapuaTmetadata readMetadata(String pid) throws JAXBException, SAXException, IOException {
URL url = ResourceUtils.getResource(String.format("META-INF/metatypes/%s.xml", pid));

if (url == null) {
return null;
}

return xmlUtil.unmarshal(ResourceUtils.openAsReader(url, StandardCharsets.UTF_8), KapuaTmetadata.class);
return Optional.empty();
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*******************************************************************************
* 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.util.Optional;

import org.eclipse.kapua.model.config.metatype.KapuaTmetadata;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.service.config.KapuaConfigurableService;
import org.eclipse.kapua.storage.TxContext;

public interface ServiceConfigurationMetadataProvider {

/**
* Reads the {@link KapuaTmetadata} for the given {@link KapuaConfigurableService} pid. If no metadata can be found, just return Optional.empty() Same if the metadata is empty (containing no
* {@link KapuaTocd}s)
*
* @param txContext
* the transaction context (can be ignored if the data source is not transactional)
* @param pid
* The {@link KapuaConfigurableService} pid
* @return The {@link KapuaTmetadata} for the given {@link KapuaConfigurableService} pid.
* @since 1.0.0
*/
Optional<KapuaTmetadata> fetchMetadata(TxContext txContext, String pid);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.kapua.commons.configuration.AccountRelativeFinder;
import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ResourceBasedServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.configuration.ResourceLimitedServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
Expand Down Expand Up @@ -75,7 +76,7 @@ ServiceConfigurationManager accountServiceConfigurationManager(
new UsedEntitiesCounterImpl(
factory,
accountRepository),
xmlUtil
new ResourceBasedServiceConfigurationMetadataProvider(xmlUtil)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.configuration.AccountRelativeFinder;
import org.eclipse.kapua.commons.configuration.ResourceBasedServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.configuration.ResourceLimitedServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
Expand Down Expand Up @@ -136,7 +137,7 @@ protected void configure() {
new UsedEntitiesCounterImpl(
accountFactory,
accountRepository),
new XmlUtil(new TestJAXBContextProvider())
new ResourceBasedServiceConfigurationMetadataProvider(new XmlUtil(new TestJAXBContextProvider()))
),
new EventStorerImpl(new EventStoreRecordImplJpaRepository(jpaRepoConfig)),
accountMapper));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.inject.Singleton;

import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ResourceBasedServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationManager;
Expand Down Expand Up @@ -68,7 +69,7 @@ ServiceConfigurationManager messageStoreServiceConfigurationManager(
entityCacheFactory.createCache("AbstractKapuaConfigurableServiceCacheId")
),
rootUserTester,
xmlUtil
new ResourceBasedServiceConfigurationMetadataProvider(xmlUtil)
) {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import javax.inject.Singleton;

import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ResourceBasedServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationManager;
Expand Down Expand Up @@ -68,7 +69,7 @@ ServiceConfigurationManager deviceConnectionServiceConfigurationManager(
rootUserTester,
availableDeviceConnectionAdapters,
kapuaDeviceRegistrySettings,
xmlUtil)
new ResourceBasedServiceConfigurationMetadataProvider(xmlUtil))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.eclipse.kapua.commons.configuration.AccountRelativeFinder;
import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ResourceBasedServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.configuration.ResourceLimitedServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
Expand Down Expand Up @@ -73,7 +74,7 @@ protected ServiceConfigurationManager deviceRegistryServiceConfigurationManager(
new UsedEntitiesCounterImpl(
factory,
deviceRepository),
xmlUtil
new ResourceBasedServiceConfigurationMetadataProvider(xmlUtil)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import org.eclipse.kapua.commons.configuration.ServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationManager;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.model.domains.Domains;
import org.eclipse.kapua.commons.util.xml.XmlUtil;
import org.eclipse.kapua.model.config.metatype.KapuaTad;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.model.config.metatype.KapuaToption;
Expand Down Expand Up @@ -64,8 +64,8 @@ public DeviceConnectionServiceConfigurationManager(
RootUserTester rootUserTester,
Map<String, DeviceConnectionCredentialAdapter> availableDeviceConnectionAdapters,
KapuaDeviceRegistrySettings kapuaDeviceRegistrySettings,
XmlUtil xmlUtil) {
super(DeviceConnectionService.class.getName(), Domains.DEVICE_CONNECTION, txManager, serviceConfigRepository, rootUserTester, xmlUtil);
ServiceConfigurationMetadataProvider serviceConfigurationMetadataProvider) {
super(DeviceConnectionService.class.getName(), Domains.DEVICE_CONNECTION, txManager, serviceConfigRepository, rootUserTester, serviceConfigurationMetadataProvider);

this.availableDeviceConnectionAdapters = availableDeviceConnectionAdapters;
this.deviceRegistrySettings = kapuaDeviceRegistrySettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import org.eclipse.kapua.commons.configuration.AccountRelativeFinder;
import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ResourceBasedServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.configuration.ResourceLimitedServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
Expand Down Expand Up @@ -78,7 +79,7 @@ public ServiceConfigurationManager jobServiceConfigurationManager(
factory,
jobRepository
),
xmlUtil));
new ResourceBasedServiceConfigurationMetadataProvider(xmlUtil)));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
import org.eclipse.kapua.commons.configuration.ServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationManager;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.model.domains.Domains;
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.authentication.credential.CredentialService;
Expand All @@ -44,13 +44,13 @@ public CredentialServiceConfigurationManagerImpl(
SystemPasswordLengthProvider systemPasswordLengthProvider,
RootUserTester rootUserTester,
KapuaAuthenticationSetting kapuaAuthenticationSetting,
XmlUtil xmlUtil) {
ServiceConfigurationMetadataProvider serviceConfigurationMetadataProvider) {
super(CredentialService.class.getName(),
Domains.CREDENTIAL,
txManager,
serviceConfigRepository,
rootUserTester,
xmlUtil);
serviceConfigurationMetadataProvider);
this.systemPasswordLengthProvider = systemPasswordLengthProvider;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import javax.inject.Singleton;

import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ResourceBasedServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
import org.eclipse.kapua.commons.configuration.ServiceConfigurationManager;
Expand Down Expand Up @@ -64,6 +65,6 @@ public ServiceConfigurationManager credentialServiceConfigurationManager(
systemPasswordLengthProvider,
rootUserTester,
kapuaAuthenticationSetting,
xmlUtil));
new ResourceBasedServiceConfigurationMetadataProvider(xmlUtil)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.eclipse.kapua.commons.configuration.AccountRelativeFinder;
import org.eclipse.kapua.commons.configuration.CachingServiceConfigRepository;
import org.eclipse.kapua.commons.configuration.ResourceBasedServiceConfigurationMetadataProvider;
import org.eclipse.kapua.commons.configuration.ResourceLimitedServiceConfigurationManagerImpl;
import org.eclipse.kapua.commons.configuration.RootUserTester;
import org.eclipse.kapua.commons.configuration.ServiceConfigImplJpaRepository;
Expand Down Expand Up @@ -75,6 +76,6 @@ public ServiceConfigurationManager groupServiceConfigurationManager(
new UsedEntitiesCounterImpl(
factory,
groupRepository
), xmlUtil));
), new ResourceBasedServiceConfigurationMetadataProvider(xmlUtil)));
}
}
Loading

0 comments on commit 68817c4

Please sign in to comment.