diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/pom.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/pom.xml new file mode 100644 index 0000000000..3890b70b68 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/pom.xml @@ -0,0 +1,93 @@ + + + + + + + device-mgt-extensions + org.wso2.carbon.devicemgt + 1.2.6-SNAPSHOT + ../pom.xml + + + 4.0.0 + org.wso2.carbon.device.mgt.extensions.device.type.deployer + bundle + WSO2 Carbon - Device Type Deployer + WSO2 Carbon - Device Type Deployer Implementation + http://wso2.org + + + + org.apache.axis2.wso2 + axis2 + + + org.wso2.carbon + org.wso2.carbon.utils + + + org.eclipse.osgi + org.eclipse.osgi + + + org.eclipse.osgi + org.eclipse.osgi.services + + + commons-logging + commons-logging + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.common + + + org.wso2.carbon.devicemgt + org.wso2.carbon.device.mgt.extensions + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + ${project.artifactId} + ${project.artifactId} + ${carbon.device.mgt.version} + Device Type Deployer Bundle + + !org.wso2.carbon.device.mgt.extensions.push.notification.provider.internal, + org.wso2.carbon.device.mgt.extensions.device.type.deployer.* + + + + + DeviceTypeDeployer + + + + + + + diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeDeployer.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeDeployer.java new file mode 100644 index 0000000000..2ace9f4970 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypeDeployer.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer; + +import org.apache.axis2.context.ConfigurationContext; +import org.apache.axis2.deployment.AbstractDeployer; +import org.apache.axis2.deployment.DeploymentException; +import org.apache.axis2.deployment.repository.util.DeploymentFileData; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.w3c.dom.Document; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceManagementConfiguration; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.exception.DeviceTypeConfigurationException; +import org.wso2.carbon.device.mgt.extensions.push.notification.provider.internal.DeviceTypeManagementDataHolder; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.DeviceTypeConfigIdentifier; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.DeviceTypeManagerService; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.util.DeviceTypeConfigUtil; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.File; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + + +public class DeviceTypeDeployer extends AbstractDeployer { + + private static Log log = LogFactory.getLog(DeviceTypeDeployer.class); + private ConfigurationContext configurationContext; + protected Map deviceTypeServiceRegistrations = new ConcurrentHashMap(); + protected Map deviceTypeConfigurationDataMap = new ConcurrentHashMap(); + + @Override + public void init(ConfigurationContext configurationContext) { + this.configurationContext = configurationContext; + } + + @Override + public void setDirectory(String s) { + + } + + @Override + public void setExtension(String s) { + + } + + @Override + public void deploy(DeploymentFileData deploymentFileData) throws DeploymentException { + try { + DeviceManagementConfiguration deviceManagementConfiguration = getDeviceTypeConfiguration( + deploymentFileData.getFile().getAbsoluteFile()); + String deviceType = deviceManagementConfiguration.getDeviceType(); + String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true); + if (deviceType != null && !deviceType.isEmpty() && tenantDomain != null + && !tenantDomain.isEmpty()) { + DeviceTypeConfigIdentifier deviceTypeConfigIdentifier = new DeviceTypeConfigIdentifier(deviceType, + tenantDomain); + ServiceRegistration serviceRegistration = registerDeviceType(deviceTypeConfigIdentifier, + deviceManagementConfiguration); + this.deviceTypeServiceRegistrations.put(deploymentFileData.getAbsolutePath(), serviceRegistration); + this.deviceTypeConfigurationDataMap.put(deploymentFileData.getAbsolutePath(), + deviceTypeConfigIdentifier); + } + } catch (Throwable e) { + log.error("Cannot deploy deviceType : " + deploymentFileData.getName(), e); + throw new DeploymentException("Device type file " + deploymentFileData.getName() + " is not deployed ", e); + } + + } + + @Override + public void undeploy(String filePath) throws DeploymentException { + DeviceTypeConfigIdentifier deviceTypeConfigIdentifier = this.deviceTypeConfigurationDataMap.get(filePath); + unregisterDeviceType(filePath); + this.deviceTypeConfigurationDataMap.remove(filePath); + log.info("Device Type undeployed successfully : " + deviceTypeConfigIdentifier.getDeviceType() + " for tenant " + + deviceTypeConfigIdentifier.getTenantDomain()); + } + + private DeviceManagementConfiguration getDeviceTypeConfiguration(File configurationFile) + throws DeviceTypeConfigurationException { + try { + Document doc = DeviceTypeConfigUtil.convertToDocument(configurationFile); + + /* Un-marshaling Webapp Authenticator configuration */ + JAXBContext ctx = JAXBContext.newInstance(DeviceManagementConfiguration.class); + Unmarshaller unmarshaller = ctx.createUnmarshaller(); + //unmarshaller.setSchema(getSchema()); + return (DeviceManagementConfiguration) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + throw new DeviceTypeConfigurationException("Error occurred while un-marshalling the file " + + configurationFile.getAbsolutePath(), e); + } + } + + private ServiceRegistration registerDeviceType(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, + DeviceManagementConfiguration deviceManagementConfiguration) { + DeviceTypeManagerService deviceTypeManagerService = new DeviceTypeManagerService(deviceTypeConfigIdentifier, + deviceManagementConfiguration); + BundleContext bundleContext = DeviceTypeManagementDataHolder.getInstance().getBundleContext(); + return bundleContext.registerService(DeviceManagementService.class.getName(), deviceTypeManagerService, null); + } + + private void unregisterDeviceType(String filePath) { + if (log.isDebugEnabled()) { + log.debug("De-activating Virtual Firealarm Device Management Service Component"); + } + try { + if (this.deviceTypeServiceRegistrations.get(filePath) != null) { + this.deviceTypeServiceRegistrations.get(filePath).unregister(); + } + if (log.isDebugEnabled()) { + log.debug(" Device Management Service has been successfully de-activated"); + } + } catch (Throwable e) { + log.error("Error occurred while de-activating Deactivating device management service.", e); + } + deviceTypeServiceRegistrations.remove(filePath); + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypePluginConstants.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypePluginConstants.java new file mode 100644 index 0000000000..cf755312ad --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/DeviceTypePluginConstants.java @@ -0,0 +1,11 @@ +package org.wso2.carbon.device.mgt.extensions.device.type.deployer; + +public class DeviceTypePluginConstants { + public static final String DB_SCRIPTS_FOLDER = "cdm"; + public static final String CONFIG_REGISTRY_ROOT = "/_system/config"; + + public static final String MEDIA_TYPE_XML = "application/xml"; + public static final String CHARSET_UTF8 = "UTF8"; + public static final String LANGUAGE_CODE_ENGLISH_US = "en_US"; + public static final String LANGUAGE_CODE_ENGLISH_UK = "en_UK"; +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DataSourceConfig.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DataSourceConfig.java new file mode 100644 index 0000000000..d51835aef3 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DataSourceConfig.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014, 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.extensions.device.type.deployer.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding device type configuration and parsing with JAXB. + */ +@XmlRootElement(name = "DataSourceConfiguration") +public class DataSourceConfig { + + private JNDILookupDefinition jndiLookupDefinition; + + @XmlElement(name = "JndiLookupDefinition", required = true) + public JNDILookupDefinition getJndiLookupDefinition() { + return jndiLookupDefinition; + } + + public void setJndiLookupDefinition(JNDILookupDefinition jndiLookupDefinition) { + this.jndiLookupDefinition = jndiLookupDefinition; + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DeviceManagementConfigRepository.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DeviceManagementConfigRepository.java new file mode 100644 index 0000000000..faafcfb02e --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DeviceManagementConfigRepository.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2014, 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.extensions.device.type.deployer.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding management repository data. + */ +@XmlRootElement(name = "ManagementRepository") +public class DeviceManagementConfigRepository { + + private DataSourceConfig dataSourceConfig; + private ProvisioningConfig provisioningConfig; + + @XmlElement(name = "DataSourceConfiguration", required = true) + public DataSourceConfig getDataSourceConfig() { + return dataSourceConfig; + } + + public void setDataSourceConfig(DataSourceConfig dataSourceConfig) { + this.dataSourceConfig = dataSourceConfig; + } + + @XmlElement(name = "ProvisioningConfig", required = true) + public ProvisioningConfig getProvisioningConfig() { + return provisioningConfig; + } + + public void setProvisioningConfig( + ProvisioningConfig provisioningConfig) { + this.provisioningConfig = provisioningConfig; + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DeviceManagementConfiguration.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DeviceManagementConfiguration.java new file mode 100644 index 0000000000..6f43af0ee5 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/DeviceManagementConfiguration.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.config; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "DeviceManagementConfiguration") +public class DeviceManagementConfiguration { + + private DeviceManagementConfigRepository deviceManagementConfigRepository; + private PushNotificationConfig pushNotificationConfig; + private String deviceType; + + private static final Log log = LogFactory.getLog(DeviceManagementConfiguration.class); + + private DeviceManagementConfiguration() { + } + + @XmlElement(name = "DeviceType", required = false) + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + @XmlElement(name = "ManagementRepository", required = true) + public DeviceManagementConfigRepository getDeviceManagementConfigRepository() { + return deviceManagementConfigRepository; + } + + public void setDeviceManagementConfigRepository(DeviceManagementConfigRepository deviceManagementConfigRepository) { + this.deviceManagementConfigRepository = deviceManagementConfigRepository; + } + + @XmlElement(name = "PushNotificationConfiguration", required = false) + public PushNotificationConfig getPushNotificationConfig() { + return pushNotificationConfig; + } + + public void setPushNotificationConfig(PushNotificationConfig pushNotificationConfig) { + this.pushNotificationConfig = pushNotificationConfig; + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/FeaturesConfig.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/FeaturesConfig.java new file mode 100644 index 0000000000..b7baf06489 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/FeaturesConfig.java @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014, 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.extensions.device.type.deployer.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding device type configuration and parsing with JAXB. + */ +@XmlRootElement(name = "FeaturesConfig") +public class FeaturesConfig { + + private JNDILookupDefinition jndiLookupDefinition; + + @XmlElement(name = "JndiLookupDefinition", required = true) + public JNDILookupDefinition getJndiLookupDefinition() { + return jndiLookupDefinition; + } + + public void setJndiLookupDefinition(JNDILookupDefinition jndiLookupDefinition) { + this.jndiLookupDefinition = jndiLookupDefinition; + } + +} \ No newline at end of file diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/JNDILookupDefinition.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/JNDILookupDefinition.java new file mode 100644 index 0000000000..19e5ff51c4 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/JNDILookupDefinition.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2014, 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.extensions.device.type.deployer.config; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; +import java.util.List; + +/** + * Class for hold JndiLookupDefinition parsing with JAXB. + */ +@XmlRootElement(name = "JndiLookupDefinition") +public class JNDILookupDefinition { + + private String jndiName; + private List jndiProperties; + + @XmlElement(name = "Name", required = false) + public String getJndiName() { + return jndiName; + } + + public void setJndiName(String jndiName) { + this.jndiName = jndiName; + } + + @XmlElementWrapper(name = "Environment", required = false) + @XmlElement(name = "Property", nillable = false) + public List getJndiProperties() { + return jndiProperties; + } + + public void setJndiProperties(List jndiProperties) { + this.jndiProperties = jndiProperties; + } + + @XmlRootElement(name = "Property") + public static class JNDIProperty { + + private String name; + + private String value; + + @XmlAttribute(name = "Name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + } + +} + diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/ProvisioningConfig.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/ProvisioningConfig.java new file mode 100644 index 0000000000..4ebf232a44 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/ProvisioningConfig.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014, 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.extensions.device.type.deployer.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * Class for holding device type configuration and parsing with JAXB. + */ +@XmlRootElement(name = "ProvisioningConfig") +public class ProvisioningConfig { + + private boolean sharedWithAllTenants; + + @XmlElement(name = "SharedWithAllTenants", required = true) + public boolean isSharedWithAllTenants() { + return sharedWithAllTenants; + } + + public void setSharedWithAllTenants(boolean sharedWithAllTenants) { + this.sharedWithAllTenants = sharedWithAllTenants; + } +} \ No newline at end of file diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/PushNotificationConfig.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/PushNotificationConfig.java new file mode 100644 index 0000000000..94955ff25f --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/PushNotificationConfig.java @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.config; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; +import java.util.List; + +@XmlRootElement(name = "PushNotificationConfiguration") +public class PushNotificationConfig { + + private String pushNotificationProvider; + private List properties; + + @XmlElementWrapper(name = "Properties", required = true) + @XmlElement(name = "Property", required = true) + public List getProperties() { + return properties; + } + + public void setProperties(List properties) { + this.properties = properties; + } + + @XmlElement(name = "PushNotificationProvider", required = true) + public String getPushNotificationProvider() { + return pushNotificationProvider; + } + + public void setPushNotificationProvider(String pushNotificationProvider) { + this.pushNotificationProvider = pushNotificationProvider; + } + + @XmlRootElement(name = "Property") + public static class Property { + + private String name; + private String value; + + @XmlAttribute(name = "Name", required = true) + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/exception/DeviceTypeConfigurationException.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/exception/DeviceTypeConfigurationException.java new file mode 100644 index 0000000000..70378fad0f --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/exception/DeviceTypeConfigurationException.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.config.exception; + +public class DeviceTypeConfigurationException extends Exception { + + private static final long serialVersionUID = -3151279431229070297L; + + public DeviceTypeConfigurationException(int errorCode, String message) { + super(message); + } + + public DeviceTypeConfigurationException(int errorCode, String message, Throwable cause) { + super(message, cause); + } + + public DeviceTypeConfigurationException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public DeviceTypeConfigurationException(String message, Throwable cause) { + super(message, cause); + } + + public DeviceTypeConfigurationException(String msg) { + super(msg); + } + + public DeviceTypeConfigurationException() { + super(); + } + + public DeviceTypeConfigurationException(Throwable cause) { + super(cause); + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/exception/InvalidConfigurationStateException.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/exception/InvalidConfigurationStateException.java new file mode 100644 index 0000000000..c3d7887b7d --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/config/exception/InvalidConfigurationStateException.java @@ -0,0 +1,53 @@ +/* + * 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.extensions.device.type.deployer.config.exception; + +public class InvalidConfigurationStateException extends RuntimeException { + + private static final long serialVersionUID = -3151279411229070297L; + + public InvalidConfigurationStateException(int errorCode, String message) { + super(message); + } + + public InvalidConfigurationStateException(int errorCode, String message, Throwable cause) { + super(message, cause); + } + + public InvalidConfigurationStateException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public InvalidConfigurationStateException(String message, Throwable cause) { + super(message, cause); + } + + public InvalidConfigurationStateException(String msg) { + super(msg); + } + + public InvalidConfigurationStateException() { + super(); + } + + public InvalidConfigurationStateException(Throwable cause) { + super(cause); + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/exception/DeviceTypeMgtPluginException.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/exception/DeviceTypeMgtPluginException.java new file mode 100644 index 0000000000..7319aabf85 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/exception/DeviceTypeMgtPluginException.java @@ -0,0 +1,43 @@ +/* + * 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.extensions.device.type.deployer.exception; + + +public class DeviceTypeMgtPluginException extends Exception{ + + public DeviceTypeMgtPluginException(String msg, Exception nestedEx) { + super(msg, nestedEx); + } + + public DeviceTypeMgtPluginException(String message, Throwable cause) { + super(message, cause); + } + + public DeviceTypeMgtPluginException(String msg) { + super(msg); + } + + public DeviceTypeMgtPluginException() { + super(); + } + + public DeviceTypeMgtPluginException(Throwable cause) { + super(cause); + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/internal/DeviceTypeManagementDataHolder.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/internal/DeviceTypeManagementDataHolder.java new file mode 100644 index 0000000000..7e3e1cb1e2 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/internal/DeviceTypeManagementDataHolder.java @@ -0,0 +1,62 @@ +/* + * 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.extensions.device.type.deployer.internal; + +import org.osgi.framework.BundleContext; +import org.wso2.carbon.registry.core.service.RegistryService; +import org.wso2.carbon.utils.ConfigurationContextService; + +public class DeviceTypeManagementDataHolder { + + private ConfigurationContextService configurationContextService; + private BundleContext bundleContext; + private RegistryService registryService; + + private static DeviceTypeManagementDataHolder thisInstance = new DeviceTypeManagementDataHolder(); + + private DeviceTypeManagementDataHolder() {} + + public static DeviceTypeManagementDataHolder getInstance() { + return thisInstance; + } + + public ConfigurationContextService getConfigurationContextService() { + return configurationContextService; + } + + public void setConfigurationContextService(ConfigurationContextService configurationContextService) { + this.configurationContextService = configurationContextService; + } + + public BundleContext getBundleContext() { + return bundleContext; + } + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + + public RegistryService getRegistryService() { + return registryService; + } + + public void setRegistryService(RegistryService registryService) { + this.registryService = registryService; + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/internal/DeviceTypeManagementServiceComponent.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/internal/DeviceTypeManagementServiceComponent.java new file mode 100644 index 0000000000..9da156f3e0 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/internal/DeviceTypeManagementServiceComponent.java @@ -0,0 +1,83 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.registry.core.service.RegistryService; +import org.wso2.carbon.utils.ConfigurationContextService; + +/** + * @scr.component name="org.wso2.carbon.device.mgt.iot.internal.DeviceTypeManagementServiceComponent" + * immediate="true" + * @scr.reference name="config.context.service" + * interface="org.wso2.carbon.utils.ConfigurationContextService" + * cardinality="0..1" + * policy="dynamic" + * bind="setConfigurationContextService" + * unbind="unsetConfigurationContextService" + * @scr.reference name="registry.service" + * interface="org.wso2.carbon.registry.core.service.RegistryService" cardinality="0..1" + * policy="dynamic" bind="setRegistryService" unbind="unsetRegistryService" + */ +public class DeviceTypeManagementServiceComponent { + + private static final Log log = LogFactory.getLog(DeviceTypeManagementServiceComponent.class); + + protected void activate(ComponentContext ctx) { + if (log.isDebugEnabled()) { + log.debug("Activating DeviceType Deployer Service Component"); + } + DeviceTypeManagementDataHolder.getInstance().setBundleContext(ctx.getBundleContext()); + } + + protected void deactivate(ComponentContext ctx) { + if (log.isDebugEnabled()) { + log.debug("De-activating DeviceType Deployer Service Component"); + } + } + + protected void setConfigurationContextService(ConfigurationContextService configurationContextService) { + if (log.isDebugEnabled()) { + log.debug("Setting ConfigurationContextService"); + } + + DeviceTypeManagementDataHolder.getInstance().setConfigurationContextService(configurationContextService); + + } + + protected void unsetConfigurationContextService(ConfigurationContextService configurationContextService) { + if (log.isDebugEnabled()) { + log.debug("Un-setting ConfigurationContextService"); + } + DeviceTypeManagementDataHolder.getInstance().setConfigurationContextService(null); + } + + protected void setRegistryService(RegistryService registryService) { + if (log.isDebugEnabled()) { + log.debug("RegistryService acquired"); + } + DeviceTypeManagementDataHolder.getInstance().setRegistryService(registryService); + } + + protected void unsetRegistryService(RegistryService registryService) { + DeviceTypeManagementDataHolder.getInstance().setRegistryService(null); + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeConfigIdentifier.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeConfigIdentifier.java new file mode 100644 index 0000000000..a9288dd731 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeConfigIdentifier.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.template; + +import java.io.Serializable; + +/** + * This class holds key for the configuration of the device type and its provider tenant. + */ +public class DeviceTypeConfigIdentifier implements Serializable { + + private String deviceType; + private String tenantDomain; + + public DeviceTypeConfigIdentifier(String deviceType, String tenantDomain) { + this.deviceType = deviceType; + this.tenantDomain = tenantDomain; + } + + @Override + public int hashCode() { + int result = this.deviceType.hashCode(); + result = 31 * result + ("@" + this.tenantDomain).hashCode(); + return result; + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof DeviceTypeConfigIdentifier) && deviceType.equals( + ((DeviceTypeConfigIdentifier) obj).deviceType) && tenantDomain.equals( + ((DeviceTypeConfigIdentifier) obj).tenantDomain); + } + + public String getDeviceType() { + return deviceType; + } + + public String getTenantDomain() { + return tenantDomain; + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java new file mode 100644 index 0000000000..df81c24577 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java @@ -0,0 +1,320 @@ +package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceManager; +import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; +import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.DeviceTypePluginConstants; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceManagementConfiguration; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.dao.DeviceTypePluginDAOManager; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.feature.AnnotationBasedFeatureManager; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.util.DeviceTypeUtils; +import org.wso2.carbon.device.mgt.extensions.license.mgt.registry.RegistryBasedLicenseManager; +import org.wso2.carbon.registry.api.RegistryException; +import org.wso2.carbon.registry.api.Resource; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.Unmarshaller; +import java.io.StringReader; +import java.io.StringWriter; +import java.nio.charset.Charset; +import java.util.List; + +public class DeviceTypeManager implements DeviceManager { + + private static final Log log = LogFactory.getLog(DeviceTypeManager.class); + private String deviceType; + private DeviceTypePluginDAOManager deviceTypePluginDAOManager; + private LicenseManager licenseManager; + + private FeatureManager featureManager; + public DeviceTypeManager(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, + DeviceManagementConfiguration deviceManagementConfiguration) { + deviceTypePluginDAOManager = new DeviceTypePluginDAOManager(deviceManagementConfiguration); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); + boolean isSharedWithAllTenants = + deviceManagementConfiguration.getDeviceManagementConfigRepository().getProvisioningConfig() + .isSharedWithAllTenants(); + DeviceTypeIdentifier deviceTypeIdentifier; + deviceType = deviceTypeConfigIdentifier.getDeviceType(); + if (isSharedWithAllTenants) { + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType); + } else { + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType, tenantId); + } + + featureManager = new AnnotationBasedFeatureManager(deviceTypeIdentifier); + + this.licenseManager = new RegistryBasedLicenseManager(); + License defaultLicense; + + try { + if (licenseManager.getLicense(deviceType, DeviceTypePluginConstants.LANGUAGE_CODE_ENGLISH_US) == null) { + defaultLicense = null; + licenseManager.addLicense(deviceType, defaultLicense); + } + featureManager.addSupportedFeaturesToDB(); + } catch (LicenseManagementException e) { + log.error("Error occurred while adding default license for Android devices", e); + } catch (DeviceManagementException e) { + log.error("Error occurred while adding supported device features for Android platform", e); + } + } + + @Override + public FeatureManager getFeatureManager() { + return featureManager; + } + + @Override + public boolean saveConfiguration(PlatformConfiguration tenantConfiguration) + throws DeviceManagementException { + try { + if (log.isDebugEnabled()) { + log.debug("Persisting " + deviceType + " configurations in Registry"); + } + StringWriter writer = new StringWriter(); + JAXBContext context = JAXBContext.newInstance(PlatformConfiguration.class); + Marshaller marshaller = context.createMarshaller(); + marshaller.marshal(tenantConfiguration, writer); + + Resource resource = DeviceTypeUtils.getConfigurationRegistry().newResource(); + resource.setContent(writer.toString()); + resource.setMediaType(DeviceTypePluginConstants.MEDIA_TYPE_XML); + DeviceTypeUtils.putRegistryResource(deviceType, resource); + return true; + } catch (DeviceTypeMgtPluginException e) { + throw new DeviceManagementException( + "Error occurred while retrieving the Registry instance : " + e.getMessage(), e); + } catch (RegistryException e) { + throw new DeviceManagementException( + "Error occurred while persisting the Registry resource of " + deviceType + " Configuration : " + + e.getMessage(), e); + } catch (JAXBException e) { + throw new DeviceManagementException( + "Error occurred while parsing the " + deviceType + " configuration : " + e.getMessage(), e); + } + } + + @Override + public PlatformConfiguration getConfiguration() throws DeviceManagementException { + Resource resource; + try { + resource = DeviceTypeUtils.getRegistryResource(deviceType); + if (resource != null) { + JAXBContext context = JAXBContext.newInstance(PlatformConfiguration.class); + Unmarshaller unmarshaller = context.createUnmarshaller(); + return (PlatformConfiguration) unmarshaller.unmarshal( + new StringReader(new String((byte[]) resource.getContent(), Charset. + forName(DeviceTypePluginConstants.CHARSET_UTF8)))); + } + return null; + } catch (DeviceTypeMgtPluginException e) { + throw new DeviceManagementException( + "Error occurred while retrieving the Registry instance : " + e.getMessage(), e); + } catch (JAXBException e) { + throw new DeviceManagementException( + "Error occurred while parsing the " + deviceType + " configuration : " + e.getMessage(), e); + } catch (RegistryException e) { + throw new DeviceManagementException( + "Error occurred while retrieving the Registry resource of " + deviceType + " Configuration : " + + e.getMessage(), e); + } + } + + @Override + public boolean enrollDevice(Device device) throws DeviceManagementException { + boolean status = false; + boolean isEnrolled = this.isEnrolled( + new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + try { + if (log.isDebugEnabled()) { + log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier()); + } + if (isEnrolled) { + this.modifyEnrollment(device); + } else { + deviceTypePluginDAOManager.beginTransaction(); + status = deviceTypePluginDAOManager.getDeviceDAO().addDevice(device); + deviceTypePluginDAOManager.commitTransaction(); + } + } catch (DeviceTypeMgtPluginException e) { + try { + deviceTypePluginDAOManager.rollbackTransaction(); + } catch (DeviceTypeMgtPluginException ex) { + String msg = "Error occurred while roll back the device enrol transaction :" + + device.toString(); + log.warn(msg, ex); + } + String msg = "Error while enrolling the " + deviceType + " device : " + device.getDeviceIdentifier(); + throw new DeviceManagementException(msg, e); + } + return status; + } + + @Override + public boolean modifyEnrollment(Device device) throws DeviceManagementException { + boolean status; + try { + if (log.isDebugEnabled()) { + log.debug("Modifying the Android device enrollment data"); + } + deviceTypePluginDAOManager.beginTransaction(); + status = deviceTypePluginDAOManager.getDeviceDAO().updateDevice(device); + deviceTypePluginDAOManager.commitTransaction(); + } catch (DeviceTypeMgtPluginException e) { + try { + deviceTypePluginDAOManager.rollbackTransaction(); + } catch (DeviceTypeMgtPluginException mobileDAOEx) { + String msg = "Error occurred while roll back the update device transaction :" + + device.toString(); + log.warn(msg, mobileDAOEx); + } + String msg = "Error while updating the enrollment of the " + deviceType + " device : " + + device.getDeviceIdentifier(); + throw new DeviceManagementException(msg, e); + } + return status; + } + + @Override + public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + //Here we don't have anything specific to do. Hence returning. + return true; + } + + @Override + public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { + boolean isEnrolled = false; + try { + if (log.isDebugEnabled()) { + log.debug("Checking the enrollment of Android device : " + deviceId.getId()); + } + Device device = + deviceTypePluginDAOManager.getDeviceDAO().getDevice(deviceId.getId()); + if (device != null) { + isEnrolled = true; + } + } catch (DeviceTypeMgtPluginException e) { + String msg = "Error while checking the enrollment status of " + deviceType + " device : " + + deviceId.getId(); + throw new DeviceManagementException(msg, e); + } + return isEnrolled; + } + + @Override + public boolean isActive(DeviceIdentifier deviceId) throws DeviceManagementException { + return true; + } + + @Override + public boolean setActive(DeviceIdentifier deviceId, boolean status) + throws DeviceManagementException { + return true; + } + + @Override + public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + Device device; + try { + if (log.isDebugEnabled()) { + log.debug("Getting the details of " + deviceType + " device : '" + deviceId.getId() + "'"); + } + device = deviceTypePluginDAOManager.getDeviceDAO().getDevice(deviceId.getId()); + } catch (DeviceTypeMgtPluginException e) { + throw new DeviceManagementException( + "Error occurred while fetching the " + deviceType + " device: '" + deviceId.getId() + "'", e); + } + return device; + } + + @Override + public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) + throws DeviceManagementException { + return true; + } + + @Override + public boolean isClaimable(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { + return false; + } + + @Override + public boolean setStatus(DeviceIdentifier deviceIdentifier, String currentUser, + EnrolmentInfo.Status status) throws DeviceManagementException { + return false; + } + + @Override + public License getLicense(String languageCode) throws LicenseManagementException { + return licenseManager.getLicense(deviceType, languageCode); + } + + @Override + public void addLicense(License license) throws LicenseManagementException { + licenseManager.addLicense(deviceType, license); + } + + @Override + public boolean requireDeviceAuthorization() { + return true; + } + + @Override + public boolean updateDeviceInfo(DeviceIdentifier deviceIdentifier, Device device) + throws DeviceManagementException { + boolean status; + Device existingDevice = this.getDevice(deviceIdentifier); + existingDevice.setProperties(device.getProperties()); + + try { + if (log.isDebugEnabled()) { + log.debug( + "updating the details of " + deviceType + " device : " + device.getDeviceIdentifier()); + } + deviceTypePluginDAOManager.beginTransaction(); + status = deviceTypePluginDAOManager.getDeviceDAO().updateDevice(existingDevice); + deviceTypePluginDAOManager.commitTransaction(); + } catch (DeviceTypeMgtPluginException e) { + try { + deviceTypePluginDAOManager.rollbackTransaction(); + } catch (DeviceTypeMgtPluginException e1) { + log.warn("Error occurred while roll back the update device info transaction : '" + + device.toString() + "'", e1); + } + throw new DeviceManagementException( + "Error occurred while updating the " + deviceType + " device: '" + + device.getDeviceIdentifier() + "'", e); + } + return status; + } + + @Override + public List getAllDevices() throws DeviceManagementException { + List devices = null; + try { + if (log.isDebugEnabled()) { + log.debug("Fetching the details of all " + deviceType + " devices"); + } + devices = deviceTypePluginDAOManager.getDeviceDAO().getAllDevices(); + } catch (DeviceTypeMgtPluginException e) { + throw new DeviceManagementException("Error occurred while fetching all " + deviceType + " devices", e); + } + return devices; + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java new file mode 100644 index 0000000000..117fc8b0e2 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManagerService.java @@ -0,0 +1,113 @@ +package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceManager; +import org.wso2.carbon.device.mgt.common.ProvisioningConfig; +import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; +import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration; +import org.wso2.carbon.device.mgt.common.push.notification.PushNotificationConfig; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceManagementConfiguration; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class DeviceTypeManagerService implements DeviceManagementService { + + private static final Log log = LogFactory.getLog(DeviceTypeManagerService.class); + + private DeviceManager deviceManager; + private PushNotificationConfig pushNotificationConfig; + private ProvisioningConfig provisioningConfig; + private String type; + + public DeviceTypeManagerService(DeviceTypeConfigIdentifier deviceTypeConfigIdentifier, + DeviceManagementConfiguration deviceManagementConfiguration) { + this.setProvisioningConfig(deviceTypeConfigIdentifier.getTenantDomain(), deviceManagementConfiguration); + this.deviceManager = new DeviceTypeManager(deviceTypeConfigIdentifier, deviceManagementConfiguration); + this.setType(deviceManagementConfiguration); + this.populatePushNotificationConfig(deviceManagementConfiguration); + + } + + @Override + public String getType() { + return type; + } + + @Override + public void init() throws DeviceManagementException { + } + + private void populatePushNotificationConfig(DeviceManagementConfiguration deviceManagementConfiguration) { + org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.PushNotificationConfig sourceConfig = + deviceManagementConfiguration.getPushNotificationConfig(); + if (sourceConfig != null) { + if (true) { + Map staticProps = new HashMap<>(); + for (org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.PushNotificationConfig.Property + property : sourceConfig.getProperties()) { + staticProps.put(property.getName(), property.getValue()); + } + pushNotificationConfig = new PushNotificationConfig(sourceConfig.getPushNotificationProvider(), + staticProps); + } else { + try { + PlatformConfiguration deviceTypeConfig = deviceManager.getConfiguration(); + if (deviceTypeConfig != null) { + List configuration = deviceTypeConfig.getConfiguration(); + if (configuration.size() > 0) { + Map properties = this.getConfigProperty(configuration); + pushNotificationConfig = new PushNotificationConfig( + sourceConfig.getPushNotificationProvider(), properties); + } + } + } catch (DeviceManagementException e) { + log.error("Unable to get the " + type + " platform configuration from registry."); + } + } + } + } + + @Override + public DeviceManager getDeviceManager() { + return deviceManager; + } + + @Override + public ApplicationManager getApplicationManager() { + return null; + } + + @Override + public ProvisioningConfig getProvisioningConfig() { + return provisioningConfig; + } + + @Override + public PushNotificationConfig getPushNotificationConfig() { + return pushNotificationConfig; + } + + private void setProvisioningConfig(String tenantDomain, DeviceManagementConfiguration deviceManagementConfiguration) { + boolean sharedWithAllTenants = deviceManagementConfiguration + .getDeviceManagementConfigRepository().getProvisioningConfig().isSharedWithAllTenants(); + provisioningConfig = new ProvisioningConfig(tenantDomain, sharedWithAllTenants); + } + + private void setType(DeviceManagementConfiguration deviceManagementConfiguration) { + type = deviceManagementConfiguration.getDeviceType(); + } + + private Map getConfigProperty(List configs) { + Map propertMap = new HashMap<>(); + for (ConfigurationEntry entry : configs) { + propertMap.put(entry.getName(), entry.getValue().toString()); + } + return propertMap; + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/dao/DeviceTypePluginDAO.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/dao/DeviceTypePluginDAO.java new file mode 100644 index 0000000000..2996205cf5 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/dao/DeviceTypePluginDAO.java @@ -0,0 +1,199 @@ +/* + * 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.extensions.device.type.deployer.template.dao; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceManagementConfiguration; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.util.DeviceTypeUtils; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +/** + * Implements CRUD for Devices. + */ +public class DeviceTypePluginDAO { + + private static final Log log = LogFactory.getLog(DeviceTypePluginDAO.class); + private DeviceTypePluginDAOManager deviceTypePluginDAOManager; + public DeviceTypePluginDAO(DeviceManagementConfiguration deviceManagementConfiguration) { + deviceTypePluginDAOManager = new DeviceTypePluginDAOManager(deviceManagementConfiguration); + } + + public Device getDevice(String deviceId) throws DeviceTypeMgtPluginException { + Connection conn = null; + PreparedStatement stmt = null; + Device device = null; + ResultSet resultSet = null; + try { + conn = deviceTypePluginDAOManager.getConnection(); + String selectDBQuery = + "SELECT VIRTUAL_FIREALARM_DEVICE_ID, DEVICE_NAME" + + " FROM VIRTUAL_FIREALARM_DEVICE WHERE VIRTUAL_FIREALARM_DEVICE_ID = ?"; + stmt = conn.prepareStatement(selectDBQuery); + stmt.setString(1, deviceId); + resultSet = stmt.executeQuery(); + + if (resultSet.next()) { + device = new Device(); + if (log.isDebugEnabled()) { + log.debug("Virtual Firealarm device " + deviceId + " data has been fetched from " + + "Virtual Firealarm database."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while fetching Virtual Firealarm device : '" + deviceId + "'"; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); + } finally { + DeviceTypeUtils.cleanupResources(stmt, resultSet); + deviceTypePluginDAOManager.closeConnection(); + } + + return device; + } + + public boolean addDevice(Device device) throws DeviceTypeMgtPluginException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = deviceTypePluginDAOManager.getConnection(); + String createDBQuery = + "INSERT INTO VIRTUAL_FIREALARM_DEVICE(VIRTUAL_FIREALARM_DEVICE_ID, DEVICE_NAME) VALUES (?, ?)"; + + stmt = conn.prepareStatement(createDBQuery); + stmt.setString(1, device.getDeviceIdentifier()); + stmt.setString(2, device.getName()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Virtual Firealarm device " + device.getDeviceIdentifier() + " data has been" + + " added to the Virtual Firealarm database."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while adding the Virtual Firealarm device '" + + device.getDeviceIdentifier() + "' to the Virtual Firealarm db."; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); + } finally { + DeviceTypeUtils.cleanupResources(stmt, null); + } + return status; + } + + public boolean updateDevice(Device device) throws DeviceTypeMgtPluginException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = DeviceTypePluginDAOManager.getConnection(); + String updateDBQuery = + "UPDATE VIRTUAL_FIREALARM_DEVICE SET DEVICE_NAME = ? WHERE VIRTUAL_FIREALARM_DEVICE_ID = ?"; + + stmt = conn.prepareStatement(updateDBQuery); + stmt.setString(1, device.getName()); + stmt.setString(2, device.getDeviceIdentifier()); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Virtualm Firealarm device " + device.getDeviceIdentifier() + " data has been" + + " modified."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while modifying the Virtual Firealarm device '" + + device.getDeviceIdentifier() + "' data."; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); + } finally { + DeviceTypeUtils.cleanupResources(stmt, null); + } + return status; + } + + public boolean deleteDevice(String iotDeviceId) throws DeviceTypeMgtPluginException { + boolean status = false; + Connection conn = null; + PreparedStatement stmt = null; + try { + conn = deviceTypePluginDAOManager.getConnection(); + String deleteDBQuery = "DELETE FROM VIRTUAL_FIREALARM_DEVICE WHERE VIRTUAL_FIREALARM_DEVICE_ID = ?"; + stmt = conn.prepareStatement(deleteDBQuery); + stmt.setString(1, iotDeviceId); + int rows = stmt.executeUpdate(); + if (rows > 0) { + status = true; + if (log.isDebugEnabled()) { + log.debug("Virtual Firealarm device " + iotDeviceId + " data has deleted" + + " from the Virtual Firealarm database."); + } + } + } catch (SQLException e) { + String msg = "Error occurred while deleting Virtual Firealarm device " + iotDeviceId; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); + } finally { + DeviceTypeUtils.cleanupResources(stmt, null); + } + return status; + } + + public List getAllDevices() throws DeviceTypeMgtPluginException { + Connection conn; + PreparedStatement stmt = null; + ResultSet resultSet = null; + Device device; + List devices = new ArrayList<>(); + try { + conn = deviceTypePluginDAOManager.getConnection(); + String selectDBQuery = + "SELECT VIRTUAL_FIREALARM_DEVICE_ID, DEVICE_NAME FROM VIRTUAL_FIREALARM_DEVICE"; + stmt = conn.prepareStatement(selectDBQuery); + resultSet = stmt.executeQuery(); + while (resultSet.next()) { + device = new Device(); + device.setDeviceIdentifier(resultSet.getString(VirtualFireAlarmConstants.DEVICE_PLUGIN_DEVICE_ID)); + device.setName(resultSet.getString(VirtualFireAlarmConstants.DEVICE_PLUGIN_DEVICE_NAME)); + devices.add(device); + } + if (log.isDebugEnabled()) { + log.debug("All Virtual Firealarm device details have fetched from Firealarm database."); + } + return devices; + } catch (SQLException e) { + String msg = "Error occurred while fetching all Virtual Firealarm device data'"; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); + } finally { + DeviceTypeUtils.cleanupResources(stmt, resultSet); + deviceTypePluginDAOManager.closeConnection(); + } + } +} \ No newline at end of file diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/dao/DeviceTypePluginDAOManager.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/dao/DeviceTypePluginDAOManager.java new file mode 100644 index 0000000000..1e7b23b3bc --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/dao/DeviceTypePluginDAOManager.java @@ -0,0 +1,129 @@ +/* + * 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.extensions.device.type.deployer.template.dao; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceManagementConfiguration; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +public class DeviceTypePluginDAOManager { + + private static final Log log = LogFactory.getLog(DeviceTypePluginDAOManager.class); + private DataSource dataSource; + private ThreadLocal currentConnection = new ThreadLocal(); + private DeviceTypePluginDAO deviceTypePluginDAO; + + public DeviceTypePluginDAOManager(DeviceManagementConfiguration deviceManagementConfiguration) { + initDAO(deviceManagementConfiguration); + deviceTypePluginDAO = new DeviceTypePluginDAO(deviceManagementConfiguration); + } + + public void initDAO(DeviceManagementConfiguration deviceManagementConfiguration) { + String datasourceName = deviceManagementConfiguration.getDeviceManagementConfigRepository() + .getDataSourceConfig().getJndiLookupDefinition().getJndiName(); + try { + Context ctx = new InitialContext(); + dataSource = (DataSource) ctx.lookup(datasourceName); + } catch (NamingException e) { + log.error("Error while looking up the data source: " + datasourceName, e); + } + } + + public DeviceTypePluginDAO getDeviceDAO() { + return deviceTypePluginDAO; + } + + public void beginTransaction() throws DeviceTypeMgtPluginException { + try { + Connection conn = dataSource.getConnection(); + conn.setAutoCommit(false); + currentConnection.set(conn); + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Error occurred while retrieving datasource connection", e); + } + } + + public Connection getConnection() throws DeviceTypeMgtPluginException { + if (currentConnection.get() == null) { + try { + currentConnection.set(dataSource.getConnection()); + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Error occurred while retrieving data source connection", e); + } + } + return currentConnection.get(); + } + + public void commitTransaction() throws DeviceTypeMgtPluginException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + conn.commit(); + } else { + if (log.isDebugEnabled()) { + log.debug("Datasource connection associated with the current thread is null, hence commit " + + "has not been attempted"); + } + } + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Error occurred while committing the transaction", e); + } finally { + closeConnection(); + } + } + + public void closeConnection() throws DeviceTypeMgtPluginException { + + Connection con = currentConnection.get(); + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + log.error("Error occurred while close the connection"); + } + } + currentConnection.remove(); + } + + public void rollbackTransaction() throws DeviceTypeMgtPluginException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + conn.rollback(); + } else { + if (log.isDebugEnabled()) { + log.debug("Datasource connection associated with the current thread is null, hence rollback " + + "has not been attempted"); + } + } + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Error occurred while rollback the transaction", e); + } finally { + closeConnection(); + } + } +} \ No newline at end of file diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/feature/AnnotationBasedFeatureManager.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/feature/AnnotationBasedFeatureManager.java new file mode 100644 index 0000000000..7945501497 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/feature/AnnotationBasedFeatureManager.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.template.feature; + +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier; +import org.wso2.carbon.device.mgt.common.Feature; +import org.wso2.carbon.device.mgt.common.FeatureManager; +import org.wso2.carbon.device.mgt.extensions.feature.mgt.GenericFeatureManager; + +import java.util.List; + +public class AnnotationBasedFeatureManager implements FeatureManager { + private DeviceTypeIdentifier deviceType; + + public AnnotationBasedFeatureManager(DeviceTypeIdentifier deviceTypeIdentifier) { + this.deviceType = deviceTypeIdentifier; + } + + @Override + public boolean addFeature(Feature feature) throws DeviceManagementException { + return false; + } + + @Override + public boolean addFeatures(List features) throws DeviceManagementException { + return false; + } + + @Override + public Feature getFeature(String name) throws DeviceManagementException { + return GenericFeatureManager.getInstance().getFeature(deviceType, name); + } + + @Override + public List getFeatures() throws DeviceManagementException { + return GenericFeatureManager.getInstance().getFeatures(deviceType); + } + + @Override + public boolean removeFeature(String name) throws DeviceManagementException { + return false; + } + + @Override + public boolean addSupportedFeaturesToDB() throws DeviceManagementException { + return false; + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/feature/ConfigurationBasedFeatureManager.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/feature/ConfigurationBasedFeatureManager.java new file mode 100644 index 0000000000..b918a66a8b --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/feature/ConfigurationBasedFeatureManager.java @@ -0,0 +1,40 @@ +package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.feature; + +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.Feature; +import org.wso2.carbon.device.mgt.common.FeatureManager; + +import java.util.List; + +public class ConfigurationBasedFeatureManager implements FeatureManager { + + @Override + public boolean addFeature(Feature feature) throws DeviceManagementException { + return false; + } + + @Override + public boolean addFeatures(List features) throws DeviceManagementException { + return false; + } + + @Override + public Feature getFeature(String name) throws DeviceManagementException { + return null; + } + + @Override + public List getFeatures() throws DeviceManagementException { + return null; + } + + @Override + public boolean removeFeature(String name) throws DeviceManagementException { + return false; + } + + @Override + public boolean addSupportedFeaturesToDB() throws DeviceManagementException { + return false; + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceSchemaInitializer.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceSchemaInitializer.java new file mode 100644 index 0000000000..e4b2aef85b --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceSchemaInitializer.java @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.template.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.utils.dbcreator.DatabaseCreator; + +import javax.sql.DataSource; +import java.io.File; + +/** + * Provides methods for initializing the database script. + */ +public class DeviceSchemaInitializer extends DatabaseCreator{ + + private static final Log log = LogFactory.getLog(DeviceSchemaInitializer.class); + private String setupSQLScriptBaseLocation; + + public DeviceSchemaInitializer(DataSource dataSource, String deviceType) { + super(dataSource); + setupSQLScriptBaseLocation = CarbonUtils.getCarbonHome() + File.separator + "dbscripts" + + File.separator + "cdm" + File.separator + "plugins" + File.separator + + deviceType + File.separator; + } + + @Override + protected String getDbScriptLocation(String databaseType) { + String scriptName = databaseType + ".sql"; + if (log.isDebugEnabled()) { + log.debug("Loading database script from :" + scriptName); + } + return setupSQLScriptBaseLocation.replaceFirst("DBTYPE", databaseType) + scriptName; + } +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceTypeUtils.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceTypeUtils.java new file mode 100644 index 0000000000..4ecf045036 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/util/DeviceTypeUtils.java @@ -0,0 +1,167 @@ +/* + * 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.extensions.device.type.deployer.template.util; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.license.mgt.License; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceManagementConfiguration; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException; +import org.wso2.carbon.device.mgt.extensions.push.notification.provider.internal.DeviceTypeManagementDataHolder; +import org.wso2.carbon.registry.api.RegistryException; +import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.registry.core.Registry; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * Contains utility methods used by plugin. + */ +public class DeviceTypeUtils { + + private static Log log = LogFactory.getLog(DeviceTypeUtils.class); + + public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing database connection", e); + } + } + } + + public static void cleanupResources(PreparedStatement stmt, ResultSet rs) { + cleanupResources(null, stmt, rs); + } + + /** + * Creates the device management schema. + */ + public static void setupDeviceManagementSchema(DeviceManagementConfiguration deviceManagementConfiguration) + throws DeviceTypeMgtPluginException { + String datasourceName = deviceManagementConfiguration.getDeviceManagementConfigRepository() + .getDataSourceConfig().getJndiLookupDefinition().getJndiName(); + try { + Context ctx = new InitialContext(); + DataSource dataSource = (DataSource) ctx.lookup(datasourceName); + DeviceSchemaInitializer initializer = new DeviceSchemaInitializer(dataSource, deviceManagementConfiguration + .getDeviceType()); + String checkSql = "select * from VIRTUAL_FIREALARM_DEVICE"; + if (!initializer.isDatabaseStructureCreated(checkSql)) { + log.info("Initializing device management repository database schema"); + initializer.createRegistryDatabase(); + } else { + log.info("Device management repository database already exists. Not creating a new database."); + } + } catch (NamingException e) { + log.error("Error while looking up the data source: " + datasourceName, e); + } catch (Exception e) { + throw new DeviceTypeMgtPluginException("Error occurred while initializing Device " + + "Management database schema", e); + } + } + + public static Registry getConfigurationRegistry() throws DeviceTypeMgtPluginException { + try { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return DeviceTypeManagementDataHolder.getInstance().getRegistryService() + .getConfigSystemRegistry(tenantId); + } catch (RegistryException e) { + throw new DeviceTypeMgtPluginException("Error in retrieving conf registry instance: " + e.getMessage(), e); + } + } + + public static boolean putRegistryResource(String path, Resource resource) throws DeviceTypeMgtPluginException { + boolean status; + try { + DeviceTypeUtils.getConfigurationRegistry().beginTransaction(); + DeviceTypeUtils.getConfigurationRegistry().put(path, resource); + DeviceTypeUtils.getConfigurationRegistry().commitTransaction(); + status = true; + } catch (RegistryException e) { + throw new DeviceTypeMgtPluginException("Error occurred while persisting registry resource : " + + e.getMessage(), e); + } + return status; + } + + public static Resource getRegistryResource(String path) throws DeviceTypeMgtPluginException { + try { + if(DeviceTypeUtils.getConfigurationRegistry().resourceExists(path)){ + return DeviceTypeUtils.getConfigurationRegistry().get(path); + } + return null; + } catch (RegistryException e) { + throw new DeviceTypeMgtPluginException("Error in retrieving registry resource : " + e.getMessage(), e); + } + } + + public static License getDefaultLicense(String deviceType) { + License license = new License(); + license.setName(deviceType); + license.setLanguage("en_US"); + license.setVersion("1.0.0"); + license.setText("This End User License Agreement (\"Agreement\") is a legal agreement between you (\"You\") " + + "and WSO2, Inc., regarding the enrollment of Your personal mobile device (\"Device\") in SoR's " + + "mobile device management program, and the loading to and removal from Your Device and Your use " + + "of certain applications and any associated software and user documentation, whether provided in " + + "\"online\" or electronic format, used in connection with the operation of or provision of services " + + "to WSO2, Inc., BY SELECTING \"I ACCEPT\" DURING INSTALLATION, YOU ARE ENROLLING YOUR DEVICE, AND " + + "THEREBY AUTHORIZING SOR OR ITS AGENTS TO INSTALL, UPDATE AND REMOVE THE APPS FROM YOUR DEVICE AS " + + "DESCRIBED IN THIS AGREEMENT. YOU ARE ALSO EXPLICITLY ACKNOWLEDGING AND AGREEING THAT (1) THIS IS " + + "A BINDING CONTRACT AND (2) YOU HAVE READ AND AGREE TO THE TERMS OF THIS AGREEMENT.\n" + + "\n" + + "IF YOU DO NOT ACCEPT THESE TERMS, DO NOT ENROLL YOUR DEVICE AND DO NOT PROCEED ANY FURTHER.\n" + + "\n" + + "You agree that: (1) You understand and agree to be bound by the terms and conditions contained " + + "in this Agreement, and (2) You are at least 21 years old and have the legal capacity to enter " + + "into this Agreement as defined by the laws of Your jurisdiction. SoR shall have the right, " + + "without prior notice, to terminate or suspend (i) this Agreement, (ii) the enrollment of Your " + + "Device, or (iii) the functioning of the Apps in the event of a violation of this Agreement or " + + "the cessation of Your relationship with SoR (including termination of Your employment if You are " + + "an employee or expiration or termination of Your applicable franchise or supply agreement if You " + + "are a franchisee of or supplier to the WSO2 WSO2, Inc., system). SoR expressly reserves all " + + "rights not expressly granted herein."); + return license; + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/util/DeviceTypeConfigUtil.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/util/DeviceTypeConfigUtil.java new file mode 100644 index 0000000000..63d4b855db --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/util/DeviceTypeConfigUtil.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2016, 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.extensions.device.type.deployer.util; + +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.exception.DeviceTypeConfigurationException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import java.io.File; + +public class DeviceTypeConfigUtil { + + public static final String CARBON_HOME = "carbon.home"; + public static final String CARBON_HOME_ENTRY = "${carbon.home}"; + + public static Document convertToDocument(File file) throws DeviceTypeConfigurationException { + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); + try { + DocumentBuilder docBuilder = factory.newDocumentBuilder(); + return docBuilder.parse(file); + } catch (Exception e) { + throw new DeviceTypeConfigurationException("Error occurred while parsing file, while converting " + + "to a org.w3c.dom.Document", e); + } + } + +} diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/resources/META-INF/component.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/resources/META-INF/component.xml new file mode 100644 index 0000000000..c1c5f52048 --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/resources/META-INF/component.xml @@ -0,0 +1,23 @@ + + + + + devicetypes + xml + org.wso2.carbon.device.mgt.extensions.device.type.deployer.DeviceTypeDeployer + + + diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/test/resources/sample.xml b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/test/resources/sample.xml new file mode 100644 index 0000000000..dd68afbf7e --- /dev/null +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/test/resources/sample.xml @@ -0,0 +1,85 @@ + + + + + sample + + + + + jdbc/SampleDM_DB + + + SAMPLE_DEVICE + + SAMPLE_DEVICE_ID + + + booleanColumn + byteColumn + dateColumn + doubleColumn + floatColumn + intColumn + longColumn + shortColumn + + + en_US + 1.0.0 + This is license text + + + + + + false + + + + + + FEATURE_CODE_1 + feature name 1 + description + + + FEATURE_CODE_2 + feature name 2 + description + + + + + + + + MQTT + true + + sample.mqtt.adapter + tcp://localhost:1883 + admin + https://localhost:9443/dynamic-client-web/register + 0 + + true + + + diff --git a/components/device-mgt-extensions/pom.xml b/components/device-mgt-extensions/pom.xml index 013dcde404..3f46d25fa6 100644 --- a/components/device-mgt-extensions/pom.xml +++ b/components/device-mgt-extensions/pom.xml @@ -37,6 +37,7 @@ org.wso2.carbon.device.mgt.extensions.push.notification.provider.gcm org.wso2.carbon.device.mgt.extensions.push.notification.provider.mqtt org.wso2.carbon.device.mgt.extensions.push.notification.provider.xmpp + org.wso2.carbon.device.mgt.extensions.device.type.deployer diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/GenericFeatureManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/GenericFeatureManager.java index 2da3394542..2f498ba589 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/GenericFeatureManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/GenericFeatureManager.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.extensions.feature.mgt; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier; import org.wso2.carbon.device.mgt.common.Feature; import java.util.HashMap; @@ -32,7 +33,7 @@ import java.util.Map; public class GenericFeatureManager { private static final Log log = LogFactory.getLog(GenericFeatureManager.class); - private static Map> featureSet = new HashMap<>(); + private static Map> featureSet = new HashMap<>(); private static GenericFeatureManager instance = new GenericFeatureManager(); private GenericFeatureManager() { @@ -45,7 +46,7 @@ public class GenericFeatureManager { /** * @param deviceTypeFeatures feature list for each device type. */ - public void addFeatures(Map> deviceTypeFeatures) { + public void addFeatures(Map> deviceTypeFeatures) { this.featureSet.putAll(deviceTypeFeatures); } @@ -54,7 +55,7 @@ public class GenericFeatureManager { * @param featureName * @return the extracted feature for the which matches the feature name and device type. */ - public Feature getFeature(String deviceType, String featureName) { + public Feature getFeature(DeviceTypeIdentifier deviceType, String featureName) { Feature extractedFeature = null; List deviceFeatureList = featureSet.get(deviceType); for (Feature feature : deviceFeatureList) { @@ -69,7 +70,7 @@ public class GenericFeatureManager { * @param deviceType returns the features for the device type. * @return */ - public List getFeatures(String deviceType) { + public List getFeatures(DeviceTypeIdentifier deviceType) { return featureSet.get(deviceType); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/lifecycle/listener/FeatureManagementLifecycleListener.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/lifecycle/listener/FeatureManagementLifecycleListener.java index 3884411ee2..a0ecf87f6d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/lifecycle/listener/FeatureManagementLifecycleListener.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/lifecycle/listener/FeatureManagementLifecycleListener.java @@ -23,6 +23,9 @@ import org.apache.catalina.LifecycleListener; import org.apache.catalina.core.StandardContext; 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.device.mgt.common.DeviceTypeIdentifier; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.extensions.feature.mgt.GenericFeatureManager; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; @@ -40,6 +43,8 @@ public class FeatureManagementLifecycleListener implements LifecycleListener { private static final String API_CONFIG_DEFAULT_VERSION = "1.0.0"; private static final String PARAM_MANAGED_API_ENABLED = "managed-api-enabled"; + private static final String PARAM_SHARED_WITH_ALL_TENANTS = "isSharedWithAllTenants"; + private static final String PARAM_PROVIDER_TENANT_DOMAIN = "providerTenantDomain"; private static final Log log = LogFactory.getLog(FeatureManagementLifecycleListener.class); @@ -54,9 +59,24 @@ public class FeatureManagementLifecycleListener implements LifecycleListener { try { AnnotationProcessor annotationProcessor = new AnnotationProcessor(context); Set annotatedAPIClasses = annotationProcessor.scanStandardContext(DeviceType.class.getName()); - Map> features = annotationProcessor.extractFeatures(annotatedAPIClasses); - if (features != null && !features.isEmpty()) { - GenericFeatureManager.getInstance().addFeatures(features); + String tenantDomain = servletContext.getInitParameter(PARAM_PROVIDER_TENANT_DOMAIN); + tenantDomain = (tenantDomain != null && !tenantDomain.isEmpty()) ? tenantDomain : + MultitenantConstants.SUPER_TENANT_DOMAIN_NAME; + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain, true); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + + String sharingValueParam = servletContext.getInitParameter(PARAM_SHARED_WITH_ALL_TENANTS); + boolean isSharedWithAllTenants = Boolean.parseBoolean(sharingValueParam); + + Map> features = annotationProcessor.extractFeatures( + annotatedAPIClasses, tenantId, isSharedWithAllTenants); + if (features != null && !features.isEmpty()) { + GenericFeatureManager.getInstance().addFeatures(features); + } + } finally { + PrivilegedCarbonContext.endTenantFlow(); } } catch (IOException e) { log.error("Error enconterd while discovering annotated classes.", e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/util/AnnotationProcessor.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/util/AnnotationProcessor.java index 43d68f0f2e..b766103671 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/util/AnnotationProcessor.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/feature/mgt/util/AnnotationProcessor.java @@ -24,6 +24,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.scannotation.AnnotationDB; import org.scannotation.WarUrlFinder; +import org.wso2.carbon.device.mgt.common.DeviceTypeIdentifier; import org.wso2.carbon.device.mgt.common.Feature; import org.wso2.carbon.device.mgt.extensions.feature.mgt.annotations.DeviceType; @@ -98,15 +99,17 @@ public class AnnotationProcessor { /** * Method identifies the URL templates and context by reading the annotations of a class */ - public Map> extractFeatures(Set entityClasses) throws ClassNotFoundException { - Map> features = null; + public Map> extractFeatures(Set entityClasses, final int tenantId, + final boolean isSharedWithAllTenants) + throws ClassNotFoundException { + Map> features = null; if (entityClasses != null && !entityClasses.isEmpty()) { features = new HashMap<>(); for (final String className : entityClasses) { - final Map> featureMap = - AccessController.doPrivileged(new PrivilegedAction>>() { - public Map> run() { - Map> featureMap = new HashMap<>(); + final Map> featureMap = + AccessController.doPrivileged(new PrivilegedAction>>() { + public Map> run() { + Map> featureMap = new HashMap<>(); try { Class clazz = classLoader.loadClass(className); Class deviceTypeClazz = (Class) classLoader.loadClass( @@ -121,7 +124,13 @@ public class AnnotationProcessor { org.wso2.carbon.device.mgt.extensions.feature.mgt .annotations.Feature.class.getName()); List featureList = getFeatures(clazz.getDeclaredMethods()); - featureMap.put(deviceType, featureList); + DeviceTypeIdentifier deviceTypeIdentifier; + if (isSharedWithAllTenants) { + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType); + } else { + deviceTypeIdentifier = new DeviceTypeIdentifier(deviceType, tenantId); + } + featureMap.put(deviceTypeIdentifier, featureList); } } catch (Throwable e) { log.error("Failed to load the annotation from the features in the " +