From 916760275f2ed28ba96bcb8955e11cda9db39d1e Mon Sep 17 00:00:00 2001 From: prabathabey Date: Thu, 19 Feb 2015 17:24:50 +0530 Subject: [PATCH] Refactoring license management implementation --- .../mgt/common/DeviceManagementConstants.java | 10 +- .../org.wso2.carbon.device.mgt.core/pom.xml | 12 +- .../device/mgt/core/LicenseManagerImpl.java | 111 -------------- .../license/LicenseConfigurationManager.java | 2 +- .../internal/DeviceManagementDataHolder.java | 20 +++ .../DeviceManagementServiceComponent.java | 62 +++++++- ...eManagementAxis2ConfigurationObserver.java | 55 ------- .../internal/LicenseManagementDataHolder.java | 74 --------- .../LicenseManagementServiceComponent.java | 140 ------------------ .../mgt/GenericArtifactManagerFactory.java | 7 +- .../mgt}/LicenseManagementException.java | 23 ++- .../mgt}/LicenseManagementService.java | 16 +- .../license/mgt/LicenseManagementUtil.java | 25 ++++ .../{ => license/mgt}/LicenseManager.java | 8 +- .../core/license/mgt/LicenseManagerImpl.java | 108 ++++++++++++++ .../license/mgt/LicenseNotFoundException.java | 57 +++++++ .../mgt/core/util/LicenseManagerUtil.java | 86 ----------- 17 files changed, 307 insertions(+), 509 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagerImpl.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementAxis2ConfigurationObserver.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementDataHolder.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementServiceComponent.java rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/{ => license/mgt}/LicenseManagementException.java (84%) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/{service => license/mgt}/LicenseManagementService.java (68%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementUtil.java rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/{ => license/mgt}/LicenseManager.java (77%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseNotFoundException.java delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/LicenseManagerUtil.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java index 494bc781c4d..23ff9d0c3ac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java @@ -61,13 +61,13 @@ public final class DeviceManagementConstants { private LicenseProperties() { throw new AssertionError(); } - public static final String OVERVIEW_PROVIDER = "overview_provider"; - public static final String OVERVIEW_NAME = "overview_name"; - public static final String OVERVIEW_LANGUAGE = "overview_language"; - public static final String OVERVIEW_VERSION = "overview_version"; + public static final String PROVIDER = "overview_provider"; + public static final String NAME = "overview_name"; + public static final String LANGUAGE = "overview_language"; + public static final String VERSION = "overview_version"; public static final String VALID_FROM = "overview_validityFrom"; public static final String VALID_TO = "overview_validityTo"; - public static final String LICENSE = "overview_license"; + public static final String TEXT = "overview_license"; public static final String LICENSE_REGISTRY_KEY = "license"; } 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 a195bad20d7..4b532132c9b 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 @@ -59,17 +59,21 @@ org.apache.commons.logging, javax.naming, javax.xml.*, - javax.sql, - org.wso2.carbon.governance.api.*, - org.wso2.carbon.base, + javax.sql.*, org.wso2.carbon.context, org.wso2.carbon.core, org.wso2.carbon.utils.*, org.wso2.carbon.registry.api, - org.wso2.carbon.registry.core.*, org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.user.api, org.wso2.carbon.user.core.*, + org.wso2.carbon.governance.api.exception, + org.wso2.carbon.governance.api.generic, + org.wso2.carbon.governance.api.generic.dataobjects, + org.wso2.carbon.registry.core, + org.wso2.carbon.registry.core.exceptions, + org.wso2.carbon.registry.core.service, + org.wso2.carbon.registry.core.session, org.w3c.dom 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 deleted file mode 100644 index 857682e1685..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagerImpl.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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.wso2.carbon.device.mgt.core; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.core.config.license.License; -import org.wso2.carbon.device.mgt.core.license.mgt.GenericArtifactManagerFactory; -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.registry.api.RegistryException; - -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 { - - private static Log log = LogFactory.getLog(DeviceManagerImpl.class); - private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH); - - @Override - public License getLicense(final String deviceType, final String languageCodes) throws LicenseManagementException { - - if (log.isDebugEnabled()) { - log.debug("Retrieving the license configured upon Device Type: '" + deviceType + "' and Language Code: '" + - languageCodes + "'"); - } - - License license = null; - GenericArtifact[] filteredArtifacts; - try { - GenericArtifactManager artifactManager = - GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(); - - filteredArtifacts = artifactManager.findGenericArtifacts( - new GenericArtifactFilter() { - public boolean matches(GenericArtifact artifact) throws GovernanceException { - String attributeNameVal = artifact.getAttribute( - DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME); - String attributeLangVal = artifact.getAttribute( - DeviceManagementConstants.LicenseProperties.OVERVIEW_LANGUAGE); - return (attributeNameVal != null && attributeLangVal != null && attributeNameVal.equals - (deviceType) && attributeLangVal.equals(languageCodes)); - } - }); - - String validFrom; - String validTo; - Date fromDate; - Date toDate; - - for (GenericArtifact artifact : filteredArtifacts) { - if (log.isDebugEnabled()) { - log.debug("Overview name: " + - artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME)); - log.debug("Overview provider: " + - artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_PROVIDER)); - log.debug("Overview language: " + - artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_LANGUAGE)); - log.debug("Overview validity from: " + - artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM)); - log.debug("Overview validity to: " + - artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO)); - } - validFrom = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM); - validTo = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO); - - fromDate = format.parse(validFrom); - toDate = format.parse(validTo); - - license = new License(); - if (fromDate.getTime() <= new Date().getTime() && new Date().getTime() <= toDate.getTime()) { - license.setText(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.LICENSE)); - } - } - } catch (RegistryException e) { - String msg = "Error occurred while initializing generic artifact manager associated with retrieving " + - "license data stored in registry"; - throw new LicenseManagementException(msg, e); - } catch (ParseException e) { - String msg = "Error occurred while parsing the date string"; - log.error(msg, e); - throw new LicenseManagementException(msg, e); - } - return license; - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java index c0137c7fe9c..43782e75185 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java @@ -21,8 +21,8 @@ package org.wso2.carbon.device.mgt.core.config.license; import org.w3c.dom.Document; -import org.wso2.carbon.device.mgt.core.LicenseManagementException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.utils.CarbonUtils; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index 8c8bf62bcaa..8c5f11d325e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -20,6 +20,8 @@ package org.wso2.carbon.device.mgt.core.internal; import org.wso2.carbon.device.mgt.core.DeviceManager; +import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager; +import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; @@ -28,6 +30,8 @@ public class DeviceManagementDataHolder { private RealmService realmService; private TenantManager tenantManager; private DeviceManager deviceManager; + private LicenseManager licenseManager; + private RegistryService registryService; private static DeviceManagementDataHolder thisInstance = new DeviceManagementDataHolder(); private DeviceManagementDataHolder() { @@ -65,4 +69,20 @@ public class DeviceManagementDataHolder { this.deviceManager = deviceManager; } + public RegistryService getRegistryService() { + return registryService; + } + + public void setRegistryService(RegistryService registryService) { + this.registryService = registryService; + } + + public LicenseManager getLicenseManager() { + return licenseManager; + } + + public void setLicenseManager(LicenseManager licenseManager) { + this.licenseManager = licenseManager; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 443239ad117..99f96cf7cfa 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -30,10 +30,17 @@ import org.wso2.carbon.device.mgt.core.DeviceManagerImpl; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; +import org.wso2.carbon.device.mgt.core.config.license.License; +import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; +import org.wso2.carbon.device.mgt.core.config.license.LicenseConfigurationManager; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagementService; +import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.core.license.mgt.LicenseManagerImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; -import org.wso2.carbon.device.mgt.core.service.LicenseManagementService; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; +import org.wso2.carbon.registry.core.service.RegistryService; import org.wso2.carbon.user.core.service.RealmService; /** @@ -50,6 +57,12 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setDeviceManagerService" * unbind="unsetDeviceManagerService" + * @scr.reference name="registry.service" + * interface="org.wso2.carbon.registry.core.service.RegistryService" + * cardinality="1..1" + * policy="dynamic" + * bind="setRegistryService" + * unbind="unsetRegistryService" */ public class DeviceManagementServiceComponent { @@ -72,6 +85,12 @@ public class DeviceManagementServiceComponent { DeviceManager deviceManager = new DeviceManagerImpl(config, this.getPluginRepository()); DeviceManagementDataHolder.getInstance().setDeviceManager(deviceManager); + LicenseConfigurationManager.getInstance().initConfig(); + LicenseConfig licenseConfig = LicenseConfigurationManager.getInstance().getLicenseConfig(); + + LicenseManager licenseManager = new LicenseManagerImpl(); + DeviceManagementDataHolder.getInstance().setLicenseManager(licenseManager); + /* If -Dsetup option enabled then create device management database schema */ String setupOption = System.getProperty(DeviceManagementConstants.Common.PROPERTY_SETUP); @@ -82,14 +101,18 @@ public class DeviceManagementServiceComponent { "is about to begin"); } this.setupDeviceManagementSchema(dsConfig); + this.setupDefaultLicenses(licenseConfig); } if (log.isDebugEnabled()) { log.debug("Registering OSGi service DeviceManagementService"); } + /* Registering Device Management service */ BundleContext bundleContext = componentContext.getBundleContext(); bundleContext.registerService(DeviceManagementService.class.getName(), new DeviceManagementService(), null); - bundleContext.registerService(LicenseManagementService.class.getName(), new LicenseManagementService(), null); + /* Registering License Management service */ + bundleContext.registerService(LicenseManagementService.class.getName(), + new LicenseManagementService(), null); if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } @@ -118,6 +141,17 @@ public class DeviceManagementServiceComponent { } } + private void setupDefaultLicenses(LicenseConfig licenseConfig) throws LicenseManagementException { + LicenseManager licenseManager = DeviceManagementDataHolder.getInstance().getLicenseManager(); + for (License license : licenseConfig.getLicenses()) { + License extLicense = licenseManager.getLicense(license.getName(), license.getLanguage()); + if (extLicense != null) { + continue; + } + licenseManager.addLicense(license); + } + } + /** * Sets Device Manager service. * @@ -168,6 +202,30 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setRealmService(null); } + /** + * Sets Registry Service. + * + * @param registryService An instance of RegistryService + */ + protected void setRegistryService(RegistryService registryService) { + if (log.isDebugEnabled()) { + log.debug("Setting Registry Service"); + } + DeviceManagementDataHolder.getInstance().setRegistryService(registryService); + } + + /** + * Unsets Registry Service. + * + * @param registryService An instance of RegistryService + */ + protected void unsetRegistryService(RegistryService registryService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting Registry Service"); + } + DeviceManagementDataHolder.getInstance().setRegistryService(null); + } + private DeviceManagementRepository getPluginRepository() { return pluginRepository; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementAxis2ConfigurationObserver.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementAxis2ConfigurationObserver.java deleted file mode 100644 index 350a1abb29a..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementAxis2ConfigurationObserver.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * * - * * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * * - * * Licensed 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.wso2.carbon.device.mgt.core.internal; - -import org.apache.axis2.context.ConfigurationContext; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.core.LicenseManagementException; -import org.wso2.carbon.device.mgt.core.util.LicenseManagerUtil; -import org.wso2.carbon.utils.Axis2ConfigurationContextObserver; - -public class LicenseManagementAxis2ConfigurationObserver implements Axis2ConfigurationContextObserver { - - private static final Log log = LogFactory.getLog(LicenseManagementAxis2ConfigurationObserver.class); - - @Override - public void creatingConfigurationContext(int tenantId) { - - } - - @Override - public void createdConfigurationContext(ConfigurationContext configurationContext) { - try { - LicenseManagerUtil.addDefaultLicenses(null); - } catch (LicenseManagementException e) { - log.error("Error occurred while adding default license configurations of the tenant '"); - } - } - - @Override - public void terminatingConfigurationContext(ConfigurationContext configurationContext) { - - } - - @Override - public void terminatedConfigurationContext(ConfigurationContext configurationContext) { - - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementDataHolder.java deleted file mode 100644 index b2ee635765e..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementDataHolder.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. 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.wso2.carbon.device.mgt.core.internal; - -import org.wso2.carbon.device.mgt.core.LicenseManager; -import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.user.core.service.RealmService; -import org.wso2.carbon.user.core.tenant.TenantManager; - -public class LicenseManagementDataHolder { - - private RealmService realmService; - private TenantManager tenantManager; - private LicenseManager licenseManager; - private RegistryService registryService; - - private static LicenseManagementDataHolder thisInstance = new LicenseManagementDataHolder(); - - public RealmService getRealmService() { - return realmService; - } - - public void setRealmService(RealmService realmService) { - this.realmService = realmService; - } - - public TenantManager getTenantManager() { - return this.tenantManager; - } - - public void setTenantManager(TenantManager tenantManager) { - this.tenantManager = tenantManager; - } - - private LicenseManagementDataHolder() { - } - - public static LicenseManagementDataHolder getInstance() { - return thisInstance; - } - public LicenseManager getLicenseManager() { - return this.licenseManager; - } - - public void setLicenseManager(LicenseManager licenseManager) { - this.licenseManager = licenseManager; - } - - public void setRegistryService(RegistryService registryService) { - this.registryService = registryService; - } - - public RegistryService getRegistryService() { - return this.registryService; - } - -} 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 deleted file mode 100644 index 1b03ab20f67..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementServiceComponent.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * WSO2 Inc. 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.wso2.carbon.device.mgt.core.internal; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.osgi.framework.BundleContext; -import org.osgi.service.component.ComponentContext; -import org.wso2.carbon.base.MultitenantConstants; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.core.LicenseManager; -import org.wso2.carbon.device.mgt.core.LicenseManagerImpl; -import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; -import org.wso2.carbon.device.mgt.core.config.license.LicenseConfigurationManager; -import org.wso2.carbon.device.mgt.core.service.LicenseManagementService; -import org.wso2.carbon.device.mgt.core.util.LicenseManagerUtil; -import org.wso2.carbon.registry.core.service.RegistryService; -import org.wso2.carbon.user.core.service.RealmService; - -public class LicenseManagementServiceComponent { - - private static Log log = LogFactory.getLog(LicenseManagementServiceComponent.class); - - protected void activate(ComponentContext componentContext) { - try { - if (log.isDebugEnabled()) { - log.debug("Initializing license management core bundle"); - } - LicenseManager licenseManager = new LicenseManagerImpl(); - LicenseManagementDataHolder.getInstance().setLicenseManager(licenseManager); - - if (log.isDebugEnabled()) { - log.debug("Configuring default licenses to be used for device enrolment"); - } - LicenseConfigurationManager.getInstance().initConfig(); - LicenseConfig licenseConfig = LicenseConfigurationManager.getInstance().getLicenseConfig(); - - /* If -Dsetup option enabled then configure device management related default licenses */ - String setupOption = - System.getProperty(DeviceManagementConstants.Common.PROPERTY_SETUP); - if (setupOption != null) { - if (log.isDebugEnabled()) { - log.debug( - "-Dsetup is enabled. Configuring default device management licenses " + - "is about to begin"); - } - /* It is required to specifically run the following code snippet as Super Tenant in order to use - * Super tenant registry to initialize the underlying GenericArtifactManager corresponding to the same */ - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain - (MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); - LicenseManagerUtil.addDefaultLicenses(licenseConfig); - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - } - - if (log.isDebugEnabled()) { - log.debug("Registering OSGi service 'LicenseManagementService'"); - } - BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext - .registerService(LicenseManagementService.class.getName(), new LicenseManagementService(), null); - if (log.isDebugEnabled()) { - log.debug("License management core bundle has been successfully initialized"); - } - } catch (Throwable e) { - String msg = "Error occurred while initializing license management core bundle"; - log.error(msg, e); - } - } - - /** - * Sets Realm Service. - * - * @param realmService An instance of RealmService - */ - protected void setRealmService(RealmService realmService) { - if (log.isDebugEnabled()) { - log.debug("Setting Realm Service in license management"); - } - LicenseManagementDataHolder.getInstance().setRealmService(realmService); - } - - /** - * Unsets Realm Service. - * - * @param realmService An instance of RealmService - */ - protected void unsetRealmService(RealmService realmService) { - if (log.isDebugEnabled()) { - log.debug("Un setting Realm Service in license management"); - } - LicenseManagementDataHolder.getInstance().setRealmService(null); - } - - /** - * Sets Registry Service. - * - * @param registryService An instance of RegistryService - */ - protected void setRegistryService(RegistryService registryService) { - if (log.isDebugEnabled()) { - log.debug("Setting Registry Service"); - } - LicenseManagementDataHolder.getInstance().setRegistryService(registryService); - } - - /** - * Unsets Registry Service. - * - * @param registryService An instance of RegistryService - */ - protected void unsetRegistryService(RegistryService registryService) { - if (log.isDebugEnabled()) { - log.debug("Unsetting Registry Service"); - } - LicenseManagementDataHolder.getInstance().setRegistryService(null); - } - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java index 810bedabd6f..6c132ca6b1d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/GenericArtifactManagerFactory.java @@ -20,13 +20,10 @@ package org.wso2.carbon.device.mgt.core.license.mgt; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.core.LicenseManagementException; -import org.wso2.carbon.device.mgt.core.internal.LicenseManagementDataHolder; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.governance.api.generic.GenericArtifactManager; -import org.wso2.carbon.governance.api.util.GovernanceUtils; import org.wso2.carbon.registry.api.Registry; import org.wso2.carbon.registry.core.exceptions.RegistryException; -import org.wso2.carbon.registry.core.session.UserRegistry; import java.util.HashMap; import java.util.Map; @@ -44,7 +41,7 @@ public class GenericArtifactManagerFactory { try { tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); registry = - LicenseManagementDataHolder.getInstance().getRegistryService().getGovernanceSystemRegistry( + DeviceManagementDataHolder.getInstance().getRegistryService().getGovernanceSystemRegistry( tenantId); } catch (RegistryException e) { throw new LicenseManagementException("Error occurred while initializing tenant system registry " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagementException.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementException.java similarity index 84% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagementException.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementException.java index 67937c11b63..7668f496823 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagementException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementException.java @@ -1,5 +1,4 @@ /* - * * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, @@ -17,16 +16,19 @@ * under the License. * */ +package org.wso2.carbon.device.mgt.core.license.mgt; -package org.wso2.carbon.device.mgt.core; - -public class LicenseManagementException extends Exception{ +public class LicenseManagementException extends Exception { - private static final long serialVersionUID = 8606378077242945475L; + private static final long serialVersionUID = -8933146283800122660L; private String errorMessage; - public static long getSerialVersionUID() { - return serialVersionUID; + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; } public LicenseManagementException(String msg, Exception nestedEx) { @@ -52,11 +54,4 @@ public class LicenseManagementException extends Exception{ super(cause); } - public String getErrorMessage() { - return errorMessage; - } - - public void setErrorMessage(String errorMessage) { - this.errorMessage = errorMessage; - } } 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/license/mgt/LicenseManagementService.java similarity index 68% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/LicenseManagementService.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementService.java index e4c10e109e8..83e996ec017 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/license/mgt/LicenseManagementService.java @@ -1,5 +1,4 @@ /* - * * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, @@ -17,19 +16,20 @@ * under the License. * */ -package org.wso2.carbon.device.mgt.core.service; +package org.wso2.carbon.device.mgt.core.license.mgt; -import org.wso2.carbon.device.mgt.core.LicenseManagementException; -import org.wso2.carbon.device.mgt.core.LicenseManager; import org.wso2.carbon.device.mgt.core.config.license.License; -import org.wso2.carbon.device.mgt.core.internal.LicenseManagementDataHolder; public class LicenseManagementService implements LicenseManager { @Override - public License getLicense(String deviceType, String languageCode) - throws LicenseManagementException { - return LicenseManagementDataHolder.getInstance().getLicenseManager().getLicense(deviceType, languageCode); + public License getLicense(String deviceType, String languageCode) throws LicenseManagementException { + return null; + } + + @Override + public void addLicense(License license) throws LicenseManagementException { + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementUtil.java new file mode 100644 index 00000000000..90dca5445af --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagementUtil.java @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.wso2.carbon.device.mgt.core.license.mgt; + +public class LicenseManagementUtil { + + + +} 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/license/mgt/LicenseManager.java similarity index 77% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManager.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManager.java index 6126f4a07a9..18dfa06d015 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/license/mgt/LicenseManager.java @@ -1,5 +1,4 @@ /* - * * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. * * WSO2 Inc. licenses this file to you under the Apache License, @@ -17,13 +16,14 @@ * under the License. * */ - -package org.wso2.carbon.device.mgt.core; +package org.wso2.carbon.device.mgt.core.license.mgt; import org.wso2.carbon.device.mgt.core.config.license.License; public interface LicenseManager { - License getLicense(String deviceType,String languageCodes) throws LicenseManagementException; + License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException; + + void addLicense(License license) throws LicenseManagementException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java new file mode 100644 index 00000000000..face0b3a9c0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseManagerImpl.java @@ -0,0 +1,108 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.wso2.carbon.device.mgt.core.license.mgt; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.core.DeviceManagerImpl; +import org.wso2.carbon.device.mgt.core.config.license.License; +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 javax.xml.namespace.QName; +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Locale; + +public class LicenseManagerImpl implements LicenseManager { + + private static Log log = LogFactory.getLog(DeviceManagerImpl.class); + private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH); + + @Override + public License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException { + GenericArtifactManager artifactManager = + GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(); + try { + GenericArtifact[] artifacts = artifactManager.findGenericArtifacts(new GenericArtifactFilter() { + @Override + public boolean matches(GenericArtifact artifact) throws GovernanceException { + String attributeNameVal = artifact.getAttribute( + DeviceManagementConstants.LicenseProperties.NAME); + String attributeLangVal = artifact.getAttribute( + DeviceManagementConstants.LicenseProperties.LANGUAGE); + return (attributeNameVal != null && attributeLangVal != null && attributeNameVal.equals + (deviceType) && attributeLangVal.equals(languageCode)); + } + }); + if (artifacts == null || artifacts.length <= 0) { + return null; + } + return this.populateLicense(artifacts[0]); + } catch (GovernanceException e) { + throw new LicenseManagementException("Error occurred while retrieving license corresponding to " + + "device type '" + deviceType + "'"); + } catch (ParseException e) { + throw new LicenseManagementException("Error occurred while parsing the ToDate/FromDate date string " + + "of the license configured upon the device type '" + deviceType + "'"); + } + } + + private License populateLicense(GenericArtifact artifact) throws GovernanceException, ParseException { + License license = new License(); + license.setName(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.NAME)); + license.setProvider(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.PROVIDER)); + license.setVersion(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VERSION)); + license.setLanguage(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.LANGUAGE)); + license.setText(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.TEXT)); + license.setValidFrom(format.parse(artifact.getAttribute( + DeviceManagementConstants.LicenseProperties.VALID_FROM))); + license.setValidTo(format.parse(artifact.getAttribute( + DeviceManagementConstants.LicenseProperties.VALID_TO))); + return license; + } + + @Override + public void addLicense(License license) throws LicenseManagementException { + GenericArtifactManager artifactManager = + GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(); + try { + GenericArtifact artifact = + artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com", + DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY)); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.NAME, license.getName()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VERSION, license.getVersion()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.PROVIDER, license.getProvider()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LANGUAGE, license.getLanguage()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.TEXT, license.getText()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO, + license.getValidTo().toString()); + artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM, + license.getValidFrom().toString()); + artifactManager.addGenericArtifact(artifact); + } catch (GovernanceException e) { + throw new LicenseManagementException("Error occurred while adding license artifact", e); + } + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseNotFoundException.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseNotFoundException.java new file mode 100644 index 00000000000..c1c6ce9117d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/license/mgt/LicenseNotFoundException.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. 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.wso2.carbon.device.mgt.core.license.mgt; + +public class LicenseNotFoundException extends RuntimeException { + + private static final long serialVersionUID = -8933146283800122680L; + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public LicenseNotFoundException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public LicenseNotFoundException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public LicenseNotFoundException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public LicenseNotFoundException() { + super(); + } + + public LicenseNotFoundException(Throwable cause) { + super(cause); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/LicenseManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/LicenseManagerUtil.java deleted file mode 100644 index 9b7476a5da6..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/LicenseManagerUtil.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * * - * * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * * - * * Licensed 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.wso2.carbon.device.mgt.core.util; - -import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.core.LicenseManagementException; -import org.wso2.carbon.device.mgt.core.config.license.License; -import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; -import org.wso2.carbon.device.mgt.core.license.mgt.GenericArtifactManagerFactory; -import org.wso2.carbon.governance.api.common.GovernanceArtifactFilter; -import org.wso2.carbon.governance.api.common.GovernanceArtifactManager; -import org.wso2.carbon.governance.api.common.dataobjects.GovernanceArtifact; -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 javax.xml.namespace.QName; -import java.util.Date; - -public class LicenseManagerUtil { - - public static void addDefaultLicenses(LicenseConfig licenseConfig) throws LicenseManagementException { - try { - GenericArtifactManager artifactManager = - GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(); - for (License license : licenseConfig.getLicenses()) { - /* TODO: This method call can be expensive as it appears to do a complete table scan to check the existence - * of an artifact for each license configuration. Need to find the optimal way of doing this */ - if (LicenseManagerUtil.isArtifactExists(artifactManager, license)) { - continue; - } - GenericArtifact artifact = - artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com", - DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY)); - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME, license.getName()); - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_VERSION, - license.getVersion()); - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_LANGUAGE, - license.getLanguage()); - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_PROVIDER, - license.getProvider()); - Date validTo = license.getValidTo(); - if (validTo != null) { - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO, validTo.toString()); - } - Date validFrom = license.getValidFrom(); - if (validFrom != null) { - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM, validFrom.toString()); - } - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LICENSE, license.getText()); - artifactManager.addGenericArtifact(artifact); - } - } catch (GovernanceException e) { - throw new LicenseManagementException("Error occurred while initializing default licences", e); - } - } - - private static boolean isArtifactExists(final GenericArtifactManager artifactManager, - final License license) throws GovernanceException { - GovernanceArtifact[] artifacts = artifactManager.findGenericArtifacts(new GenericArtifactFilter() { - @Override - public boolean matches(GenericArtifact artifact) throws GovernanceException { - return artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME).equals( - license.getName()); - } - }); - return (artifacts != null && artifacts.length > 0); - } - -}