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 a6087eb0d32..952b316a5d7 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 @@ -72,7 +72,8 @@ org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.user.api, org.wso2.carbon.user.core.*, - org.w3c.dom + org.w3c.dom, + javax.xml.namespace.*; !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 8af1724de77..ee84a217cf9 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 @@ -22,6 +22,7 @@ public final class DeviceManagementConstants { throw new AssertionError(); } public static final String PROPERTY_SETUP = "setup"; + public static final String DEFAULT_LICENSE_CONFIG_XML_NAME = "DefaultLicense.xml"; } } 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 b1316eff5c0..3fd0ad6acc1 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,11 +22,27 @@ 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.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.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.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.registry.core.Registry; +import org.wso2.carbon.registry.core.exceptions.RegistryException; +import org.wso2.carbon.registry.core.service.RegistryService; +import org.wso2.carbon.registry.core.session.UserRegistry; 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" @@ -35,6 +51,9 @@ import org.wso2.carbon.user.core.service.RealmService; * 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 { @@ -42,19 +61,83 @@ public class LicenseManagementServiceComponent { protected void activate(ComponentContext componentContext) { - if (log.isDebugEnabled()) { - log.debug("Initializing license management core bundle"); - } - LicenseManager licenseManager = new LicenseManagerImpl(); - LicenseManagementDataHolder.getInstance().setLicenseManager(licenseManager); + try { + if (log.isDebugEnabled()) { + log.debug("Initializing license management core bundle"); + } + LicenseManager licenseManager = new LicenseManagerImpl(); + LicenseManagementDataHolder.getInstance().setLicenseManager(licenseManager); - if (log.isDebugEnabled()) { - log.debug("Registering OSGi service LicenseManagementService"); + /* 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 (setupOption != null) { + if (log.isDebugEnabled()) { + log.debug("-Dsetup is enabled.Check default licenses and add if not exists in registry"); + } + LicenseConfigurationManager.getInstance().initConfig(); + LicenseManagementConfig licenseManagementConfig = LicenseConfigurationManager.getInstance() + .getLicenseMgtConfig(); + addDefaultLicenses(licenseManagementConfig); + } + + 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 throwable) { + String msg = "Error occurred while initializing license management core bundle"; + log.error(msg, throwable); } - 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"); + } + + private void addDefaultLicenses(LicenseManagementConfig licenseManagementConfig) throws LicenseManagementException { + + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername("admin"); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(MultitenantConstants.SUPER_TENANT_ID); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME); + + Registry registry = (UserRegistry) PrivilegedCarbonContext.getThreadLocalCarbonContext().getRegistry( + RegistryType.USER_GOVERNANCE); + try { + GenericArtifactManager artifactManager = new GenericArtifactManager(registry, + DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY); + GenericArtifact artifact; + + for (org.wso2.carbon.device.mgt.core.config.license.License license : licenseManagementConfig + .getLicenseList()) { + + 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.getLicense()); + artifactManager.addGenericArtifact(artifact); + } + } catch (GovernanceException govEx) { + String errorMsg = "Governance error"; + log.error(errorMsg); + throw new LicenseManagementException(errorMsg, govEx); + } catch (RegistryException regEx) { + String errorMsg = "Registry error"; + throw new LicenseManagementException(errorMsg, regEx); } } @@ -79,4 +162,12 @@ public class LicenseManagementServiceComponent { } LicenseManagementDataHolder.getInstance().setRealmService(null); } + + protected void setRegistryService(RegistryService registryService) { + // CommonUtil.setRegistryService(registryService); + } + + protected void unsetRegistryService(RegistryService registryService) { + //CommonUtil.setRegistryService(null); + } }