Improving License handling functionality

4.x.x
prabathabey 10 years ago
parent 98ac04479d
commit 8db95a349d

@ -15,37 +15,74 @@
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package org.wso2.carbon.device.mgt.common; package org.wso2.carbon.device.mgt.common;
import java.util.Date;
public class License { public class License {
private String licenseName; private String name;
private String licenseText; private String version;
private String licenseVersion; 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 String getLicenseName() { public Date getValidTo() {
return licenseName; return validTo;
} }
public void setLicenseName(String licenseName) { public void setValidTo(Date validTo) {
this.licenseName = licenseName; this.validTo = validTo;
} }
public String getLicenseText() { public Date getValidFrom() {
return licenseText; return validFrom;
} }
public void setLicenseText(String licenseText) { public void setValidFrom(Date validFrom) {
this.licenseText = licenseText; this.validFrom = validFrom;
} }
public String getLicenseVersion() { public String getText() {
return licenseVersion; return text;
} }
public void setLicenseVersion(String licenseVersion) { public void setText(String text) {
this.licenseVersion = licenseVersion; this.text = text;
} }
} }

@ -21,19 +21,18 @@ package org.wso2.carbon.device.mgt.core;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.base.MultitenantConstants;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.context.RegistryType;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.License; import org.wso2.carbon.device.mgt.common.License;
import org.wso2.carbon.device.mgt.common.LicenseManagementException; import org.wso2.carbon.device.mgt.common.LicenseManagementException;
import org.wso2.carbon.device.mgt.core.internal.LicenseManagementDataHolder;
import org.wso2.carbon.device.mgt.core.license.mgt.GenericArtifactManagerFactory;
import org.wso2.carbon.governance.api.exception.GovernanceException; import org.wso2.carbon.governance.api.exception.GovernanceException;
import org.wso2.carbon.governance.api.generic.GenericArtifactFilter; import org.wso2.carbon.governance.api.generic.GenericArtifactFilter;
import org.wso2.carbon.governance.api.generic.GenericArtifactManager; import org.wso2.carbon.governance.api.generic.GenericArtifactManager;
import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact; import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact;
import org.wso2.carbon.governance.api.util.GovernanceUtils; import org.wso2.carbon.governance.api.util.GovernanceUtils;
import org.wso2.carbon.registry.core.Registry; import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException; import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry; import org.wso2.carbon.registry.core.session.UserRegistry;
import java.text.DateFormat; import java.text.DateFormat;
@ -51,25 +50,15 @@ public class LicenseManagerImpl implements LicenseManager {
public License getLicense(final String deviceType, final String languageCodes) throws LicenseManagementException { public License getLicense(final String deviceType, final String languageCodes) throws LicenseManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("entered get License in license manager impl"); log.debug("Retrieving the license configured upon Device Type: '" + deviceType + "' and Language Code: '" +
languageCodes + "'");
} }
// TODO: After completes JAX-RX user login, this need to be change to CarbonContext
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);
License license = null;
GenericArtifact[] filteredArtifacts; GenericArtifact[] filteredArtifacts;
License license = new License();
try { try {
GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry); GenericArtifactManager artifactManager =
GenericArtifactManager artifactManager = new GenericArtifactManager(registry, GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager();
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY);
filteredArtifacts = artifactManager.findGenericArtifacts( filteredArtifacts = artifactManager.findGenericArtifacts(
new GenericArtifactFilter() { new GenericArtifactFilter() {
@ -82,6 +71,7 @@ public class LicenseManagerImpl implements LicenseManager {
(deviceType) && attributeLangVal.equals(languageCodes)); (deviceType) && attributeLangVal.equals(languageCodes));
} }
}); });
String validFrom; String validFrom;
String validTo; String validTo;
Date fromDate; Date fromDate;
@ -102,26 +92,25 @@ public class LicenseManagerImpl implements LicenseManager {
} }
validFrom = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM); validFrom = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM);
validTo = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO); validTo = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO);
try {
fromDate = format.parse(validFrom); fromDate = format.parse(validFrom);
toDate = format.parse(validTo); toDate = format.parse(validTo);
if (fromDate.getTime() <= new Date().getTime() && new Date().getTime() <= toDate.getTime()) {
license.setLicenseText( license = new License();
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.LICENSE)); if (fromDate.getTime() <= new Date().getTime() && new Date().getTime() <= toDate.getTime()) {
} license.setText(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.LICENSE));
} catch (ParseException e) {
log.error("Valid from: " + validFrom);
log.error("Valid to: " + validTo);
log.error("Valid date parse error: ", e);
} }
} }
} catch (RegistryException regEx) { } catch (RegistryException e) {
String errorMsg = "Registry error occurred: "; String msg = "Error occurred while initializing generic artifact manager associated with retrieving " +
log.error(errorMsg, regEx); "license data stored in registry";
throw new LicenseManagementException(errorMsg, regEx); throw new LicenseManagementException(msg, e);
} finally { } catch (ParseException e) {
PrivilegedCarbonContext.endTenantFlow(); String msg = "Error occurred while parsing the date string";
log.error(msg, e);
throw new LicenseManagementException(msg, e);
} }
return license; return license;
} }
} }

