From 9ad8d6eab9c8b12fbe113213ae62c62c9bb3c8f9 Mon Sep 17 00:00:00 2001 From: prabathabey Date: Wed, 18 Feb 2015 18:33:18 +0530 Subject: [PATCH] Refactoring License management functionality --- .../mgt/common/DeviceManagementConstants.java | 6 +- .../carbon/device/mgt/common/License.java | 88 ----------------- .../org.wso2.carbon.device.mgt.core/pom.xml | 7 +- .../mgt/core/DeviceManagementConstants.java | 3 +- .../mgt/core}/LicenseManagementException.java | 2 +- .../device/mgt/core/LicenseManager.java | 6 +- .../device/mgt/core/LicenseManagerImpl.java | 7 +- .../mgt/core/config/license/License.java | 39 ++++---- .../LicenseConfig.java} | 8 +- .../LicenseConfigurationManager.java | 30 +++--- ...eManagementAxis2ConfigurationObserver.java | 45 +++++++++ .../DeviceManagementServiceComponent.java | 5 +- ...eManagementAxis2ConfigurationObserver.java | 55 +++++++++++ .../LicenseManagementServiceComponent.java | 95 +++++++------------ .../mgt/GenericArtifactManagerFactory.java | 26 +++-- .../service/LicenseManagementService.java | 17 ++-- .../mgt/core/util/LicenseManagerUtil.java | 86 +++++++++++++++++ .../src/main/resources/DefaultLicense.xml | 50 ---------- .../main/resources/conf/license-config.xml | 52 ++++++++++ .../src/main/resources/p2.inf | 1 + .../src/main/resources/rxts/license.rxt | 2 +- pom.xml | 1 - 22 files changed, 352 insertions(+), 279 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/License.java rename components/device-mgt/{org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common => org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core}/LicenseManagementException.java (97%) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/{LicenseManagementConfig.java => license/LicenseConfig.java} (86%) rename components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/{ => license}/LicenseConfigurationManager.java (68%) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementAxis2ConfigurationObserver.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementAxis2ConfigurationObserver.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/LicenseManagerUtil.java delete mode 100644 features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/DefaultLicense.xml create mode 100644 features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/license-config.xml 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 31c056ed6d..494bc781c4 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 @@ -20,7 +20,6 @@ package org.wso2.carbon.device.mgt.common; public final class DeviceManagementConstants { public static final class DataSourceProperties { - private DataSourceProperties() { throw new AssertionError(); } @@ -31,7 +30,6 @@ public final class DeviceManagementConstants { } public static final class SecureValueProperties { - private SecureValueProperties() { throw new AssertionError(); } @@ -41,7 +39,6 @@ public final class DeviceManagementConstants { } public static final class MobileDeviceTypes { - private MobileDeviceTypes() { throw new AssertionError(); } @@ -52,7 +49,6 @@ public final class DeviceManagementConstants { } public static final class LanguageCodes { - private LanguageCodes() { throw new AssertionError(); } @@ -62,7 +58,6 @@ public final class DeviceManagementConstants { } public static final class LicenseProperties { - private LicenseProperties() { throw new AssertionError(); } @@ -75,4 +70,5 @@ public final class DeviceManagementConstants { public static final String LICENSE = "overview_license"; public static final String LICENSE_REGISTRY_KEY = "license"; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/License.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/License.java deleted file mode 100644 index 3f38f106d4..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/License.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Copyright (c) 2014, 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.common; - -import java.util.Date; - -public class License { - - private String name; - private String version; - private String language; - private String provider; - private Date validTo; - private Date validFrom; - private String text; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getVersion() { - return version; - } - - public void setVersion(String version) { - this.version = version; - } - - public String getLanguage() { - return language; - } - - public void setLanguage(String language) { - this.language = language; - } - - public String getProvider() { - return provider; - } - - public void setProvider(String provider) { - this.provider = provider; - } - - public Date getValidTo() { - return validTo; - } - - public void setValidTo(Date validTo) { - this.validTo = validTo; - } - - public Date getValidFrom() { - return validFrom; - } - - public void setValidFrom(Date validFrom) { - this.validFrom = validFrom; - } - - public String getText() { - return text; - } - - public void setText(String text) { - this.text = text; - } - -} 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 f4efe1eef2..a195bad20d 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 @@ -57,11 +57,9 @@ org.osgi.framework, org.osgi.service.component, org.apache.commons.logging, - javax.xml.bind.*, javax.naming, + javax.xml.*, javax.sql, - javax.xml.parsers, - javax.transaction.*, org.wso2.carbon.governance.api.*, org.wso2.carbon.base, org.wso2.carbon.context, @@ -72,8 +70,7 @@ org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.user.api, org.wso2.carbon.user.core.*, - org.w3c.dom, - javax.xml.namespace.*; + org.w3c.dom !org.wso2.carbon.device.mgt.core.internal, diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 19478bc564..62be056434 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -23,8 +23,9 @@ public final class DeviceManagementConstants { private Common() { throw new AssertionError(); } + public static final String PROPERTY_SETUP = "setup"; - public static final String DEFAULT_LICENSE_CONFIG_XML_NAME = "DefaultLicense.xml"; + public static final String DEFAULT_LICENSE_CONFIG_XML_NAME = "license-config.xml"; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/LicenseManagementException.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagementException.java similarity index 97% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/LicenseManagementException.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagementException.java index 1b39a74981..67937c11b6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/LicenseManagementException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/LicenseManagementException.java @@ -18,7 +18,7 @@ * */ -package org.wso2.carbon.device.mgt.common; +package org.wso2.carbon.device.mgt.core; public class LicenseManagementException extends Exception{ 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 35c846ac2e..6126f4a07a 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 @@ -20,8 +20,10 @@ package org.wso2.carbon.device.mgt.core; -import org.wso2.carbon.device.mgt.common.*; +import org.wso2.carbon.device.mgt.core.config.license.License; public interface LicenseManager { - public License getLicense(String deviceType,String languageCodes) throws LicenseManagementException; + + 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 78b68d1dc9..857682e168 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 @@ -22,18 +22,13 @@ 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.common.License; -import org.wso2.carbon.device.mgt.common.LicenseManagementException; -import org.wso2.carbon.device.mgt.core.internal.LicenseManagementDataHolder; +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.governance.api.util.GovernanceUtils; -import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.api.RegistryException; -import org.wso2.carbon.registry.core.session.UserRegistry; import java.text.DateFormat; import java.text.ParseException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/License.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/License.java index e9d5188559..dc7a24135e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/License.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/License.java @@ -22,6 +22,7 @@ package org.wso2.carbon.device.mgt.core.config.license; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import java.util.Date; @XmlRootElement(name = "License") public class License { @@ -30,12 +31,11 @@ public class License { private String name; private String version; private String language; + private Date validFrom; + private Date validTo; + private String text; - private String validFrom; - private String validTo; - private String license; - - @XmlElement(name = "provider") + @XmlElement(name = "Provider", required = true) public String getProvider() { return provider; } @@ -44,7 +44,7 @@ public class License { this.provider = provider; } - @XmlElement(name = "name") + @XmlElement(name = "Name", required = true) public String getName() { return name; } @@ -53,7 +53,7 @@ public class License { this.name = name; } - @XmlElement(name = "version") + @XmlElement(name = "Version", required = true) public String getVersion() { return version; } @@ -62,7 +62,7 @@ public class License { this.version = version; } - @XmlElement(name = "language") + @XmlElement(name = "Language", required = true) public String getLanguage() { return language; } @@ -71,30 +71,31 @@ public class License { this.language = language; } - @XmlElement(name = "license") - public String getLicense() { - return license; + @XmlElement(name = "Text", required = true) + public String getText() { + return text; } - public void setLicense(String license) { - this.license = license; + public void setText(String text) { + this.text = text; } - @XmlElement(name = "validFrom") - public String getValidFrom() { + @XmlElement(name = "ValidFrom") + public Date getValidFrom() { return validFrom; } - public void setValidFrom(String validFrom) { + public void setValidFrom(Date validFrom) { this.validFrom = validFrom; } - @XmlElement(name = "validTo") - public String getValidTo() { + @XmlElement(name = "ValidTo") + public Date getValidTo() { return validTo; } - public void setValidTo(String validTo) { + public void setValidTo(Date validTo) { this.validTo = validTo; } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/LicenseManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfig.java similarity index 86% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/LicenseManagementConfig.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfig.java index acd426e61c..b378fa70c2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/LicenseManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfig.java @@ -18,17 +18,15 @@ * */ -package org.wso2.carbon.device.mgt.core.config; - -import org.wso2.carbon.device.mgt.common.License; +package org.wso2.carbon.device.mgt.core.config.license; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; import java.util.List; -@XmlRootElement(name = "DefaultLicense") -public class LicenseManagementConfig { +@XmlRootElement(name = "LicenseConfig") +public class LicenseConfig { private List licenses; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/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 similarity index 68% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/LicenseConfigurationManager.java rename to components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/license/LicenseConfigurationManager.java index 798f6870a6..c0137c7fe9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/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 @@ -18,10 +18,10 @@ * */ -package org.wso2.carbon.device.mgt.core.config; +package org.wso2.carbon.device.mgt.core.config.license; import org.w3c.dom.Document; -import org.wso2.carbon.device.mgt.common.LicenseManagementException; +import org.wso2.carbon.device.mgt.core.LicenseManagementException; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.utils.CarbonUtils; @@ -32,11 +32,11 @@ import java.io.File; public class LicenseConfigurationManager { - private LicenseManagementConfig licenseMgtConfig; + private LicenseConfig licenseConfig; private static LicenseConfigurationManager licenseConfigManager; - private static final String licenseMgtConfigXMLPath = - CarbonUtils.getCarbonConfigDirPath() + File.separator + + private static final String LICENSE_CONFIG_PATH = + CarbonUtils.getEtcCarbonConfigDirPath() + File.separator + DeviceManagementConstants.Common.DEFAULT_LICENSE_CONFIG_XML_NAME; public static LicenseConfigurationManager getInstance() { @@ -51,23 +51,23 @@ public class LicenseConfigurationManager { } public synchronized void initConfig() throws LicenseManagementException { - - //catch generic exception.if any exception occurs wrap and throw LicenseManagementException try { - File licenseMgtConfig = new File(licenseMgtConfigXMLPath); + File licenseMgtConfig = new File(LicenseConfigurationManager.LICENSE_CONFIG_PATH); Document doc = DeviceManagerUtil.convertToDocument(licenseMgtConfig); /* Un-marshaling License Management configuration */ - JAXBContext cdmContext = JAXBContext.newInstance(LicenseManagementConfig.class); + JAXBContext cdmContext = JAXBContext.newInstance(LicenseConfig.class); Unmarshaller unmarshaller = cdmContext.createUnmarshaller(); - this.licenseMgtConfig = (LicenseManagementConfig) unmarshaller.unmarshal(doc); + this.licenseConfig = (LicenseConfig) unmarshaller.unmarshal(doc); } catch (Exception e) { - throw new LicenseManagementException("Error occurred while initializing RSS config", e); - } - } + /* Catches generic exception as there's no specific task to be carried out catching a particular + exception */ + throw new LicenseManagementException("Error occurred while initializing License Configurations", e); + } + } - public LicenseManagementConfig getLicenseMgtConfig() { - return licenseMgtConfig; + public LicenseConfig getLicenseConfig() { + return licenseConfig; } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementAxis2ConfigurationObserver.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementAxis2ConfigurationObserver.java new file mode 100644 index 0000000000..65d52e9fdf --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementAxis2ConfigurationObserver.java @@ -0,0 +1,45 @@ +/* + * * + * * 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.wso2.carbon.utils.Axis2ConfigurationContextObserver; + +public class DeviceManagementAxis2ConfigurationObserver implements Axis2ConfigurationContextObserver { + + @Override + public void creatingConfigurationContext(int i) { + + } + + @Override + public void createdConfigurationContext(ConfigurationContext configurationContext) { + + } + + @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/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 f97d232226..443239ad11 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 @@ -32,6 +32,7 @@ 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.dao.DeviceManagementDAOFactory; 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.user.core.service.RealmService; @@ -87,8 +88,8 @@ public class DeviceManagementServiceComponent { log.debug("Registering OSGi service DeviceManagementService"); } BundleContext bundleContext = componentContext.getBundleContext(); - bundleContext.registerService(DeviceManagementService.class.getName(), - new DeviceManagementService(), null); + bundleContext.registerService(DeviceManagementService.class.getName(), new DeviceManagementService(), null); + bundleContext.registerService(LicenseManagementService.class.getName(), new LicenseManagementService(), null); if (log.isDebugEnabled()) { log.debug("Device management core bundle has been successfully initialized"); } 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 new file mode 100644 index 0000000000..350a1abb29 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/LicenseManagementAxis2ConfigurationObserver.java @@ -0,0 +1,55 @@ +/* + * * + * * 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/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 b310029c6e..1b03ab20f6 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 @@ -22,35 +22,18 @@ 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.device.mgt.common.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.common.License; -import org.wso2.carbon.device.mgt.common.LicenseManagementException; +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.LicenseConfigurationManager; -import org.wso2.carbon.device.mgt.core.config.LicenseManagementConfig; -import org.wso2.carbon.device.mgt.core.license.mgt.GenericArtifactManagerFactory; +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.governance.api.exception.GovernanceException; -import org.wso2.carbon.governance.api.generic.GenericArtifactManager; -import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact; +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; -import javax.xml.namespace.QName; - -/** - * @scr.component name="org.wso2.carbon.license.manager" immediate="true" - * @scr.reference name="user.realmservice.default" - * interface="org.wso2.carbon.user.core.service.RealmService" - * cardinality="1..1" - * policy="dynamic" - * bind="setRealmService" - * unbind="unsetRealmService" - * @scr.reference name="registryService.service" - * interface="org.wso2.carbon.registry.core.service.RegistryService" cardinality="1..1" - * policy="dynamic" bind="setRegistryService" unbind="unsetRegistryService" - */ public class LicenseManagementServiceComponent { private static Log log = LogFactory.getLog(LicenseManagementServiceComponent.class); @@ -63,21 +46,36 @@ public class LicenseManagementServiceComponent { LicenseManager licenseManager = new LicenseManagerImpl(); LicenseManagementDataHolder.getInstance().setLicenseManager(licenseManager); - /* If -Dsetup option enabled then create creates default license management */ - String setupOption = System.getProperty( - org.wso2.carbon.device.mgt.core.DeviceManagementConstants.Common.PROPERTY_SETUP); + 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.Check default licenses and add if not exists in registry"); + 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(); } - LicenseConfigurationManager.getInstance().initConfig(); - LicenseManagementConfig licenseManagementConfig = LicenseConfigurationManager.getInstance() - .getLicenseMgtConfig(); - addDefaultLicenses(licenseManagementConfig); } + if (log.isDebugEnabled()) { - log.debug("Registering OSGi service LicenseManagementService"); + log.debug("Registering OSGi service 'LicenseManagementService'"); } BundleContext bundleContext = componentContext.getBundleContext(); bundleContext @@ -85,38 +83,9 @@ public class LicenseManagementServiceComponent { if (log.isDebugEnabled()) { log.debug("License management core bundle has been successfully initialized"); } - } catch (Throwable throwable) { + } catch (Throwable e) { String msg = "Error occurred while initializing license management core bundle"; - log.error(msg, throwable); - } - } - - private void addDefaultLicenses(LicenseManagementConfig licenseManagementConfig) throws LicenseManagementException { - try { - GenericArtifactManager artifactManager = - GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(); - - GenericArtifact artifact; - for (License license : licenseManagementConfig.getLicenses()) { - 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()); - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO, - license.getValidTo().toString()); - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM, - license.getValidFrom().toString()); - artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LICENSE, license.getText()); - artifactManager.addGenericArtifact(artifact); - } - } catch (GovernanceException e) { - String msg = "Error occurred while initializing default licences"; - throw new LicenseManagementException(msg, e); + log.error(msg, e); } } 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 35dc459cee..810bedabd6 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 @@ -19,12 +19,14 @@ package org.wso2.carbon.device.mgt.core.license.mgt; import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.RegistryType; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; -import org.wso2.carbon.device.mgt.common.LicenseManagementException; +import org.wso2.carbon.device.mgt.core.LicenseManagementException; +import org.wso2.carbon.device.mgt.core.internal.LicenseManagementDataHolder; 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; @@ -37,18 +39,30 @@ public class GenericArtifactManagerFactory { public static GenericArtifactManager getTenantAwareGovernanceArtifactManager() throws LicenseManagementException { - Registry registry = CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.USER_GOVERNANCE); + Registry registry; + int tenantId; + try { + tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + registry = + LicenseManagementDataHolder.getInstance().getRegistryService().getGovernanceSystemRegistry( + tenantId); + } catch (RegistryException e) { + throw new LicenseManagementException("Error occurred while initializing tenant system registry " + + "to be used to manipulate License artifacts", e); + } + try { GenericArtifactManager artifactManager; synchronized (lock) { artifactManager = - tenantArtifactManagers.get(CarbonContext.getThreadLocalCarbonContext().getTenantId()); + tenantArtifactManagers.get(tenantId); if (artifactManager == null) { + /* Hack, to fix https://wso2.org/jira/browse/REGISTRY-2427 */ + //GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry); artifactManager = new GenericArtifactManager((org.wso2.carbon.registry.core.Registry) registry, DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY); - tenantArtifactManagers.put(CarbonContext.getThreadLocalCarbonContext().getTenantId(), - artifactManager); + tenantArtifactManagers.put(tenantId, artifactManager); } } return artifactManager; 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 7358c52596..e4c10e109e 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 @@ -17,20 +17,19 @@ * under the License. * */ - package org.wso2.carbon.device.mgt.core.service; -import org.wso2.carbon.device.mgt.common.License; -import org.wso2.carbon.device.mgt.common.LicenseManagementException; +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); - } + @Override + public License getLicense(String deviceType, String languageCode) + throws LicenseManagementException { + return LicenseManagementDataHolder.getInstance().getLicenseManager().getLicense(deviceType, languageCode); + } + } 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 new file mode 100644 index 0000000000..9b7476a5da --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/LicenseManagerUtil.java @@ -0,0 +1,86 @@ +/* + * * + * * 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); + } + +} diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/DefaultLicense.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/DefaultLicense.xml deleted file mode 100644 index 2cea20f942..0000000000 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/DefaultLicense.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - wso2 - android - 1.0.0 - 1.0.0 - 01-01-2014 - 31-12-2035 - - - - wso2 - ios - 1.0.0 - 1.0.0 - 01-01-2014 - 31-12-2035 - - - - wso2 - windows - 1.0.0 - 1.0.0 - 01-01-2014 - 31-12-2035 - - - \ No newline at end of file diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/license-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/license-config.xml new file mode 100644 index 0000000000..dfaa9db038 --- /dev/null +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/license-config.xml @@ -0,0 +1,52 @@ + + + + + + + wso2 + android + 1.0.0 + 1.0.0 + 01-01-2014 + 31-12-2035 + + + + wso2 + ios + 1.0.0 + 1.0.0 + 01-01-2014 + 31-12-2035 + + + + wso2 + windows + 1.0.0 + 1.0.0 + 01-01-2014 + 31-12-2035 + + + + \ No newline at end of file diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf index 4951100d4b..c00882bffa 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/p2.inf @@ -1,4 +1,5 @@ instructions.configure = \ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/cdm-config.xml,target:${installFolder}/../../conf/cdm-config.xml,overwrite:true);\ +org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/conf/license-config.xml,target:${installFolder}/../../conf/etc/license-config.xml,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/dbscripts/cdm,target:${installFolder}/../../../dbscripts/cdm,overwrite:true);\ org.eclipse.equinox.p2.touchpoint.natives.copy(source:${installFolder}/../features/org.wso2.carbon.device.mgt.server_${feature.version}/rxts/license.rxt,target:${installFolder}/../../../repository/resources/rxts/license.rxt,overwrite:true);\ \ No newline at end of file diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/rxts/license.rxt b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/rxts/license.rxt index 2fc84c6db9..b85f5b73fb 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/rxts/license.rxt +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/rxts/license.rxt @@ -1,7 +1,7 @@ - /license/@{overview_provider}/@{overview_name}/@{overview_language}/@{overview_version} + /device-mgt/license/@{overview_name}/@{overview_language}/@{overview_version} overview_name diff --git a/pom.xml b/pom.xml index ee307fb047..98ea945241 100644 --- a/pom.xml +++ b/pom.xml @@ -612,7 +612,6 @@ 0.9.1-SNAPSHOT -