From 0135051d84c3fe044cb10b8030e9a13267564255 Mon Sep 17 00:00:00 2001 From: manoj Date: Fri, 23 Jan 2015 18:17:40 +0530 Subject: [PATCH] License Management --- .../org.wso2.carbon.device.mgt.core/pom.xml | 5 + .../device/mgt/core/DeviceManagerImpl.java | 3 + .../device/mgt/core/LicenseManager.java | 5 +- .../device/mgt/core/LicenseManagerImpl.java | 98 +++++++++++++++++-- .../LicenseManagementServiceComponent.java | 5 +- .../service/LicenseManagementService.java | 8 +- .../src/repository/resources/rxts/license.rxt | 15 ++- 7 files changed, 115 insertions(+), 24 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 8690a2e3fd..30cd0d32cc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -121,6 +121,11 @@ org.wso2.carbon org.wso2.carbon.user.api + + org.wso2.carbon + org.wso2.carbon.governance.api + 4.2.1 + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java index 726c43c343..8f8f24328b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagerImpl.java @@ -17,6 +17,8 @@ */ package org.wso2.carbon.device.mgt.core; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; @@ -35,6 +37,7 @@ import java.util.List; public class DeviceManagerImpl implements DeviceManager { + private static Log log = LogFactory.getLog(DeviceManagerImpl.class); private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; private DeviceManagementConfig config; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManager.java index dc13149fa4..35c846ac2e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManager.java @@ -21,10 +21,7 @@ package org.wso2.carbon.device.mgt.core; import org.wso2.carbon.device.mgt.common.*; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; public interface LicenseManager { - public License getLicense(DeviceManagementConstants.MobileDeviceTypes deviceType, - DeviceManagementConstants.LanguageCodes languageCodes) throws LicenseManagementException; - + public License getLicense(String deviceType,String languageCodes) throws LicenseManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagerImpl.java index aa55100774..d00936e3fa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagerImpl.java @@ -17,21 +17,103 @@ * under the License. * */ - package org.wso2.carbon.device.mgt.core; -import org.wso2.carbon.device.mgt.common.*; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.base.MultitenantConstants; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.context.RegistryType; +import org.wso2.carbon.device.mgt.common.License; +import org.wso2.carbon.device.mgt.common.LicenseManagementException; +import org.wso2.carbon.governance.api.exception.GovernanceException; +import org.wso2.carbon.governance.api.generic.GenericArtifactFilter; +import org.wso2.carbon.governance.api.generic.GenericArtifactManager; +import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact; +import org.wso2.carbon.governance.api.util.GovernanceUtils; +import org.wso2.carbon.registry.core.Registry; +import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.registry.core.session.UserRegistry; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; + + +public class LicenseManagerImpl implements LicenseManager { -public class LicenseManagerImpl implements LicenseManager{ + private static Log log = LogFactory.getLog(DeviceManagerImpl.class); @Override - public License getLicense(DeviceManagementConstants.MobileDeviceTypes deviceType, - DeviceManagementConstants.LanguageCodes languageCodes) throws LicenseManagementException { + public License getLicense(final String deviceType, + final String languageCodes) throws LicenseManagementException { + if (log.isDebugEnabled()){ + log.debug("entered get License in license manager impl"); + } + // TODO: After completes JAX-RX user login, this need to be change to CarbonContext + PrivilegedCarbonContext.getThreadLocalCarbonContext().startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername("admin"); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); - return null; - } + Registry registry = (UserRegistry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry( + RegistryType.USER_GOVERNANCE); + + GenericArtifact[] filteredArtifacts; + License license = new License(); + try { + GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry); + GenericArtifactManager artifactManager = new GenericArtifactManager(registry, "license"); + + filteredArtifacts = artifactManager.findGenericArtifacts( + new GenericArtifactFilter() { + public boolean matches(GenericArtifact artifact) throws GovernanceException { + String attributeNameVal = artifact.getAttribute("overview_name"); + String attributeLangVal = artifact.getAttribute("overview_language"); + return (attributeNameVal != null && attributeLangVal != null && attributeNameVal.equals + (deviceType) && attributeLangVal.equals(languageCodes)); + } + }); + String validFrom; + String validTo; + DateFormat format; + Date fromDate; + Date toDate; + + for (GenericArtifact artifact : filteredArtifacts) { + if (log.isDebugEnabled()){ + log.debug("Overview name:"+artifact.getAttribute("overview_name")); + log.debug("Overview provider:"+artifact.getAttribute("overview_provider")); + log.debug("Overview Language:"+artifact.getAttribute("overview_language")); + log.debug("overview_validityFrom:"+artifact.getAttribute("overview_validityFrom")); + log.debug("overview_validityTo:"+artifact.getAttribute("overview_validityTo")); + } + validFrom = artifact.getAttribute("overview_validityFrom"); + validTo = artifact.getAttribute("overview_validityTo"); + format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH); + try { + fromDate = format.parse(validFrom); + toDate = format.parse(validTo); + if (fromDate.getTime()<= new Date().getTime() && new Date().getTime() <= toDate.getTime()){ + license.setLicenseText(artifact.getAttribute("overview_license")); + } + } catch (ParseException e) { + log.error("validFrom:"+ validFrom); + log.error("validTo:"+validTo); + log.error("Valid date parse error:",e); + } + } + } catch (RegistryException regEx) { + log.error("registry exception:",regEx); + throw new LicenseManagementException(); + }finally { + PrivilegedCarbonContext.endTenantFlow(); + } + + return license; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementServiceComponent.java index 3be323e27c..b1316eff5c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementServiceComponent.java @@ -16,7 +16,6 @@ * under the License. * */ - package org.wso2.carbon.device.mgt.core.internal; import org.apache.commons.logging.Log; @@ -65,7 +64,7 @@ public class LicenseManagementServiceComponent { */ protected void setRealmService(RealmService realmService) { if (log.isDebugEnabled()) { - log.debug("Setting Realm Service"); + log.debug("Setting Realm Service in license management"); } LicenseManagementDataHolder.getInstance().setRealmService(realmService); } @@ -76,7 +75,7 @@ public class LicenseManagementServiceComponent { */ protected void unsetRealmService(RealmService realmService) { if (log.isDebugEnabled()) { - log.debug("Unsetting Realm Service"); + log.debug("Un setting Realm Service in license management"); } LicenseManagementDataHolder.getInstance().setRealmService(null); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/LicenseManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/LicenseManagementService.java index 3f383ba4ab..bccbcea3c7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/LicenseManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/LicenseManagementService.java @@ -20,19 +20,15 @@ package org.wso2.carbon.device.mgt.core.service; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.License; import org.wso2.carbon.device.mgt.common.LicenseManagementException; import org.wso2.carbon.device.mgt.core.LicenseManager; -import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.internal.LicenseManagementDataHolder; -public class LicenseManagementService implements LicenseManager{ +public class LicenseManagementService implements LicenseManager { @Override - public License getLicense(DeviceManagementConstants.MobileDeviceTypes deviceType, - DeviceManagementConstants.LanguageCodes languageCode) throws LicenseManagementException { - + public License getLicense(String deviceType, String languageCode) throws LicenseManagementException { return LicenseManagementDataHolder.getInstance().getLicenseManager().getLicense(deviceType, languageCode); } } diff --git a/product/modules/distribution/src/repository/resources/rxts/license.rxt b/product/modules/distribution/src/repository/resources/rxts/license.rxt index 72f5c2618a..2fc84c6db9 100644 --- a/product/modules/distribution/src/repository/resources/rxts/license.rxt +++ b/product/modules/distribution/src/repository/resources/rxts/license.rxt @@ -1,16 +1,19 @@ - /license/@{overview_provider}/@{overview_name}/@{overview_version} + /license/@{overview_provider}/@{overview_name}/@{overview_language}/@{overview_version} overview_name - + + + + @@ -24,11 +27,17 @@ Name + + Language + Version - Createdtime + Validity From + + + Validity To License