From a6cdbb856bc7067774270b6c7bf78caee95a400e Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 16 Sep 2018 17:26:20 +0530 Subject: [PATCH 1/4] Adding unit test for StorageManagementUtilTest.java --- .../StorageManagementUtilTest.java | 76 +++++++++++++++++++ .../src/test/resources/testng.xml | 1 + 2 files changed, 77 insertions(+) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/StorageManagementUtilTest.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/StorageManagementUtilTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/StorageManagementUtilTest.java new file mode 100644 index 0000000000..c857b0a67b --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/StorageManagementUtilTest.java @@ -0,0 +1,76 @@ +/* + * Copyright (c) 2018, 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.application.mgt.core; + +import org.junit.Assert; +import org.testng.annotations.AfterMethod; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.Test; +import org.wso2.carbon.device.application.mgt.common.exception.ResourceManagementException; +import org.wso2.carbon.device.application.mgt.core.util.StorageManagementUtil; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; + + +public class StorageManagementUtilTest { + private static final String TEMP_FOLDER = "src/test/resources/util/temp"; + private static final String APK_FILE = "src/test/resources/util/app-debug.apk"; + private static final String APK_FILE_NAME = "/app-debug.apk"; + + @BeforeMethod + public void before() throws IOException { + File file = new File(TEMP_FOLDER); + if (file.exists()) { + StorageManagementUtil.deleteDir(file); + } + } + + @Test + public void testCreateArtifactDirectory() { + try { + StorageManagementUtil.createArtifactDirectory(TEMP_FOLDER); + } catch (ResourceManagementException e) { + e.printStackTrace(); + Assert.fail("Directory creation failed."); + } + } + + @Test + public void testSaveFile() throws IOException, ResourceManagementException { + StorageManagementUtil.createArtifactDirectory(TEMP_FOLDER); + InputStream apk = new FileInputStream(APK_FILE); + StorageManagementUtil.saveFile(apk, TEMP_FOLDER + APK_FILE_NAME); + File file = new File(TEMP_FOLDER + APK_FILE_NAME); + if(!file.exists()) { + Assert.fail("File saving failed."); + } + } + + @AfterMethod + public void deleteFileTest() throws IOException, ResourceManagementException { + File file = new File(TEMP_FOLDER); + StorageManagementUtil.deleteDir(file); + if(file.exists()) { + Assert.fail("File deleting failed."); + } + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/testng.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/testng.xml index ca512950bc..9a69c5a87f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/testng.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/resources/testng.xml @@ -23,6 +23,7 @@ + \ No newline at end of file From 8983fd6d51f1d2697bad2769788e1ce064c3e9ef Mon Sep 17 00:00:00 2001 From: inoshperera Date: Sun, 16 Sep 2018 17:32:41 +0530 Subject: [PATCH 2/4] formatting fixes --- .../StorageManagementUtilTest.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/StorageManagementUtilTest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/StorageManagementUtilTest.java index c857b0a67b..68817849cf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/StorageManagementUtilTest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/test/java/org.wso2.carbon.device.application.mgt.core/StorageManagementUtilTest.java @@ -30,7 +30,6 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; - public class StorageManagementUtilTest { private static final String TEMP_FOLDER = "src/test/resources/util/temp"; private static final String APK_FILE = "src/test/resources/util/app-debug.apk"; @@ -60,7 +59,7 @@ public class StorageManagementUtilTest { InputStream apk = new FileInputStream(APK_FILE); StorageManagementUtil.saveFile(apk, TEMP_FOLDER + APK_FILE_NAME); File file = new File(TEMP_FOLDER + APK_FILE_NAME); - if(!file.exists()) { + if (!file.exists()) { Assert.fail("File saving failed."); } } @@ -69,7 +68,7 @@ public class StorageManagementUtilTest { public void deleteFileTest() throws IOException, ResourceManagementException { File file = new File(TEMP_FOLDER); StorageManagementUtil.deleteDir(file); - if(file.exists()) { + if (file.exists()) { Assert.fail("File deleting failed."); } } From a4e17ef99b1aeaaf282ff24c489e651484b43c86 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Fri, 21 Sep 2018 06:37:59 +0530 Subject: [PATCH 3/4] Adding app install basics WIP --- .../pom.xml | 2 +- .../mgt/common/AndroidApplication.java | 49 +++++++ .../application/mgt/common/AppOperation.java | 106 +++++++++++++++ .../mgt/common/EnterpriseApplication.java | 57 ++++++++ .../exception/DeviceConnectorException.java | 48 +++++++ .../mgt/common/services/DeviceConnector.java | 42 ++++++ .../mgt/core/dao/SubscriptionDAO.java | 2 +- .../GenericSubscriptionDAOImpl.java | 5 +- .../mgt/core/impl/MAMDeviceConnectorImpl.java | 125 ++++++++++++++++++ .../core/impl/SubscriptionManagerImpl.java | 3 +- .../util/AndroidApplicationOperationUtil.java | 53 ++++++++ .../mgt/core/util/MDMAppConstants.java | 81 ++++++++++++ 12 files changed, 568 insertions(+), 5 deletions(-) create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AndroidApplication.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AppOperation.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/EnterpriseApplication.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/DeviceConnectorException.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/DeviceConnector.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/MAMDeviceConnectorImpl.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/AndroidApplicationOperationUtil.java create mode 100644 components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/MDMAppConstants.java diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/pom.xml index 79f49e3850..1bab17a3bf 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/pom.xml @@ -51,7 +51,7 @@ ${carbon.device.mgt.version} Application Management Common Bundle - org.wso2.carbon.device.mgt.common.operation.mgt.*;version="${carbon.device.mgt.version}", + org.wso2.carbon.device.mgt.common.*;version="${carbon.device.mgt.version}", com.google.gson, io.swagger.annotations.*;resolution:=optional diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AndroidApplication.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AndroidApplication.java new file mode 100644 index 0000000000..3949e49ff1 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AndroidApplication.java @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2018, 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.application.mgt.common; + +import com.google.gson.Gson; +import org.wso2.carbon.device.application.mgt.common.exception.DeviceConnectorException; + +public class AndroidApplication { + private String type; + private String appIdentifier; + + public String getAppIdentifier() { + return appIdentifier; + } + + public void setAppIdentifier(String appIdentifier) { + this.appIdentifier = appIdentifier; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String toJSON() throws DeviceConnectorException { + Gson gson = new Gson(); + return gson.toJson(this); + } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AppOperation.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AppOperation.java new file mode 100644 index 0000000000..ace5779cd2 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AppOperation.java @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2018, 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.application.mgt.common; + +public class AppOperation { + + private static final long serialVersionUID = 7603215716452548282L; + + private Application application; + private int tenantId; + private String activityId; + private String scheduledDateTime; + private OperationType type; + private String subscribedBy; + private int appReleaseId; + private InstallState installState; + + public InstallState getInstallState() { + return installState; + } + + public void setInstallState(InstallState installState) { + this.installState = installState; + } + + public enum InstallState { + PENDING, INPROGRESS, INSTALLED, UNINSTALLED, ERROR + } + + public int getAppReleaseId() { + return appReleaseId; + } + + public void setAppReleaseId(int appReleaseId) { + this.appReleaseId = appReleaseId; + } + + public String getSubscribedBy() { + return subscribedBy; + } + + public void setSubscribedBy(String subscribedBy) { + this.subscribedBy = subscribedBy; + } + + public enum OperationType { + INSTALL, UNINSTALL, UPDATE + } + + public OperationType getType() { + return type; + } + + public void setType(OperationType type) { + this.type = type; + } + + public Application getApplication() { + return application; + } + + public void setApplication(Application application) { + this.application = application; + } + + public int getTenantId() { + return tenantId; + } + + public void setTenantId(int tenantId) { + this.tenantId = tenantId; + } + + public String getActivityId() { + return activityId; + } + + public void setActivityId(String activityId) { + this.activityId = activityId; + } + + public String getScheduledDateTime() { + return scheduledDateTime; + } + + public void setScheduledDateTime(String scheduledDateTime) { + this.scheduledDateTime = scheduledDateTime; + } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/EnterpriseApplication.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/EnterpriseApplication.java new file mode 100644 index 0000000000..2adf626059 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/EnterpriseApplication.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2018, 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.application.mgt.common; + +import com.google.gson.Gson; + +import java.io.Serializable; + +/** + * This class represents the Enterprise Application information. + */ +public class EnterpriseApplication extends AndroidApplication implements Serializable { + + private String url; + private String schedule; + private String packageName; + + public String getSchedule() { + return schedule; + } + + public void setSchedule(String schedule) { + this.schedule = schedule; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getPackageName() { + return packageName; + } + + public void setPackageName(String packageName) { + this.packageName = packageName; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/DeviceConnectorException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/DeviceConnectorException.java new file mode 100644 index 0000000000..3039b8dfcc --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/exception/DeviceConnectorException.java @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2017, 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.application.mgt.common.exception; + +/** + * Represents the exception thrown during device connections. + */ +public class DeviceConnectorException extends Exception { + private String message; + + public DeviceConnectorException(String message, Throwable throwable) { + super(message, throwable); + setMessage(message); + } + + public DeviceConnectorException(String message) { + super(message); + setMessage(message); + } + + public DeviceConnectorException() { + + } + @Override + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/DeviceConnector.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/DeviceConnector.java new file mode 100644 index 0000000000..d8c9af774b --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/DeviceConnector.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2018, 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.application.mgt.common.services; + +import org.wso2.carbon.device.application.mgt.common.AppOperation; +import org.wso2.carbon.device.application.mgt.common.exception.DeviceConnectorException; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + +import java.util.List; + +/** + * This interface contains operations necessary to perform actions on a device such as install and application on a + * device, uninstall or upgrade. This must be implemented to connect to an external device management server. + */ +public interface DeviceConnector { + + Boolean sendOperationToDevice(AppOperation appOperation, DeviceIdentifier deviceIdentifier) throws + DeviceConnectorException; + + Boolean sendOperationToGroup(AppOperation appOperation, String groupID) throws DeviceConnectorException; + + Boolean sendOperationToUser(AppOperation appOperation, List userList) throws DeviceConnectorException; + + Boolean sendOperationToRole(AppOperation appOperation, String role) throws DeviceConnectorException; + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java index e7d02adb78..83f8be707c 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/SubscriptionDAO.java @@ -42,7 +42,7 @@ public interface SubscriptionDAO { * @throws ApplicationManagementDAOException If unable to add a mapping between device and application */ void subscribeDeviceToApplication(int tenantId, String subscribedBy, List deviceList, int appId, - int releaseId) throws ApplicationManagementDAOException; + int releaseId, String installStatus) throws ApplicationManagementDAOException; /** * Adds a mapping between user and the application which the application is installed on. This mapping will be diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java index 3a89159814..e5e4253691 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/impl/subscription/GenericSubscriptionDAOImpl.java @@ -37,14 +37,14 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc @Override public void subscribeDeviceToApplication(int tenantId, String subscribedBy, List deviceList, int appId, - int releaseId) throws ApplicationManagementDAOException { + int releaseId, String installStatus) throws ApplicationManagementDAOException { Connection conn; PreparedStatement stmt = null; try { conn = this.getDBConnection(); long time = System.currentTimeMillis() / 1000; String sql = "INSERT INTO AP_DEVICE_SUBSCRIPTION(TENANT_ID, SUBSCRIBED_BY, SUBSCRIBED_TIMESTAMP, " - + "DM_DEVICE_ID, AP_APP_RELEASE_ID, AP_APP_ID) VALUES (?, ?, ?, ?, ?, ?)"; + + "DM_DEVICE_ID, AP_APP_RELEASE_ID, AP_APP_ID, INSTALL_STATUS) VALUES (?, ?, ?, ?, ?, ?)"; stmt = conn.prepareStatement(sql); for (Device device : deviceList) { stmt.setInt(1, tenantId); @@ -53,6 +53,7 @@ public class GenericSubscriptionDAOImpl extends AbstractDAOImpl implements Subsc stmt.setInt(4, device.getId()); stmt.setInt(5, releaseId); stmt.setInt(6, appId); + stmt.setString(7, installStatus); stmt.addBatch(); if (log.isDebugEnabled()) { log.debug("Adding a mapping to device ID[" + device.getId() + "] to the application [" + appId diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/MAMDeviceConnectorImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/MAMDeviceConnectorImpl.java new file mode 100644 index 0000000000..e0b6539e94 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/MAMDeviceConnectorImpl.java @@ -0,0 +1,125 @@ +/* + * Copyright (c) 2018, 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.application.mgt.core.impl; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.application.mgt.common.AppOperation; +import org.wso2.carbon.device.application.mgt.common.exception.DeviceConnectorException; +import org.wso2.carbon.device.application.mgt.common.services.DeviceConnector; +import org.wso2.carbon.device.application.mgt.core.dao.ApplicationDAO; +import org.wso2.carbon.device.application.mgt.core.dao.SubscriptionDAO; +import org.wso2.carbon.device.application.mgt.core.dao.common.ApplicationManagementDAOFactory; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagementDAOException; +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.core.service.DeviceManagementProviderService; + +import java.util.ArrayList; +import java.util.List; + +public class MAMDeviceConnectorImpl implements DeviceConnector{ + private static final Log log = LogFactory.getLog(MAMDeviceConnectorImpl.class); + private ApplicationDAO applicationDAO; + private SubscriptionDAO subscriptionDAO; + + public MAMDeviceConnectorImpl() { + this.applicationDAO = ApplicationManagementDAOFactory.getApplicationDAO(); + this.subscriptionDAO = ApplicationManagementDAOFactory.getSubscriptionDAO(); + } + + @Override + public Boolean sendOperationToDevice(AppOperation appOperation, DeviceIdentifier deviceIdentifier) throws DeviceConnectorException { + if (String.valueOf(appOperation.getType()).equals("INSTALL")) { + + } else if (String.valueOf(appOperation.getType()).equals("UNINSTALL")) { + + } else if (String.valueOf(appOperation.getType()).equals("UPDATE")) { + + } + return null; + } + + @Override + public Boolean sendOperationToGroup(AppOperation appOperation, String groupID) throws DeviceConnectorException { + return null; + } + + @Override + public Boolean sendOperationToUser(AppOperation appOperation, List userList) throws DeviceConnectorException { + if (String.valueOf(appOperation.getType()).equals("INSTALL")) { + //First subscribe the user to the app. + try { + + subscriptionDAO.subscribeUserToApplication(appOperation.getTenantId(), appOperation.getSubscribedBy(), + userList, appOperation.getApplication().getId(), appOperation.getAppReleaseId()); + for (String username: userList) { + List devices = getDeviceManagementService().getDevicesOfUser(username); + List deviceIdentifiers = convertDeviceToDeviceIdentifier(devices); +// getDeviceManagementService().addOperation(appOperation.getApplication().getDeviceType(), +// operationEKA, devices); + subscriptionDAO.subscribeDeviceToApplication(appOperation.getTenantId(), appOperation.getSubscribedBy(), + devices, appOperation.getApplication().getId(), appOperation.getAppReleaseId(), + String.valueOf(AppOperation.InstallState.PENDING)); + } + } catch (ApplicationManagementDAOException e) { + String msg = "Error subscribing the user to the application Id" + appOperation.getApplication().getId(); + log.error(msg, e); + throw new DeviceConnectorException(msg, e); + } catch (DeviceManagementException e) { + String msg = "Error getting the list of user devices."; + log.error(msg, e); + throw new DeviceConnectorException(msg, e); + } + + } else if (String.valueOf(appOperation.getType()).equals("UNINSTALL")) { + + } else if (String.valueOf(appOperation.getType()).equals("UPDATE")) { + + } + return null; + } + + @Override + public Boolean sendOperationToRole(AppOperation appOperation, String role) throws DeviceConnectorException { + return null; + } + + private List convertDeviceToDeviceIdentifier(List devices) { + List deviceIdentifiers = new ArrayList<>(); + for (Device device:devices) { + deviceIdentifiers.add(new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); + } + return deviceIdentifiers; + } + + public DeviceManagementProviderService getDeviceManagementService() { + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + DeviceManagementProviderService deviceManagementProviderService = + (DeviceManagementProviderService) ctx.getOSGiService(DeviceManagementProviderService.class, null); + if (deviceManagementProviderService == null) { + String msg = "DeviceImpl Management provider service has not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceManagementProviderService; + } +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java index b82057447f..53714937a6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/SubscriptionManagerImpl.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.application.mgt.core.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.application.mgt.common.AppOperation; import org.wso2.carbon.device.application.mgt.common.Application; import org.wso2.carbon.device.application.mgt.common.ApplicationInstallResponse; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; @@ -250,7 +251,7 @@ public class SubscriptionManagerImpl implements SubscriptionManager { } } subscriptionDAO.subscribeDeviceToApplication(tenantId, subscriber, deviceList, application.getId(), - applicationReleaseId); + applicationReleaseId, String.valueOf(AppOperation.InstallState.UNINSTALLED)); } finally { ConnectionManagerUtil.closeDBConnection(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/AndroidApplicationOperationUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/AndroidApplicationOperationUtil.java new file mode 100644 index 0000000000..bb1f5d955a --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/AndroidApplicationOperationUtil.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2018, 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.application.mgt.core.util; + +import org.wso2.carbon.device.application.mgt.common.AppOperation; +import org.wso2.carbon.device.application.mgt.common.EnterpriseApplication; +import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; + +public class AndroidApplicationOperationUtil { +// public static Operation installApp(AppOperation appOperation) { +// ProfileOperation operation = new ProfileOperation(); +// operation.setCode(MDMAppConstants.AndroidConstants.OPCODE_INSTALL_APPLICATION); +// operation.setType(Operation.Type.PROFILE); +// switch (appOperation.getApplication().getType()) { +// case "ENTERPRISE"://TODO: fix with ENUM +// EnterpriseApplication enterpriseApplication = new EnterpriseApplication(); +// enterpriseApplication.setType(appOperation.getApplication().toString()); +// enterpriseApplication.setUrl(application.getLocation()); +// enterpriseApplication.setSchedule(appOperation.getScheduledDateTime()); +// enterpriseApplication.setPackageName(appOperation.getApplication().get); +// operation.setPayLoad(enterpriseApplication.toJSON()); +// break; +// case "PUBLIC": +// setOperationForPublicApp(operation, application); +// break; +// case "WEBAPP": +// setOperationForWebApp(operation, application); +// break; +// default: +// String errorMessage = "Invalid application type."; +// throw new DeviceApplicationException(errorMessage); +// } +// return operation; +// } + +} diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/MDMAppConstants.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/MDMAppConstants.java new file mode 100644 index 0000000000..c1b158f0be --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/MDMAppConstants.java @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2018, 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.application.mgt.core.util; + +public class MDMAppConstants { + public static final String USER = "user"; + public static final String ROLE = "role"; + public static final String IOS = "ios"; + public static final String ANDROID = "android"; + public static final String WEBAPP = "webapp"; + public static final String INSTALL = "install"; + public static final String UPDATE = "update"; + public static final String ACTIVE = "active"; + public static final String ENTERPRISE = "enterprise"; + public static final String DEVICE = "device"; + public static final String MOBILE_DEVICE = "mobileDevice"; + public static final String NEXUS = "nexus"; + public static final String IPHONE = "iphone"; + public static final String NONE = "none"; + public static final String IMAGE_URL = "ImageURL"; + public static final String TYPE = "type"; + public static final String ID = "id"; + + public class IOSConstants { + + private IOSConstants() { + throw new AssertionError(); + } + + public static final String IS_REMOVE_APP = "isRemoveApp"; + public static final String IS_PREVENT_BACKUP = "isPreventBackup"; + public static final String I_TUNES_ID = "iTunesId"; + public static final String LABEL = "label"; + public static final String PUBLIC = "public"; + public static final String OPCODE_INSTALL_ENTERPRISE_APPLICATION = + "INSTALL_ENTERPRISE_APPLICATION"; + public static final String OPCODE_INSTALL_STORE_APPLICATION = "INSTALL_STORE_APPLICATION"; + public static final String OPCODE_INSTALL_WEB_APPLICATION = "WEB_CLIP"; + public static final String OPCODE_REMOVE_APPLICATION = "REMOVE_APPLICATION"; + } + + public class AndroidConstants { + private AndroidConstants() { + throw new AssertionError(); + } + + public static final String OPCODE_INSTALL_APPLICATION = "INSTALL_APPLICATION"; + public static final String OPCODE_UPDATE_APPLICATION = "UPDATE_APPLICATION"; + public static final String OPCODE_UNINSTALL_APPLICATION = "UNINSTALL_APPLICATION"; + } + + public class RegistryConstants { + private RegistryConstants() { + throw new AssertionError(); + } + + public static final String GENERAL_CONFIG_RESOURCE_PATH = "general"; + } + + public class APPManagerConstants { + private static final String APP_MANAGER_MDM_SERVICE_NAME = + "org.wso2.carbon.appmgt.mobile.interfaces.MDMOperations"; + } + +} From a0030a9b7f4ebf015a6afde89287ea0859dd1a48 Mon Sep 17 00:00:00 2001 From: inoshperera Date: Fri, 21 Sep 2018 06:49:45 +0530 Subject: [PATCH 4/4] adding seriasation ID --- .../device/application/mgt/common/AndroidApplication.java | 2 ++ .../device/application/mgt/common/EnterpriseApplication.java | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AndroidApplication.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AndroidApplication.java index 3949e49ff1..139c74b79b 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AndroidApplication.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/AndroidApplication.java @@ -25,6 +25,8 @@ public class AndroidApplication { private String type; private String appIdentifier; + private static final long serialVersionUID = 760343716452548282L; + public String getAppIdentifier() { return appIdentifier; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/EnterpriseApplication.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/EnterpriseApplication.java index 2adf626059..b1b3aac4c1 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/EnterpriseApplication.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/EnterpriseApplication.java @@ -30,7 +30,9 @@ public class EnterpriseApplication extends AndroidApplication implements Seriali private String url; private String schedule; private String packageName; - + + private static final long serialVersionUID = 660343716452348222L; + public String getSchedule() { return schedule; }