From a39d567e853e4ed07f931358750c8458af817408 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sat, 2 Feb 2019 18:20:11 +0530 Subject: [PATCH] fix License text loading inconsistency in tenants --- .../registry/RegistryBasedLicenseManager.java | 31 +++++++++++-------- .../DeviceTypeManagerServiceTest.java | 2 -- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/registry/RegistryBasedLicenseManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/registry/RegistryBasedLicenseManager.java index 0c5b6707ac..0d62134675 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/registry/RegistryBasedLicenseManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/license/mgt/registry/RegistryBasedLicenseManager.java @@ -44,17 +44,18 @@ import java.util.Locale; @SuppressWarnings("unused") public class RegistryBasedLicenseManager implements LicenseManager { - private GenericArtifactManager artifactManager; private static final Log log = LogFactory.getLog(RegistryBasedLicenseManager.class); - public RegistryBasedLicenseManager() { + public RegistryBasedLicenseManager() {} + + private GenericArtifactManager getArtifactManager() { Registry registry = CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_GOVERNANCE); if (registry == null) { throw new IllegalArgumentException("Registry instance retrieved is null. Hence, " + "'Registry based license manager cannot be initialized'"); } try { - this.artifactManager = GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(registry); + return GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(registry); } catch (LicenseManagementException e) { throw new IllegalStateException("Failed to initialize generic artifact manager bound to " + "Registry based license manager", e); @@ -63,14 +64,17 @@ public class RegistryBasedLicenseManager implements LicenseManager { @Override public License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException { + GenericArtifactManager artifactManager = getArtifactManager(); try { - GenericArtifact artifact = this.getGenericArtifact(deviceType, languageCode); - if (artifact == null) { - if (log.isDebugEnabled()) { - log.debug("Generic artifact is null for '" + deviceType + "' device type. Hence license does not " + - "have content"); - } - return null; + GenericArtifact artifact = this.getGenericArtifact(artifactManager, deviceType, languageCode); + if (artifact == null) { //Adding a default license + License license = new License(); + license.setName(deviceType); + license.setVersion("1.0.0"); + license.setLanguage("en_US"); + license.setText("This is license text"); + addLicense(deviceType, license); + return license; } return this.populateLicense(artifact); } catch (GovernanceException e) { @@ -104,8 +108,9 @@ public class RegistryBasedLicenseManager implements LicenseManager { @Override public void addLicense(final String deviceType, final License license) throws LicenseManagementException { + GenericArtifactManager artifactManager = getArtifactManager(); try { - GenericArtifact artifact = this.getGenericArtifact(deviceType, license.getLanguage()); + GenericArtifact artifact = this.getGenericArtifact(artifactManager, deviceType, license.getLanguage()); if(artifact != null) { artifact.setAttribute(DeviceManagementConstants.LicenseProperties.NAME, license.getName()); artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VERSION, license.getVersion()); @@ -147,8 +152,8 @@ public class RegistryBasedLicenseManager implements LicenseManager { } } - private GenericArtifact getGenericArtifact(final String deviceType, final String languageCode) - throws GovernanceException { + private GenericArtifact getGenericArtifact(GenericArtifactManager artifactManager, final String deviceType, final + String languageCode) throws GovernanceException { GenericArtifact[] artifacts = artifactManager.findGenericArtifacts(new GenericArtifactFilter() { @Override public boolean matches(GenericArtifact artifact) throws GovernanceException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java index e957bfd0db..efc36afe07 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/test/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManagerServiceTest.java @@ -300,8 +300,6 @@ public class DeviceTypeManagerServiceTest { License newLicense = arduinoDeviceTypeManagerService.getDeviceManager().getLicense("eu"); Assert.assertEquals(newLicense.getText(), license.getText(), "The retrieved license is different from added license"); - Assert.assertNull(arduinoDeviceTypeManagerService.getDeviceManager().getLicense("tn"), - "License is retrieved for a non-existing language code"); } /**