From b401d7f61ebeedc952d5e3a6edd5b7e032e5a23d Mon Sep 17 00:00:00 2001 From: harshanl Date: Tue, 17 Nov 2015 23:05:21 +0530 Subject: [PATCH 1/2] added GCM support to android --- .../pom.xml | 14 +- .../AndroidPolicyMonitoringService.java | 7 +- .../mobile/impl/android/gcm/GCMService.java | 140 ++++++++++++++++++ .../MobileDeviceManagementDataHolder.java | 20 +++ ...obileDeviceManagementServiceComponent.java | 16 +- .../dbscripts/plugins/android/h2.sql | 2 +- .../dbscripts/plugins/android/mssql.sql | 2 +- .../dbscripts/plugins/android/mysql.sql | 2 +- .../dbscripts/plugins/android/oracle.sql | 4 +- .../dbscripts/plugins/android/postgresql.sql | 2 +- pom.xml | 12 +- 11 files changed, 207 insertions(+), 14 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/gcm/GCMService.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml index 05201d0159..c632be6bed 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/pom.xml @@ -61,28 +61,28 @@ javax.xml.bind.*, javax.naming, javax.sql, - javax.xml.bind.annotation.*, javax.xml.parsers, org.w3c.dom, - org.wso2.carbon.core, org.wso2.carbon.context, org.wso2.carbon.utils.*, org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.ndatasource.core, org.wso2.carbon.policy.mgt.common.*, - org.wso2.carbon.policy.mgt.core.*, org.wso2.carbon.registry.core, - org.wso2.carbon.registry.core.exceptions, org.wso2.carbon.registry.core.service, org.wso2.carbon.registry.core.session, org.wso2.carbon.registry.api, org.wso2.carbon.device.mgt.extensions.license.mgt.registry, - com.google.gson.* + com.google.android.gcm.*, + com.google.gson, + org.json.simple, + org.json.simple.parser !org.wso2.carbon.device.mgt.mobile.internal, !org.wso2.carbon.device.mgt.mobile.impl, org.wso2.carbon.device.mgt.mobile.*, + com.google.android.gcm.*, @@ -170,5 +170,9 @@ com.google.code.gson gson + + com.google.android.gcm + gcm-server + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java index 49ff0ab17d..3738d156e1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java @@ -28,6 +28,8 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.mobile.impl.android.gcm.GCMService; +import org.wso2.carbon.device.mgt.mobile.internal.MobileDeviceManagementDataHolder; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceData; import org.wso2.carbon.policy.mgt.common.monitor.ComplianceFeature; @@ -43,7 +45,10 @@ public class AndroidPolicyMonitoringService implements PolicyMonitoringService { @Override public void notifyDevices(List list) throws PolicyComplianceException { - + GCMService gcmService = MobileDeviceManagementDataHolder.getInstance().getGCMService(); + if (gcmService.isGCMEnabled()) { + gcmService.sendNotification("POLICY_BUNDLE", list); + } } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/gcm/GCMService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/gcm/GCMService.java new file mode 100644 index 0000000000..57c79f7a94 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/gcm/GCMService.java @@ -0,0 +1,140 @@ +/* + * 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.mobile.impl.android.gcm; + +import com.google.android.gcm.server.Message; +import com.google.android.gcm.server.Sender; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.CarbonContext; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; +import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants; +import org.wso2.carbon.device.mgt.mobile.internal.MobileDeviceManagementDataHolder; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/** + * GCM notification service implementation for Android platform. + */ +public class GCMService { + + private static final Log log = LogFactory.getLog(GCMService.class); + public static final String GCM_APIKEY = "gcmAPIKey"; + public static final String NOTIFIER_TYPE = "notifierType"; + public static final String GCM_NOTIFIER_CODE = "2"; + private static HashMap tenantConfigurationCache = new HashMap<>(); + + public boolean isGCMEnabled() { + String notifierType = getConfigurationProperty(NOTIFIER_TYPE); + if (GCM_NOTIFIER_CODE.equals(notifierType)) { + return true; + } + return false; + } + + public void sendNotification(String messageData, Device device) { + int seconds = 60; + Sender sender = new Sender(getConfigurationProperty(GCM_APIKEY)); + Message message = + new Message.Builder().timeToLive(seconds).delayWhileIdle(false).addData("data", messageData).build(); + try { + sender.send(message, getGCMToken(device.getProperties()), 5); + } catch (IOException e) { + log.error("Exception occurred while sending the GCM notification.",e); + } + } + + public void sendNotification(String messageData, List devices) { + int seconds = 60; + Sender sender = new Sender(getConfigurationProperty(GCM_APIKEY)); + Message message = + new Message.Builder().timeToLive(seconds).delayWhileIdle(false).addData("data", messageData).build(); + try { + sender.send(message, getGCMTokens(devices), 5); + } catch (IOException e) { + log.error("Exception occurred while sending the GCM notification.",e); + } + } + + private static List getGCMTokens(List devices) { + List tokens = new ArrayList<>(); + for (Device device : devices) { + tokens.add(getGCMToken(device.getProperties())); + } + return tokens; + } + + private static String getGCMToken(List properties) { + String gcmToken = null; + for (Device.Property property : properties) { + if (AndroidPluginConstants.GCM_TOKEN.equals(property.getName())) { + gcmToken = property.getValue(); + break; + } + } + return gcmToken; + } + + private static String getConfigurationProperty(String property) { + DeviceManagementService androidDMService = MobileDeviceManagementDataHolder.getInstance(). + getAndroidDeviceManagementService(); + try { + //Get the TenantConfiguration from cache if not we'll get it from DM service + TenantConfiguration tenantConfiguration = getTenantConfigurationFromCache(); + if (tenantConfiguration == null) { + tenantConfiguration = androidDMService.getDeviceManager().getConfiguration(); + if (tenantConfiguration != null) { + addTenantConfigurationToCache(tenantConfiguration); + } + } + + if (tenantConfiguration != null) { + List configs = tenantConfiguration.getConfiguration(); + for (ConfigurationEntry entry : configs) { + if (property.equals(entry.getName())) { + return (String) entry.getValue(); + } + } + } + return ""; + } catch (DeviceManagementException e) { + log.error("Exception occurred while fetching the tenant-config.",e); + } + return null; + } + + private static void addTenantConfigurationToCache(TenantConfiguration tenantConfiguration) { + tenantConfigurationCache.put(getTenantId(), tenantConfiguration); + } + + private static TenantConfiguration getTenantConfigurationFromCache() { + return tenantConfigurationCache.get(getTenantId()); + } + + private static int getTenantId() { + return CarbonContext.getThreadLocalCarbonContext().getTenantId(); + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementDataHolder.java index 40c6610090..869904e81c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementDataHolder.java @@ -18,6 +18,8 @@ package org.wso2.carbon.device.mgt.mobile.internal; +import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.mobile.impl.android.gcm.GCMService; import org.wso2.carbon.registry.core.service.RegistryService; /** @@ -26,6 +28,8 @@ import org.wso2.carbon.registry.core.service.RegistryService; public class MobileDeviceManagementDataHolder { private RegistryService registryService; + private DeviceManagementService androidDeviceManagementService; + private GCMService gcmService; private static MobileDeviceManagementDataHolder thisInstance = new MobileDeviceManagementDataHolder(); @@ -44,4 +48,20 @@ public class MobileDeviceManagementDataHolder { this.registryService = registryService; } + public DeviceManagementService getAndroidDeviceManagementService() { + return androidDeviceManagementService; + } + + public void setAndroidDeviceManagementService( + DeviceManagementService androidDeviceManagementService) { + this.androidDeviceManagementService = androidDeviceManagementService; + } + + public GCMService getGCMService() { + return gcmService; + } + + public void setGCMService(GCMService gcmService) { + this.gcmService = gcmService; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java index 8f71321f43..92404b67ae 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java @@ -32,6 +32,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.AbstractMobileDeviceManagementDAOFa import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManagementService; import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidPolicyMonitoringService; +import org.wso2.carbon.device.mgt.mobile.impl.android.gcm.GCMService; import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManagementService; import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsPolicyMonitoringService; import org.wso2.carbon.ndatasource.core.DataSourceService; @@ -61,6 +62,7 @@ public class MobileDeviceManagementServiceComponent { private ServiceRegistration androidServiceRegRef; private ServiceRegistration windowsServiceRegRef; + private ServiceRegistration gcmServiceRegRef; private static final Log log = LogFactory.getLog(MobileDeviceManagementServiceComponent.class); @@ -98,14 +100,20 @@ public class MobileDeviceManagementServiceComponent { log.error("Exception occurred while initializing mobile device management database schema", e); } } + DeviceManagementService androidDeviceManagementService = new AndroidDeviceManagementService(); + GCMService gcmService = new GCMService(); androidServiceRegRef = bundleContext.registerService(DeviceManagementService.class.getName(), - new AndroidDeviceManagementService(), null); + androidDeviceManagementService, null); windowsServiceRegRef = bundleContext.registerService(DeviceManagementService.class.getName(), new WindowsDeviceManagementService(), null); + gcmServiceRegRef = + bundleContext.registerService(GCMService.class.getName(), gcmService, null); + + // Policy management service bundleContext.registerService(PolicyMonitoringService.class, @@ -113,6 +121,9 @@ public class MobileDeviceManagementServiceComponent { bundleContext.registerService(PolicyMonitoringService.class, new WindowsPolicyMonitoringService(), null); + MobileDeviceManagementDataHolder.getInstance().setAndroidDeviceManagementService( + androidDeviceManagementService); + MobileDeviceManagementDataHolder.getInstance().setGCMService(gcmService); if (log.isDebugEnabled()) { log.debug("Mobile Device Management Service Component has been successfully activated"); } @@ -132,6 +143,9 @@ public class MobileDeviceManagementServiceComponent { if (windowsServiceRegRef != null) { windowsServiceRegRef.unregister(); } + if (gcmServiceRegRef != null) { + gcmServiceRegRef.unregister(); + } if (log.isDebugEnabled()) { log.debug( "Mobile Device Management Service Component has been successfully de-activated"); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/h2.sql index 09b3cc91c3..3673fd122e 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/h2.sql @@ -4,7 +4,7 @@ -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `AD_DEVICE` ( `DEVICE_ID` VARCHAR(45) NOT NULL, - `GCM_TOKEN` VARCHAR(45) NULL DEFAULT NULL, + `GCM_TOKEN` VARCHAR(1000) NULL DEFAULT NULL, `DEVICE_INFO` VARCHAR(8000) NULL DEFAULT NULL, `IMEI` VARCHAR(45) NULL DEFAULT NULL, `IMSI` VARCHAR(45) NULL DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mssql.sql index f95ec0f913..be8caf6c43 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mssql.sql @@ -3,7 +3,7 @@ -- ----------------------------------------------------- CREATE TABLE AD_DEVICE ( DEVICE_ID VARCHAR(45) NOT NULL, - GCM_TOKEN VARCHAR(45) NULL DEFAULT NULL, + GCM_TOKEN VARCHAR(1000) NULL DEFAULT NULL, DEVICE_INFO VARCHAR(8000) NULL DEFAULT NULL, IMEI VARCHAR(45) NULL DEFAULT NULL, IMSI VARCHAR(45) NULL DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mysql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mysql.sql index 0f80bdd344..8e85b92c2b 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mysql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mysql.sql @@ -3,7 +3,7 @@ -- ----------------------------------------------------- CREATE TABLE IF NOT EXISTS `AD_DEVICE` ( `DEVICE_ID` VARCHAR(45) NOT NULL, - `GCM_TOKEN` VARCHAR(45) NULL DEFAULT NULL, + `GCM_TOKEN` VARCHAR(1000) NULL DEFAULT NULL, `DEVICE_INFO` VARCHAR(8000) NULL DEFAULT NULL, `IMEI` VARCHAR(45) NULL DEFAULT NULL, `IMSI` VARCHAR(45) NULL DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/oracle.sql index 7e3007ee6c..8f00cd3316 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/oracle.sql @@ -3,8 +3,8 @@ -- ----------------------------------------------------- CREATE TABLE AD_DEVICE ( DEVICE_ID VARCHAR(45) NOT NULL , - DEVICE_INFO VARCHAR(1000) DEFAULT NULL, - GCM_TOKEN VARCHAR(45) DEFAULT NULL, + DEVICE_INFO VARCHAR(8000) DEFAULT NULL, + GCM_TOKEN VARCHAR(1000) DEFAULT NULL, IMEI VARCHAR(45) DEFAULT NULL, IMSI VARCHAR(45) DEFAULT NULL, OS_VERSION VARCHAR(45) DEFAULT NULL, diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/postgresql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/postgresql.sql index 18d67c43dd..e4512c53fc 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/postgresql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/postgresql.sql @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS AD_DEVICE ( DEVICE_ID VARCHAR(45) NOT NULL , DEVICE_INFO TEXT NULL DEFAULT NULL, - GCM_TOKEN VARCHAR(45) NULL DEFAULT NULL, + GCM_TOKEN VARCHAR(1000) NULL DEFAULT NULL, IMEI VARCHAR(45) NULL DEFAULT NULL, IMSI VARCHAR(45) NULL DEFAULT NULL, OS_VERSION VARCHAR(45) NULL DEFAULT NULL, diff --git a/pom.xml b/pom.xml index 029ddf51e5..1ece8c3c17 100644 --- a/pom.xml +++ b/pom.xml @@ -528,6 +528,11 @@ gson ${google.gson.version} + + com.google.android.gcm + gcm-server + ${gcm.server.version} + @@ -595,7 +600,7 @@ 2.0.0 3.0.21 2.2.4 - + 1.0.2 @@ -785,5 +790,10 @@ false + + gcm-server-repository + GCM Server repository - GitHub + https://github.com/slorber/gcm-server-repository/raw/master/releases/ + From cfb6464cbe4de259142837193587a9cdec1130cb Mon Sep 17 00:00:00 2001 From: harshanl Date: Wed, 18 Nov 2015 12:51:54 +0530 Subject: [PATCH 2/2] Fixed policy monitoring empty device list issue --- .../mgt/mobile/impl/android/AndroidPolicyMonitoringService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java index 3738d156e1..6a03fa962f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidPolicyMonitoringService.java @@ -46,7 +46,7 @@ public class AndroidPolicyMonitoringService implements PolicyMonitoringService { @Override public void notifyDevices(List list) throws PolicyComplianceException { GCMService gcmService = MobileDeviceManagementDataHolder.getInstance().getGCMService(); - if (gcmService.isGCMEnabled()) { + if (gcmService.isGCMEnabled() && !list.isEmpty()) { gcmService.sendNotification("POLICY_BUNDLE", list); } }