forked from community/device-mgt-core
parent
8662eb36e4
commit
31b931537f
@ -1,22 +1,72 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
* 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
|
||||
* 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
|
||||
* 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.
|
||||
* 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.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
|
||||
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;
|
||||
int tenantId;
|
||||
try {
|
||||
tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();
|
||||
registry =
|
||||
DeviceManagementDataHolder.getInstance().getRegistryService().getGovernanceSystemRegistry(
|
||||
tenantId);
|
||||
} catch (RegistryException e) {
|
||||
throw new LicenseManagementException("Error occurred while initializing tenant system registry " +
|
||||
"to be used to manipulate License artifacts", e);
|
||||
}
|
||||
|
||||
try {
|
||||
GenericArtifactManager artifactManager;
|
||||
synchronized (lock) {
|
||||
artifactManager =
|
||||
tenantArtifactManagers.get(tenantId);
|
||||
if (artifactManager == null) {
|
||||
/* Hack, to fix https://wso2.org/jira/browse/REGISTRY-2427 */
|
||||
//GovernanceUtils.loadGovernanceArtifacts((UserRegistry) registry);
|
||||
artifactManager =
|
||||
new GenericArtifactManager((org.wso2.carbon.registry.core.Registry) registry,
|
||||
DeviceManagementConstants.LicenseProperties.LICENSE_REGISTRY_KEY);
|
||||
tenantArtifactManagers.put(tenantId, artifactManager);
|
||||
}
|
||||
}
|
||||
return artifactManager;
|
||||
} catch (RegistryException e) {
|
||||
throw new LicenseManagementException("Error occurred while initializing GovernanceArtifactManager " +
|
||||
"associated with tenant '" + CarbonContext.getThreadLocalCarbonContext().getTenantDomain() + "'");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,57 +1,111 @@
|
||||
/*
|
||||
* Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
|
||||
* 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
|
||||
* 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
|
||||
* 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.
|
||||
* 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.core.DeviceManagerImpl;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.core.DeviceManagementServiceProviderImpl;
|
||||
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.internal.DeviceManagementDataHolder;
|
||||
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 {
|
||||
License deviceLicense = new License();
|
||||
LicenseConfig licenseConfig = DeviceManagementDataHolder.getInstance().getLicenseConfig();
|
||||
for (License license : licenseConfig.getLicenses()) {
|
||||
if ((deviceType.equals(license.getName())) &&
|
||||
(languageCode.equals(license.getLanguage()))) {
|
||||
deviceLicense = license;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return deviceLicense;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addLicense(License license) throws LicenseManagementException {
|
||||
|
||||
}
|
||||
private static Log log = LogFactory.getLog(DeviceManagementServiceProviderImpl.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 boolean addLicense(String deviceType, 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);
|
||||
return true;
|
||||
} catch (GovernanceException e) {
|
||||
throw new LicenseManagementException("Error occurred while adding license for device type " +
|
||||
deviceType + "'", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,58 @@
|
||||
CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE (
|
||||
ID INT auto_increment NOT NULL,
|
||||
NAME VARCHAR(300) NULL DEFAULT NULL,
|
||||
PRIMARY KEY (ID)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS DM_DEVICE (
|
||||
ID INTEGER auto_increment NOT NULL,
|
||||
DESCRIPTION TEXT NULL DEFAULT NULL,
|
||||
NAME VARCHAR(100) NULL DEFAULT NULL,
|
||||
DATE_OF_ENROLLMENT BIGINT NULL DEFAULT NULL,
|
||||
DATE_OF_LAST_UPDATE BIGINT NULL DEFAULT NULL,
|
||||
OWNERSHIP VARCHAR(45) NULL DEFAULT NULL,
|
||||
STATUS VARCHAR(15) NULL DEFAULT NULL,
|
||||
DEVICE_TYPE_ID INT(11) NULL DEFAULT NULL,
|
||||
DEVICE_IDENTIFICATION VARCHAR(300) NULL DEFAULT NULL,
|
||||
OWNER VARCHAR(45) NULL DEFAULT NULL,
|
||||
TENANT_ID INTEGER DEFAULT 0,
|
||||
PRIMARY KEY (ID),
|
||||
CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID )
|
||||
REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS DM_OPERATION (
|
||||
ID INTEGER AUTO_INCREMENT NOT NULL,
|
||||
TYPE VARCHAR(50) NOT NULL,
|
||||
CREATED_TIMESTAMP TIMESTAMP NOT NULL,
|
||||
RECEIVED_TIMESTAMP TIMESTAMP NULL,
|
||||
STATUS VARCHAR(50) NULL,
|
||||
PRIMARY KEY (ID)
|
||||
)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS DM_CONFIG_OPERATION (
|
||||
OPERATION_ID INTEGER NOT NULL,
|
||||
PRIMARY KEY (OPERATION_ID),
|
||||
CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES
|
||||
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
|
||||
)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS DM_COMMAND_OPERATION (
|
||||
OPERATION_ID INTEGER NOT NULL,
|
||||
ENABLED INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (OPERATION_ID),
|
||||
CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES
|
||||
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
|
||||
)
|
||||
|
||||
CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_MAPPING (
|
||||
ID INTEGER AUTO_INCREMENT NOT NULL,
|
||||
DEVICE_ID INTEGER NOT NULL,
|
||||
OPERATION_ID INTEGER NOT NULL,
|
||||
PRIMARY KEY (ID),
|
||||
CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (DEVICE_ID) REFERENCES
|
||||
DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
|
||||
CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES
|
||||
DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION
|
||||
)
|
||||
|
Loading…
Reference in new issue