Improving License handling functionality

revert-70aa11f8
prabathabey 10 years ago
parent 98ac04479d
commit 8db95a349d

@ -15,37 +15,74 @@
* 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 licenseName;
private String licenseText;
private String licenseVersion;
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 String getLicenseName() {
return licenseName;
public Date getValidTo() {
return validTo;
}
public void setLicenseName(String licenseName) {
this.licenseName = licenseName;
public void setValidTo(Date validTo) {
this.validTo = validTo;
}
public String getLicenseText() {
return licenseText;
public Date getValidFrom() {
return validFrom;
}
public void setLicenseText(String licenseText) {
this.licenseText = licenseText;
public void setValidFrom(Date validFrom) {
this.validFrom = validFrom;
}
public String getLicenseVersion() {
return licenseVersion;
public String getText() {
return text;
}
public void setLicenseVersion(String licenseVersion) {
this.licenseVersion = licenseVersion;
public void setText(String text) {
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.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.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.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.core.exceptions.RegistryException;
import org.wso2.carbon.registry.api.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;
import java.text.DateFormat;
@ -51,25 +50,15 @@ public class LicenseManagerImpl implements LicenseManager {
public License getLicense(final String deviceType, final String languageCodes) throws LicenseManagementException {
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;
License license = new License();
try {
GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
GenericArtifactManager artifactManager = new GenericArtifactManager(registry,
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY);
GenericArtifactManager artifactManager =
GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager();
filteredArtifacts = artifactManager.findGenericArtifacts(
new GenericArtifactFilter() {
@ -82,6 +71,7 @@ public class LicenseManagerImpl implements LicenseManager {
(deviceType) && attributeLangVal.equals(languageCodes));
}
});
String validFrom;
String validTo;
Date fromDate;
@ -102,26 +92,25 @@ public class LicenseManagerImpl implements LicenseManager {
}
validFrom = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM);
validTo = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO);
try {
fromDate = format.parse(validFrom);
toDate = format.parse(validTo);
if (fromDate.getTime() <= new Date().getTime() && new Date().getTime() <= toDate.getTime()) {
license.setLicenseText(
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);
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 regEx) {
String errorMsg = "Registry error occurred: ";
log.error(errorMsg, regEx);
throw new LicenseManagementException(errorMsg, regEx);
} finally {
PrivilegedCarbonContext.endTenantFlow();
} 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;
}
}

@ -20,22 +20,26 @@
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.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "DefaultLicense")
public class LicenseManagementConfig {
private List<License> licenseList;
private List<License> licenses;
@XmlElementWrapper(name = "Licenses")
@XmlElement(name = "License")
public List<License> getLicenseList() {
return licenseList;
public List<License> getLicenses() {
return licenses;
}
public void setLicenseList(List<License> licenseList) {
this.licenseList = licenseList;
public void setLicenses(List<License> licenses) {
this.licenses = licenses;
}
}

@ -17,10 +17,10 @@
* 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;
@ -29,6 +29,8 @@ public class LicenseManagementDataHolder {
private RealmService realmService;
private TenantManager tenantManager;
private LicenseManager licenseManager;
private RegistryService registryService;
private static LicenseManagementDataHolder thisInstance = new LicenseManagementDataHolder();
public RealmService getRealmService() {
@ -40,7 +42,7 @@ public class LicenseManagementDataHolder {
}
public TenantManager getTenantManager() {
return tenantManager;
return this.tenantManager;
}
public void setTenantManager(TenantManager tenantManager) {
@ -54,10 +56,19 @@ public class LicenseManagementDataHolder {
return thisInstance;
}
public LicenseManager getLicenseManager() {
return licenseManager;
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;
}
}

@ -22,23 +22,19 @@ 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.License;
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.license.mgt.GenericArtifactManagerFactory;
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;
@ -60,7 +56,6 @@ 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");
@ -97,23 +92,12 @@ public class LicenseManagementServiceComponent {
}
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()) {
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());
@ -127,21 +111,18 @@ public class LicenseManagementServiceComponent {
license.getValidTo().toString());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM,
license.getValidFrom().toString());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LICENSE,license.getLicense());
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.LICENSE, license.getText());
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);
} catch (GovernanceException e) {
String msg = "Error occurred while initializing default licences";
throw new LicenseManagementException(msg, e);
}
}
/**
* Sets Realm Service.
*
* @param realmService An instance of RealmService
*/
protected void setRealmService(RealmService realmService) {
@ -153,6 +134,7 @@ public class LicenseManagementServiceComponent {
/**
* Unsets Realm Service.
*
* @param realmService An instance of RealmService
*/
protected void unsetRealmService(RealmService realmService) {
@ -162,11 +144,28 @@ public class LicenseManagementServiceComponent {
LicenseManagementDataHolder.getInstance().setRealmService(null);
}
/**
* Sets Registry Service.
*
* @param registryService An instance of 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) {
//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