Modify logic of saving DeviceTypePluginDAOManager to support multi tenancy

revert-70aa11f8
Saad Sahibjan 5 years ago
parent d6d91e4bc2
commit 0c7843f2fe

@ -59,6 +59,7 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceDAOD
import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager; import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeDeployerPayloadException; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeDeployerPayloadException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypePluginExtensionException;
import org.wso2.carbon.device.mgt.extensions.device.type.template.feature.ConfigurationBasedFeatureManager; import org.wso2.carbon.device.mgt.extensions.device.type.template.feature.ConfigurationBasedFeatureManager;
import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypePluginConstants; import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypePluginConstants;
import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypeUtils; import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypeUtils;
@ -223,10 +224,19 @@ public class DeviceTypeManager implements DeviceManager {
* device type plugin in working with its DAO components * device type plugin in working with its DAO components
*/ */
private void setDeviceTypePluginManager() { private void setDeviceTypePluginManager() {
if (StringUtils.isNotEmpty(deviceType) && deviceTypePluginDAOManager != null) { if (StringUtils.isNotEmpty(deviceType)) {
DeviceTypePluginExtensionService deviceTypeManagerExtensionService = if (deviceTypePluginDAOManager != null) {
new DeviceTypePluginExtensionServiceImpl(); DeviceTypePluginExtensionService deviceTypeManagerExtensionService =
deviceTypeManagerExtensionService.addPluginDAOManager(deviceType, deviceTypePluginDAOManager); new DeviceTypePluginExtensionServiceImpl();
deviceTypeManagerExtensionService.addPluginDAOManager(deviceType, deviceTypePluginDAOManager);
} else {
log.warn("Could not save DeviceTypePluginDAOManager for device type: " + deviceType +
" since DeviceTypePluginDAOManager is null.");
}
} else {
String msg = "Could not save DeviceTypePluginDAOManager since device type is null or empty.";
log.error(msg);
throw new DeviceTypePluginExtensionException(msg);
} }
} }

@ -17,7 +17,11 @@
*/ */
package org.wso2.carbon.device.mgt.extensions.device.type.template; package org.wso2.carbon.device.mgt.extensions.device.type.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.extensions.device.type.template.dao.DeviceTypePluginDAOManager; import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager;
import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypePluginExtensionException;
import org.wso2.carbon.device.mgt.extensions.spi.DeviceTypePluginExtensionService; import org.wso2.carbon.device.mgt.extensions.spi.DeviceTypePluginExtensionService;
import java.util.HashMap; import java.util.HashMap;
@ -25,19 +29,38 @@ import java.util.Map;
public class DeviceTypePluginExtensionServiceImpl implements DeviceTypePluginExtensionService { public class DeviceTypePluginExtensionServiceImpl implements DeviceTypePluginExtensionService {
private static final Log log = LogFactory.getLog(DeviceTypePluginExtensionServiceImpl.class);
private static volatile Map<String, DeviceTypePluginDAOManager> pluginDAOManagers = new HashMap<>(); private static volatile Map<String, DeviceTypePluginDAOManager> pluginDAOManagers = new HashMap<>();
@Override @Override
public void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager) { public void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager) {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (pluginDAOManager != null) { if (pluginDAOManager != null) {
if (!pluginDAOManagers.containsKey(deviceType)) { if (!pluginDAOManagers.containsKey(tenantId + deviceType)) {
pluginDAOManagers.put(deviceType, pluginDAOManager); if (log.isDebugEnabled()) {
log.debug("Saving DeviceTypePluginDAOManager against tenant id " + tenantId +
" and device type: " + deviceType);
}
pluginDAOManagers.put(tenantId + deviceType, pluginDAOManager);
} }
} }
} }
@Override @Override
public DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) { public DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) {
return pluginDAOManagers.get(deviceType); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (pluginDAOManagers.containsKey(tenantId + deviceType)) {
if (log.isDebugEnabled()) {
log.debug("Retrieving DeviceTypePluginDAOManager against tenant id " + tenantId +
" and device type: " + deviceType);
}
return pluginDAOManagers.get(tenantId + deviceType);
} else {
String msg = "DeviceTypePluginDAOManager could not be found against tenant id " + tenantId +
" and device type: " + deviceType;
log.error(msg);
throw new DeviceTypePluginExtensionException(msg);
}
} }
} }

@ -0,0 +1,12 @@
package org.wso2.carbon.device.mgt.extensions.device.type.template.exception;
public class DeviceTypePluginExtensionException extends RuntimeException {
public DeviceTypePluginExtensionException(String msg) {
super(msg);
}
public DeviceTypePluginExtensionException(String msg, Throwable cause) {
super(msg, cause);
}
}

@ -26,14 +26,14 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceType
public interface DeviceTypePluginExtensionService { public interface DeviceTypePluginExtensionService {
/** /**
* Save device type specific pluginDAOManager in a HashMap * Save device type specific DeviceTypePluginDAOManager in a HashMap againast tenant ID and device type
* @param deviceType - Type of the device (i.e; android, ios, windows) * @param deviceType - Type of the device (i.e; android, ios, windows)
* @param pluginDAOManager - Device type plugin DAO manager instance to be saved against device type * @param pluginDAOManager - Device type plugin DAO manager instance to be saved against device type
*/ */
void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager); void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager);
/** /**
* Retrieve the DeviceTypePluginDAOManager instance given the device type * Retrieve the DeviceTypePluginDAOManager instance against tenant ID and given device type
* @param deviceType - Type of the device (i.e; android, ios, windows) * @param deviceType - Type of the device (i.e; android, ios, windows)
* @return an Instance of {@link DeviceTypePluginDAOManager} * @return an Instance of {@link DeviceTypePluginDAOManager}
*/ */

Loading…
Cancel
Save