|
|
|
@ -19,6 +19,8 @@
|
|
|
|
|
|
|
|
|
|
package org.wso2.carbon.device.mgt.extensions.license.mgt.registry;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.wso2.carbon.context.CarbonContext;
|
|
|
|
|
import org.wso2.carbon.context.RegistryType;
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
|
|
|
@ -44,6 +46,7 @@ public class RegistryBasedLicenseManager implements LicenseManager {
|
|
|
|
|
|
|
|
|
|
private Registry registry;
|
|
|
|
|
private GenericArtifactManager artifactManager;
|
|
|
|
|
private static final Log log = LogFactory.getLog(RegistryBasedLicenseManager.class);
|
|
|
|
|
|
|
|
|
|
public RegistryBasedLicenseManager() {
|
|
|
|
|
Registry registry = CarbonContext.getThreadLocalCarbonContext().getRegistry(RegistryType.SYSTEM_GOVERNANCE);
|
|
|
|
@ -63,21 +66,15 @@ public class RegistryBasedLicenseManager implements LicenseManager {
|
|
|
|
|
@Override
|
|
|
|
|
public License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException {
|
|
|
|
|
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));
|
|
|
|
|
GenericArtifact artifact = this.getGenericArtifact(deviceType, languageCode);
|
|
|
|
|
if (artifact == null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Generic artifact is null for '" + deviceType + "' device type. Hence license does not " +
|
|
|
|
|
"have content");
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (artifacts == null || artifacts.length <= 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return this.populateLicense(artifacts[0]);
|
|
|
|
|
return this.populateLicense(artifact);
|
|
|
|
|
} catch (GovernanceException e) {
|
|
|
|
|
throw new LicenseManagementException("Error occurred while retrieving license corresponding to " +
|
|
|
|
|
"device type '" + deviceType + "'", e);
|
|
|
|
@ -96,26 +93,35 @@ public class RegistryBasedLicenseManager implements LicenseManager {
|
|
|
|
|
license.setText(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.TEXT));
|
|
|
|
|
|
|
|
|
|
DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
|
|
|
|
|
license.setValidFrom(format.parse(artifact.getAttribute(
|
|
|
|
|
DeviceManagementConstants.LicenseProperties.VALID_FROM)));
|
|
|
|
|
license.setValidTo(format.parse(artifact.getAttribute(
|
|
|
|
|
DeviceManagementConstants.LicenseProperties.VALID_TO)));
|
|
|
|
|
String validFrom = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM);
|
|
|
|
|
if (validFrom != null && !validFrom.isEmpty()) {
|
|
|
|
|
license.setValidFrom(format.parse(validFrom));
|
|
|
|
|
}
|
|
|
|
|
String validTo = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO);
|
|
|
|
|
if (validTo != null && !validTo.isEmpty()) {
|
|
|
|
|
license.setValidFrom(format.parse(validTo));
|
|
|
|
|
}
|
|
|
|
|
return license;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void addLicense(final String deviceType, final License license) throws LicenseManagementException {
|
|
|
|
|
GenericArtifactManager artifactManager =
|
|
|
|
|
GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager(registry);
|
|
|
|
|
try {
|
|
|
|
|
GenericArtifact artifact =
|
|
|
|
|
artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com",
|
|
|
|
|
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY));
|
|
|
|
|
GenericArtifact artifact = this.getGenericArtifact(deviceType, license.getLanguage());
|
|
|
|
|
if (artifact != null) {
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Generic artifact is null for '" + deviceType + "' device type. Hence license does not " +
|
|
|
|
|
"have content");
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
artifact = artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com", deviceType));
|
|
|
|
|
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.ARTIFACT_NAME, license.getName());
|
|
|
|
|
Date validTo = license.getValidTo();
|
|
|
|
|
if (validTo != null) {
|
|
|
|
|
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO, validTo.toString());
|
|
|
|
@ -131,4 +137,20 @@ public class RegistryBasedLicenseManager implements LicenseManager {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private GenericArtifact getGenericArtifact(final String deviceType, final String languageCode)
|
|
|
|
|
throws GovernanceException {
|
|
|
|
|
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.
|
|
|
|
|
equalsIgnoreCase(deviceType) && attributeLangVal.equalsIgnoreCase(languageCode));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return (artifacts == null || artifacts.length == 0) ? null : artifacts[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|