forked from community/device-mgt-core
parent
9ad8d6eab9
commit
916760275f
@ -1,111 +0,0 @@
|
|||||||
/*
|
|
||||||
*
|
|
||||||
* 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;
|
|
||||||
|
|
||||||
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.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.registry.api.RegistryException;
|
|
||||||
|
|
||||||
import java.text.DateFormat;
|
|
||||||
import java.text.ParseException;
|
|
||||||
import java.text.SimpleDateFormat;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
public class LicenseManagerImpl implements LicenseManager {
|
|
||||||
|
|
||||||
private static Log log = LogFactory.getLog(DeviceManagerImpl.class);
|
|
||||||
private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public License getLicense(final String deviceType, final String languageCodes) throws LicenseManagementException {
|
|
||||||
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Retrieving the license configured upon Device Type: '" + deviceType + "' and Language Code: '" +
|
|
||||||
languageCodes + "'");
|
|
||||||
}
|
|
||||||
|
|
||||||
License license = null;
|
|
||||||
GenericArtifact[] filteredArtifacts;
|
|
||||||
try {
|
|
||||||
GenericArtifactManager artifactManager =
|
|
||||||
GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager();
|
|
||||||
|
|
||||||
filteredArtifacts = artifactManager.findGenericArtifacts(
|
|
||||||
new GenericArtifactFilter() {
|
|
||||||
public boolean matches(GenericArtifact artifact) throws GovernanceException {
|
|
||||||
String attributeNameVal = artifact.getAttribute(
|
|
||||||
DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME);
|
|
||||||
String attributeLangVal = artifact.getAttribute(
|
|
||||||
DeviceManagementConstants.LicenseProperties.OVERVIEW_LANGUAGE);
|
|
||||||
return (attributeNameVal != null && attributeLangVal != null && attributeNameVal.equals
|
|
||||||
(deviceType) && attributeLangVal.equals(languageCodes));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
String validFrom;
|
|
||||||
String validTo;
|
|
||||||
Date fromDate;
|
|
||||||
Date toDate;
|
|
||||||
|
|
||||||
for (GenericArtifact artifact : filteredArtifacts) {
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Overview name: " +
|
|
||||||
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_NAME));
|
|
||||||
log.debug("Overview provider: " +
|
|
||||||
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_PROVIDER));
|
|
||||||
log.debug("Overview language: " +
|
|
||||||
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.OVERVIEW_LANGUAGE));
|
|
||||||
log.debug("Overview validity from: " +
|
|
||||||
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM));
|
|
||||||
log.debug("Overview validity to: " +
|
|
||||||
artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO));
|
|
||||||
}
|
|
||||||
validFrom = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM);
|
|
||||||
validTo = artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VALID_TO);
|
|
||||||
|
|
||||||
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 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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,55 +0,0 @@
|
|||||||
/*
|
|
||||||
* *
|
|
||||||
* * 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) {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,74 +0,0 @@
|
|||||||
/*
|
|
||||||
*
|
|
||||||
* 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.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;
|
|
||||||
|
|
||||||
public class LicenseManagementDataHolder {
|
|
||||||
|
|
||||||
private RealmService realmService;
|
|
||||||
private TenantManager tenantManager;
|
|
||||||
private LicenseManager licenseManager;
|
|
||||||
private RegistryService registryService;
|
|
||||||
|
|
||||||
private static LicenseManagementDataHolder thisInstance = new LicenseManagementDataHolder();
|
|
||||||
|
|
||||||
public RealmService getRealmService() {
|
|
||||||
return realmService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setRealmService(RealmService realmService) {
|
|
||||||
this.realmService = realmService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public TenantManager getTenantManager() {
|
|
||||||
return this.tenantManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTenantManager(TenantManager tenantManager) {
|
|
||||||
this.tenantManager = tenantManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
private LicenseManagementDataHolder() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public static LicenseManagementDataHolder getInstance() {
|
|
||||||
return thisInstance;
|
|
||||||
}
|
|
||||||
public LicenseManager getLicenseManager() {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -1,140 +0,0 @@
|
|||||||
/*
|
|
||||||
*
|
|
||||||
* 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.internal;
|
|
||||||
|
|
||||||
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.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.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.device.mgt.core.util.LicenseManagerUtil;
|
|
||||||
import org.wso2.carbon.registry.core.service.RegistryService;
|
|
||||||
import org.wso2.carbon.user.core.service.RealmService;
|
|
||||||
|
|
||||||
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");
|
|
||||||
}
|
|
||||||
LicenseManager licenseManager = new LicenseManagerImpl();
|
|
||||||
LicenseManagementDataHolder.getInstance().setLicenseManager(licenseManager);
|
|
||||||
|
|
||||||
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. 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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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 e) {
|
|
||||||
String msg = "Error occurred while initializing license management core bundle";
|
|
||||||
log.error(msg, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets Realm Service.
|
|
||||||
*
|
|
||||||
* @param realmService An instance of RealmService
|
|
||||||
*/
|
|
||||||
protected void setRealmService(RealmService realmService) {
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Setting Realm Service in license management");
|
|
||||||
}
|
|
||||||
LicenseManagementDataHolder.getInstance().setRealmService(realmService);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Unsets Realm Service.
|
|
||||||
*
|
|
||||||
* @param realmService An instance of RealmService
|
|
||||||
*/
|
|
||||||
protected void unsetRealmService(RealmService realmService) {
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Un setting Realm Service in license management");
|
|
||||||
}
|
|
||||||
LicenseManagementDataHolder.getInstance().setRealmService(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets Registry Service.
|
|
||||||
*
|
|
||||||
* @param registryService An instance of RegistryService
|
|
||||||
*/
|
|
||||||
protected void setRegistryService(RegistryService 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) {
|
|
||||||
if (log.isDebugEnabled()) {
|
|
||||||
log.debug("Unsetting Registry Service");
|
|
||||||
}
|
|
||||||
LicenseManagementDataHolder.getInstance().setRegistryService(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public class LicenseManagementUtil {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,108 @@
|
|||||||
|
/*
|
||||||
|
* 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.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||||
|
import org.wso2.carbon.device.mgt.core.DeviceManagerImpl;
|
||||||
|
import org.wso2.carbon.device.mgt.core.config.license.License;
|
||||||
|
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.text.DateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Locale;
|
||||||
|
|
||||||
|
public class LicenseManagerImpl implements LicenseManager {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(DeviceManagerImpl.class);
|
||||||
|
private static final DateFormat format = new SimpleDateFormat("dd-mm-yyyy", Locale.ENGLISH);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public License getLicense(final String deviceType, final String languageCode) throws LicenseManagementException {
|
||||||
|
GenericArtifactManager artifactManager =
|
||||||
|
GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager();
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
if (artifacts == null || artifacts.length <= 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return this.populateLicense(artifacts[0]);
|
||||||
|
} catch (GovernanceException e) {
|
||||||
|
throw new LicenseManagementException("Error occurred while retrieving license corresponding to " +
|
||||||
|
"device type '" + deviceType + "'");
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new LicenseManagementException("Error occurred while parsing the ToDate/FromDate date string " +
|
||||||
|
"of the license configured upon the device type '" + deviceType + "'");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private License populateLicense(GenericArtifact artifact) throws GovernanceException, ParseException {
|
||||||
|
License license = new License();
|
||||||
|
license.setName(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.NAME));
|
||||||
|
license.setProvider(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.PROVIDER));
|
||||||
|
license.setVersion(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.VERSION));
|
||||||
|
license.setLanguage(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.LANGUAGE));
|
||||||
|
license.setText(artifact.getAttribute(DeviceManagementConstants.LicenseProperties.TEXT));
|
||||||
|
license.setValidFrom(format.parse(artifact.getAttribute(
|
||||||
|
DeviceManagementConstants.LicenseProperties.VALID_FROM)));
|
||||||
|
license.setValidTo(format.parse(artifact.getAttribute(
|
||||||
|
DeviceManagementConstants.LicenseProperties.VALID_TO)));
|
||||||
|
return license;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addLicense(License license) throws LicenseManagementException {
|
||||||
|
GenericArtifactManager artifactManager =
|
||||||
|
GenericArtifactManagerFactory.getTenantAwareGovernanceArtifactManager();
|
||||||
|
try {
|
||||||
|
GenericArtifact artifact =
|
||||||
|
artifactManager.newGovernanceArtifact(new QName("http://www.wso2.com",
|
||||||
|
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY));
|
||||||
|
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.VALID_TO,
|
||||||
|
license.getValidTo().toString());
|
||||||
|
artifact.setAttribute(DeviceManagementConstants.LicenseProperties.VALID_FROM,
|
||||||
|
license.getValidFrom().toString());
|
||||||
|
artifactManager.addGenericArtifact(artifact);
|
||||||
|
} catch (GovernanceException e) {
|
||||||
|
throw new LicenseManagementException("Error occurred while adding license artifact", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,57 @@
|
|||||||
|
/*
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
public class LicenseNotFoundException extends RuntimeException {
|
||||||
|
|
||||||
|
private static final long serialVersionUID = -8933146283800122680L;
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setErrorMessage(String errorMessage) {
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LicenseNotFoundException(String msg, Exception nestedEx) {
|
||||||
|
super(msg, nestedEx);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LicenseNotFoundException(String message, Throwable cause) {
|
||||||
|
super(message, cause);
|
||||||
|
setErrorMessage(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LicenseNotFoundException(String msg) {
|
||||||
|
super(msg);
|
||||||
|
setErrorMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LicenseNotFoundException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public LicenseNotFoundException(Throwable cause) {
|
||||||
|
super(cause);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,86 +0,0 @@
|
|||||||
/*
|
|
||||||
* *
|
|
||||||
* * 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in new issue