@ -20,22 +20,26 @@
package org.wso2.carbon.device.mgt.core.config; package org.wso2.carbon.device.mgt.core.config;
import org.wso2.carbon.device.mgt.core.config.license.License; import org.wso2.carbon.device.mgt.common.License;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import java.util.List; import java.util.List;
@XmlRootElement(name = "DefaultLicense") @XmlRootElement(name = "DefaultLicense")
public class LicenseManagementConfig { public class LicenseManagementConfig {
private List<License> licenseList; private List<License> licenses;
@XmlElementWrapper(name = "Licenses")
@XmlElement(name = "License") @XmlElement(name = "License")
public List<License> getLicenseList() { public List<License> getLicenses() {
return licenseList; return licenses;
} }
public void setLicenseList(List<License> licenseList) { public void setLicenses(List<License> licenses) {
this.licenseList = licenseList; this.licenses = licenses;
} }
} }

@ -17,10 +17,10 @@
* under the License. * under the License.
* *
*/ */
package org.wso2.carbon.device.mgt.core.internal; package org.wso2.carbon.device.mgt.core.internal;
import org.wso2.carbon.device.mgt.core.LicenseManager; 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.service.RealmService;
import org.wso2.carbon.user.core.tenant.TenantManager; import org.wso2.carbon.user.core.tenant.TenantManager;
@ -29,6 +29,8 @@ public class LicenseManagementDataHolder {
private RealmService realmService; private RealmService realmService;
private TenantManager tenantManager; private TenantManager tenantManager;
private LicenseManager licenseManager; private LicenseManager licenseManager;
private RegistryService registryService;
private static LicenseManagementDataHolder thisInstance = new LicenseManagementDataHolder(); private static LicenseManagementDataHolder thisInstance = new LicenseManagementDataHolder();
public RealmService getRealmService() { public RealmService getRealmService() {
@ -40,7 +42,7 @@ public class LicenseManagementDataHolder {
} }
public TenantManager getTenantManager() { public TenantManager getTenantManager() {
return tenantManager; return this.tenantManager;
} }
public void setTenantManager(TenantManager tenantManager) { public void setTenantManager(TenantManager tenantManager) {
@ -54,10 +56,19 @@ public class LicenseManagementDataHolder {
return thisInstance; return thisInstance;
} }
public LicenseManager getLicenseManager() { public LicenseManager getLicenseManager() {
return licenseManager; return this.licenseManager;
} }
public void setLicenseManager(LicenseManager licenseManager) { public void setLicenseManager(LicenseManager licenseManager) {
this.licenseManager = licenseManager; this.licenseManager = licenseManager;
} }
public void setRegistryService(RegistryService registryService) {
this.registryService = registryService;
}
public RegistryService getRegistryService() {
return this.registryService;
}
} }

@ -22,23 +22,19 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext; 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.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.License;
import org.wso2.carbon.device.mgt.common.LicenseManagementException; import org.wso2.carbon.device.mgt.common.LicenseManagementException;
import org.wso2.carbon.device.mgt.core.LicenseManager; import org.wso2.carbon.device.mgt.core.LicenseManager;
import org.wso2.carbon.device.mgt.core.LicenseManagerImpl; 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.LicenseConfigurationManager;
import org.wso2.carbon.device.mgt.core.config.LicenseManagementConfig; 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.service.LicenseManagementService; import org.wso2.carbon.device.mgt.core.service.LicenseManagementService;
import org.wso2.carbon.governance.api.exception.GovernanceException; import org.wso2.carbon.governance.api.exception.GovernanceException;
import org.wso2.carbon.governance.api.generic.GenericArtifactManager; import org.wso2.carbon.governance.api.generic.GenericArtifactManager;
import org.wso2.carbon.governance.api.generic.dataobjects.GenericArtifact; 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.service.RegistryService;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.service.RealmService;
import javax.xml.namespace.QName; import javax.xml.namespace.QName;
@ -60,7 +56,6 @@ public class LicenseManagementServiceComponent {
private static Log log = LogFactory.getLog(LicenseManagementServiceComponent.class); private static Log log = LogFactory.getLog(LicenseManagementServiceComponent.class);
protected void activate(ComponentContext componentContext) { protected void activate(ComponentContext componentContext) {
try { try {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Initializing license management core bundle"); log.debug("Initializing license management core bundle");
@ -97,23 +92,12 @@ public class LicenseManagementServiceComponent {
} }
private void addDefaultLicenses(LicenseManagementConfig licenseManagementConfig) throws LicenseManagementException { 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 { try {
GenericArtifactManager artifactManager = new GenericArtifactManager(registry, GenericArtifactManager artifactManager =
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY); GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager();
GenericArtifact artifact;
for (org.wso2.carbon.device.mgt.core.config.license.License license : licenseManagementConfig
.getLicenseList()) {
GenericArtifact artifact;
for (License license : licenseManagementConfig.getLicenses()) {
artifact = artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com", artifact = artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com",
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY)); DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY));
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME, license.getName()); artifact.setAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME, license.getName());
@ -127,21 +111,18 @@ public class LicenseManagementServiceComponent {
license.getValidTo().toString()); license.getValidTo().toString());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM, artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM,
license.getValidFrom().toString()); license.getValidFrom().toString());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LICENSE,license.getLicense()); artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LICENSE, license.getText());
artifactManager.addGenericArtifact(artifact); artifactManager.addGenericArtifact(artifact);
} }
} catch (GovernanceException govEx) { } catch (GovernanceException e) {
String errorMsg = "Governance error"; String msg = "Error occurred while initializing default licences";
log.error(errorMsg); throw new LicenseManagementException(msg, e);
throw new LicenseManagementException(errorMsg, govEx);
} catch (RegistryException regEx) {
String errorMsg = "Registry error";
throw new LicenseManagementException(errorMsg, regEx);
} }
} }
/** /**
* Sets Realm Service. * Sets Realm Service.
*
* @param realmService An instance of RealmService * @param realmService An instance of RealmService
*/ */
protected void setRealmService(RealmService realmService) { protected void setRealmService(RealmService realmService) {
@ -153,6 +134,7 @@ public class LicenseManagementServiceComponent {
/** /**
* Unsets Realm Service. * Unsets Realm Service.
*
* @param realmService An instance of RealmService * @param realmService An instance of RealmService
*/ */
protected void unsetRealmService(RealmService realmService) { protected void unsetRealmService(RealmService realmService) {
@ -162,11 +144,28 @@ public class LicenseManagementServiceComponent {
LicenseManagementDataHolder.getInstance().setRealmService(null); LicenseManagementDataHolder.getInstance().setRealmService(null);
} }
/**
* Sets Registry Service.
*
* @param registryService An instance of RegistryService
*/
protected void setRegistryService(RegistryService registryService) { protected void setRegistryService(RegistryService registryService) {
// CommonUtil.setRegistryService(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) { protected void unsetRegistryService(RegistryService registryService) {
//CommonUtil.setRegistryService(null); if (log.isDebugEnabled()) {
log.debug("Unsetting Registry Service");
}
LicenseManagementDataHolder.getInstance().setRegistryService(null);
} }
} }

@ -0,0 +1,61 @@
/*
* 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.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.governance.api.generic.GenericArtifactManager;
import org.wso2.carbon.registry.api.Registry;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import java.util.HashMap;
import java.util.Map;
public class GenericArtifactManagerFactory {
private static Map<Integer, GenericArtifactManager> tenantArtifactManagers =
new HashMap<Integer, GenericArtifactManager>();
private static final Object lock = new Object();
public static GenericArtifactManager getTenantAwareGovernanceArtifactManager() throws
LicenseManagementException {
Registry registry = CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.USER_GOVERNANCE);
try {
GenericArtifactManager artifactManager;
synchronized (lock) {
artifactManager =
tenantArtifactManagers.get(CarbonContext.getThreadLocalCarbonContext().getTenantId());
if (artifactManager == null) {
artifactManager =
new GenericArtifactManager((org.wso2.carbon.registry.core.Registry) registry,
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY);
tenantArtifactManagers.put(CarbonContext.getThreadLocalCarbonContext().getTenantId(),
artifactManager);
}
}
return artifactManager;
} catch (RegistryException e) {
throw new LicenseManagementException("Error occurred while initializing GovernanceArtifactManager " +
"associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'");
}
}
}
Loading…
Cancel
Save