Merge pull request #652 from inoshperera/master

moving app subscription to a new thread
revert-dabc3590
Chatura Dilan 8 years ago committed by GitHub
commit 95f61ea7a0

@ -20,6 +20,7 @@ import org.apache.commons.logging.LogFactory;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException; import org.json.simple.parser.ParseException;
import org.wso2.carbon.appmgt.impl.service.ServiceReferenceHolder;
import org.wso2.carbon.appmgt.mdm.osgiconnector.mdmmgt.beans.MobileApp; import org.wso2.carbon.appmgt.mdm.osgiconnector.mdmmgt.beans.MobileApp;
import org.wso2.carbon.appmgt.mdm.osgiconnector.mdmmgt.beans.MobileAppTypes; import org.wso2.carbon.appmgt.mdm.osgiconnector.mdmmgt.beans.MobileAppTypes;
import org.wso2.carbon.appmgt.mdm.osgiconnector.mdmmgt.common.DeviceApplicationException; import org.wso2.carbon.appmgt.mdm.osgiconnector.mdmmgt.common.DeviceApplicationException;
@ -32,6 +33,7 @@ import org.wso2.carbon.appmgt.mobile.beans.ApplicationOperationDevice;
import org.wso2.carbon.appmgt.mobile.interfaces.ApplicationOperations; import org.wso2.carbon.appmgt.mobile.interfaces.ApplicationOperations;
import org.wso2.carbon.appmgt.mobile.mdm.App; import org.wso2.carbon.appmgt.mobile.mdm.App;
import org.wso2.carbon.appmgt.mobile.mdm.Device; import org.wso2.carbon.appmgt.mobile.mdm.Device;
import org.wso2.carbon.appmgt.mobile.store.Generic;
import org.wso2.carbon.appmgt.mobile.utils.MobileApplicationException; import org.wso2.carbon.appmgt.mobile.utils.MobileApplicationException;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.DeviceManagementException;
@ -39,6 +41,15 @@ import org.wso2.carbon.device.mgt.common.Platform;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException;
import org.wso2.carbon.device.mgt.common.operation.mgt.Activity; import org.wso2.carbon.device.mgt.common.operation.mgt.Activity;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.appmgt.mobile.utils.User;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.registry.api.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.session.UserRegistry;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.carbon.user.api.UserStoreManager;
import org.wso2.carbon.user.core.service.RealmService;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -47,6 +58,9 @@ import java.util.Properties;
public class ApplicationOperationsImpl implements ApplicationOperations { public class ApplicationOperationsImpl implements ApplicationOperations {
private static final Log log = LogFactory.getLog(ApplicationOperationsImpl.class); private static final Log log = LogFactory.getLog(ApplicationOperationsImpl.class);
public static final String MEDIA_TYPE_XML = "application/xml";
public static final String INSTALL = "install";
public static final String UNINSTALL = "uninstall";
/** /**
* @param applicationOperationAction holds the information needs to perform an action on mdm. * @param applicationOperationAction holds the information needs to perform an action on mdm.
@ -73,8 +87,8 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
getDevicesOfUser(userName); getDevicesOfUser(userName);
for (org.wso2.carbon.device.mgt.common.Device device : deviceList) { for (org.wso2.carbon.device.mgt.common.Device device : deviceList) {
if(MDMAppConstants.WEBAPP.equals(applicationOperationAction.getApp().getPlatform()) || if (MDMAppConstants.WEBAPP.equals(applicationOperationAction.getApp().getPlatform()) ||
applicationOperationAction.getApp().getPlatform().equalsIgnoreCase(device.getType())){ applicationOperationAction.getApp().getPlatform().equalsIgnoreCase(device.getType())) {
if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo(). if (MDMAppConstants.ACTIVE.equalsIgnoreCase(device.getEnrolmentInfo().
getStatus().toString())) { getStatus().toString())) {
deviceIdentifiers.add(getDeviceIdentifierByDevice(device)); deviceIdentifiers.add(getDeviceIdentifierByDevice(device));
@ -236,17 +250,15 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
List<Device> devices; List<Device> devices;
List<org.wso2.carbon.device.mgt.common.Device> deviceList = null; List<org.wso2.carbon.device.mgt.common.Device> deviceList = null;
try { try {
if(MDMAppConstants.WEBAPP.equals if (MDMAppConstants.WEBAPP.equals
(applicationOperationDevice.getPlatform())) { (applicationOperationDevice.getPlatform())) {
deviceList = MDMServiceAPIUtils deviceList = MDMServiceAPIUtils
.getDeviceManagementService(applicationOperationDevice.getTenantId()). .getDeviceManagementService(applicationOperationDevice.getTenantId()).
getDevicesOfUser( getDevicesOfUser(applicationOperationDevice.getCurrentUser().getUsername());
applicationOperationDevice.getCurrentUser().getUsername());
} else { } else {
deviceList = MDMServiceAPIUtils deviceList = MDMServiceAPIUtils
.getDeviceManagementService(applicationOperationDevice.getTenantId()). .getDeviceManagementService(applicationOperationDevice.getTenantId()).
getDevicesOfUser( getDevicesOfUser(applicationOperationDevice.getCurrentUser().getUsername(),
applicationOperationDevice.getCurrentUser().getUsername(),
applicationOperationDevice.getPlatform()); applicationOperationDevice.getPlatform());
} }
devices = new ArrayList<>(deviceList.size()); devices = new ArrayList<>(deviceList.size());
@ -311,5 +323,138 @@ public class ApplicationOperationsImpl implements ApplicationOperations {
} }
} }
public static UserStoreManager getUserStoreManager() throws UserStoreException {
RealmService realmService;
UserStoreManager userStoreManager;
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
realmService = (RealmService) ctx.getOSGiService(RealmService.class, null);
if (realmService == null) {
String msg = "Realm service has not initialized.";
log.error(msg);
throw new IllegalStateException(msg);
}
int tenantId = ctx.getTenantId();
userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager();
return userStoreManager;
}
class ApplicationSubscription extends Thread {
User currentUser;
String action;
App app;
int tenantId;
String type;
String[] params;
UserStoreManager userStoreManager;
UserRegistry userRegistry;
public ApplicationSubscription(User currentUser, String action, App app, int tenantId, final String type,
final String[] params) {
this.currentUser = currentUser;
this.action = action;
this.app = app;
this.tenantId = tenantId;
this.type = type;
this.params = params;
}
@Override
public void run() {
try {
PrivilegedCarbonContext.startTenantFlow();
PrivilegedCarbonContext privilegedCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext();
privilegedCarbonContext.setTenantId(tenantId);
RealmService realmService = (RealmService) privilegedCarbonContext.getOSGiService(RealmService.class, null);
if (realmService == null) {
String msg = "RealmService is not initialized";
log.error(msg);
throw new IllegalStateException(msg);
}
String tenantDomain;
try {
tenantDomain = realmService.getTenantManager().getDomain(tenantId);
privilegedCarbonContext.setTenantDomain(tenantDomain);
privilegedCarbonContext.setUsername(currentUser.getUsername());
userStoreManager = getUserStoreManager();
userRegistry = ServiceReferenceHolder.getInstance().getRegistryService()
.getGovernanceUserRegistry(currentUser.getUsername(), tenantId);
} catch (UserStoreException e) {
log.error("Error occured while fetching user store", e);
} catch (RegistryException e) {
log.error("Error occured while fetching registry instance", e);
}
String basePath = "/users/";
String subscriptionPath = "/subscriptions/mobileapp/";
String path;
if (type != null && type.equals("role")) {
for (String param : params) {
String[] users;
if (log.isDebugEnabled()) {
log.debug("role being added:" + param);
}
try {
users = userStoreManager.getUserListOfRole(param);
for (String user : users) {
path = basePath + user + subscriptionPath + app.getId();
updateSubscription(action, path, user, userRegistry);
}
} catch (UserStoreException e) {
log.error("Error occured while getting user list of role " + param, e);
}
}
} else if (type != null && type.equals("user")) {
for (String user : params) {
if (log.isDebugEnabled()) {
log.debug("user:" + user);
}
path = basePath + user + subscriptionPath + app.getId();
updateSubscription(action, path, user, userRegistry);
}
} else if (type != null && type.equals("device")) {
log.debug("device user:" + currentUser.getUsername());
path = basePath + currentUser.getUsername() + subscriptionPath + app.getId();
updateSubscription(action, path, currentUser.getUsername(), userRegistry);
}
log.info("registry subscription complete.");
} finally {
PrivilegedCarbonContext.endTenantFlow();
}
}
}
private void updateSubscription(String action, String path, String username, UserRegistry userRegistry) {
if (log.isDebugEnabled()) {
log.debug("update subscribe user:" + username + " , action:" + action + " ,path:" + path);
}
if (action != null && action.equals(INSTALL)) {
try {
if (!userRegistry.resourceExists(path)) {
Resource resource = userRegistry.newResource();
resource.setMediaType(MEDIA_TYPE_XML);
userRegistry.put(path, resource);
Generic generic = new Generic();
generic.showAppVisibilityToUser(path, username, "ALLOW");
}
} catch (RegistryException e) {
log.error("Error occured while accessing registry.", e);
}
} else if (action != null && action.equals(UNINSTALL)) {
try {
if (userRegistry.resourceExists(path)) {
userRegistry.delete(path);
Generic generic = new Generic();
generic.showAppVisibilityToUser(path, username, "DENY");
}
} catch (RegistryException e) {
log.error("Error occured while accessing registry.", e);
}
}
}
} }

Loading…
Cancel
Save