From f215b5ee440fc4647df2a16ed808d964d93c5bc1 Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Wed, 7 Jun 2017 17:34:06 +0530 Subject: [PATCH 1/2] Added comments to the device status configs. --- .../src/test/resources/sample.xml | 24 +++++++++++++++++++ .../src/main/resources/conf/cdm-config.xml | 1 + 2 files changed, 25 insertions(+) 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 index 414bcaa8dd..a4a89876ae 100644 --- 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 @@ -125,4 +125,28 @@ + + + DEVICE_INFO + APPLICATION_LIST + DEVICE_LOCATION + + + + + + false + 300 + 600 + 300 + \ No newline at end of file diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index 023166a6c1..f1178cab4f 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -66,6 +66,7 @@ 20 20 + true From 43ba1e1b430958b4ee1b9975b1c8cfc9ac726ba5 Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Tue, 13 Jun 2017 17:08:26 +0530 Subject: [PATCH 2/2] Added Device-Cache for caching device objects which resolves harshanl/product-iots#1005 --- .../device/mgt/common/DeviceIdentifier.java | 2 +- .../mgt/core/DeviceManagementConstants.java | 3 + ...ApplicationManagerProviderServiceImpl.java | 33 +++-- .../device/mgt/core/cache/DeviceCacheKey.java | 81 ++++++++++++ .../mgt/core/cache/DeviceCacheManager.java | 37 ++++++ .../cache/impl/DeviceCacheManagerImpl.java | 109 ++++++++++++++++ .../core/config/DeviceManagementConfig.java | 13 +- .../cache/DeviceCacheConfiguration.java | 47 +++++++ .../carbon/device/mgt/core/dao/DeviceDAO.java | 2 +- .../DeviceManagementServiceComponent.java | 3 + .../NotificationManagementServiceImpl.java | 19 ++- .../operation/mgt/OperationManagerImpl.java | 15 +-- .../DeviceManagementProviderServiceImpl.java | 118 ++++++++---------- .../task/impl/DeviceStatusMonitoringTask.java | 10 ++ .../mgt/core/util/DeviceManagerUtil.java | 64 +++++++++- .../core/impl/PolicyInformationPointImpl.java | 2 +- .../repository/conf/cdm-config.xml | 50 ++++---- .../src/main/resources/conf/cdm-config.xml | 4 + 18 files changed, 482 insertions(+), 130 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheKey.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManager.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/DeviceCacheManagerImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/DeviceCacheConfiguration.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java index 5b0e234218..f37e7e1d25 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceIdentifier.java @@ -73,4 +73,4 @@ public class DeviceIdentifier implements Serializable{ ", type='" + type + '\'' + '}'; } -} +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java index 15df1e4435..af07800f3d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementConstants.java @@ -22,6 +22,9 @@ import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; public final class DeviceManagementConstants { + public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; + public static final String DEVICE_CACHE = "DEVICE_CACHE"; + public static final class Common { private Common() { throw new AssertionError(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index 756f09e32a..34080c188d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -21,14 +21,22 @@ package org.wso2.carbon.device.mgt.core.app.mgt; 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.*; +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.InvalidDeviceException; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.Application; 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.Operation; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig; -import org.wso2.carbon.device.mgt.core.dao.*; +import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO; +import org.wso2.carbon.device.mgt.core.dao.ApplicationMappingDAO; +import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import java.sql.SQLException; @@ -190,10 +198,9 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem List applications) throws ApplicationManagementException { List installedAppList = getApplicationListForDevice(deviceIdentifier); try { + Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceIdentifier, + false); int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - DeviceManagementDAOFactory.beginTransaction(); - Device device = deviceDAO.getDevice(deviceIdentifier, tenantId); - if (log.isDebugEnabled()) { log.debug("Device:" + device.getId() + ":identifier:" + deviceIdentifier.getId()); } @@ -212,6 +219,7 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem appIdsToRemove.add(installedApp.getId()); } } + DeviceManagementDAOFactory.beginTransaction(); applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove, tenantId); Application installedApp; List applicationIds = new ArrayList<>(); @@ -247,6 +255,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem throw new ApplicationManagementException("Error occurred saving application list to the device", e); } catch (TransactionManagementException e) { throw new ApplicationManagementException("Error occurred while initializing transaction", e); + } catch (DeviceManagementException e) { + throw new ApplicationManagementException("Error occurred obtaining the device object.", e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -255,11 +265,9 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem @Override public List getApplicationListForDevice( DeviceIdentifier deviceId) throws ApplicationManagementException { - Device device; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - DeviceManagementDAOFactory.openConnection(); - device = deviceDAO.getDevice(deviceId, tenantId); + Device device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId, + false); if (device == null) { if (log.isDebugEnabled()) { log.debug("No device is found upon the device identifier '" + deviceId.getId() + @@ -267,15 +275,18 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } return null; } + DeviceManagementDAOFactory.openConnection(); return applicationDAO.getInstalledApplications(device.getId()); } catch (DeviceManagementDAOException e) { throw new ApplicationManagementException("Error occurred while fetching the Application List of '" + deviceId.getType() + "' device carrying the identifier'" + deviceId.getId(), e); } catch (SQLException e) { throw new ApplicationManagementException("Error occurred while opening a connection to the data source", e); + } catch (DeviceManagementException e) { + throw new ApplicationManagementException("Error occurred while fetching the device of '" + + deviceId.getType() + "' carrying the identifier'" + deviceId.getId(), e); } finally { DeviceManagementDAOFactory.closeConnection(); } } - -} +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheKey.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheKey.java new file mode 100644 index 0000000000..79f3d7507c --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheKey.java @@ -0,0 +1,81 @@ +/* + * 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.mgt.core.cache; + +import java.util.Objects; + +/** + * This represents a Key object used in DeviceCache. + */ +public class DeviceCacheKey { + + private String deviceId; + private String deviceType; + private int tenantId; + private volatile int hashCode; + + public String getDeviceId() { + return deviceId; + } + + public void setDeviceId(String deviceId) { + this.deviceId = deviceId; + } + + public String getDeviceType() { + return deviceType; + } + + public void setDeviceType(String deviceType) { + this.deviceType = deviceType; + } + + public int getTenantId() { + return tenantId; + } + + public void setTenantId(int tenantId) { + this.tenantId = tenantId; + } + + @Override + public boolean equals(Object obj) { + if (obj == null) { + return false; + } + if (!DeviceCacheKey.class.isAssignableFrom(obj.getClass())) { + return false; + } + final DeviceCacheKey other = (DeviceCacheKey) obj; + String thisId = this.deviceId + "-" + this.deviceType + "_" + this.tenantId; + String otherId = other.deviceId + "-" + other.deviceType + "_" + other.tenantId; + if (!thisId.equals(otherId)) { + return false; + } + return true; + } + + @Override + public int hashCode() { + if (hashCode == 0) { + hashCode = Objects.hash(deviceId, deviceType, tenantId); + } + return hashCode; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManager.java new file mode 100644 index 0000000000..138fce487d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/DeviceCacheManager.java @@ -0,0 +1,37 @@ +/* + * 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.mgt.core.cache; + +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.Device; + +import java.util.List; + +/** + * This defines the contract to be implemented by DeviceCacheManager which holds the necessary functionalities to + * manage a cache of Device objects. + */ +public interface DeviceCacheManager { + + void addDeviceToCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId); + void removeDeviceFromCache(DeviceIdentifier identifier, int tenantId); + void removeDevicesFromCache(List deviceList, int tenantId); + void updateDeviceInCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId); + Device getDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId); +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/DeviceCacheManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/DeviceCacheManagerImpl.java new file mode 100644 index 0000000000..26da3c8f54 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/DeviceCacheManagerImpl.java @@ -0,0 +1,109 @@ +/* + * 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.mgt.core.cache.impl; + +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.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey; +import org.wso2.carbon.device.mgt.core.cache.DeviceCacheManager; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; + +import javax.cache.Cache; +import java.util.List; + +/** + * Implementation of DeviceCacheManager. + */ +public class DeviceCacheManagerImpl implements DeviceCacheManager { + + private static final Log log = LogFactory.getLog(DeviceCacheManagerImpl.class); + + private static DeviceCacheManagerImpl deviceCacheManager; + + private DeviceCacheManagerImpl() { + } + + public static DeviceCacheManagerImpl getInstance() { + if (deviceCacheManager == null) { + synchronized (DeviceCacheManagerImpl.class) { + if (deviceCacheManager == null) { + deviceCacheManager = new DeviceCacheManagerImpl(); + } + } + } + return deviceCacheManager; + } + + @Override + public void addDeviceToCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) { + Cache lCache = DeviceManagerUtil.getDeviceCache(); + DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); + if (lCache.containsKey(cacheKey)) { + this.updateDeviceInCache(deviceIdentifier, device, tenantId); + } else { + lCache.put(cacheKey, device); + } + } + + @Override + public void removeDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) { + Cache lCache = DeviceManagerUtil.getDeviceCache(); + DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.remove(cacheKey); + } + } + + @Override + public void removeDevicesFromCache(List deviceList, int tenantId) { + Cache lCache = DeviceManagerUtil.getDeviceCache(); + for (DeviceIdentifier deviceIdentifier : deviceList) { + DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.remove(cacheKey); + } + } + } + + @Override + public void updateDeviceInCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) { + Cache lCache = DeviceManagerUtil.getDeviceCache(); + DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.replace(cacheKey, device); + } + } + + @Override + public Device getDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) { + Cache lCache = DeviceManagerUtil.getDeviceCache(); + return lCache.get(getCacheKey(deviceIdentifier, tenantId)); + } + + + private DeviceCacheKey getCacheKey(DeviceIdentifier deviceIdentifier, int tenantId) { + DeviceCacheKey deviceCacheKey = new DeviceCacheKey(); + deviceCacheKey.setDeviceId(deviceIdentifier.getId()); + deviceCacheKey.setDeviceType(deviceIdentifier.getType()); + deviceCacheKey.setTenantId(tenantId); + return deviceCacheKey; + } +} \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java index 5bd8a7849d..f1bcee0ea6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/DeviceManagementConfig.java @@ -17,6 +17,7 @@ */ package org.wso2.carbon.device.mgt.core.config; +import org.wso2.carbon.device.mgt.core.config.cache.DeviceCacheConfiguration; import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations; import org.wso2.carbon.device.mgt.core.config.pagination.PaginationConfiguration; import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; @@ -25,9 +26,7 @@ import org.wso2.carbon.device.mgt.core.config.status.task.DeviceStatusTaskConfig import org.wso2.carbon.device.mgt.core.config.task.TaskConfiguration; import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlElementWrapper; import javax.xml.bind.annotation.XmlRootElement; -import java.util.List; /** * Represents Device Mgt configuration. @@ -43,6 +42,7 @@ public final class DeviceManagementConfig { private PaginationConfiguration paginationConfiguration; private PushNotificationConfiguration pushNotificationConfiguration; private DeviceStatusTaskConfig deviceStatusTaskConfig; + private DeviceCacheConfiguration deviceCacheConfiguration; @XmlElement(name = "ManagementRepository", required = true) @@ -108,5 +108,14 @@ public final class DeviceManagementConfig { public void setDeviceStatusTaskConfig(DeviceStatusTaskConfig deviceStatusTaskConfig) { this.deviceStatusTaskConfig = deviceStatusTaskConfig; } + + @XmlElement(name = "DeviceCacheConfiguration", required = true) + public DeviceCacheConfiguration getDeviceCacheConfiguration() { + return deviceCacheConfiguration; + } + + public void setDeviceCacheConfiguration(DeviceCacheConfiguration deviceCacheConfiguration) { + this.deviceCacheConfiguration = deviceCacheConfiguration; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/DeviceCacheConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/DeviceCacheConfiguration.java new file mode 100644 index 0000000000..34a813fbcf --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/DeviceCacheConfiguration.java @@ -0,0 +1,47 @@ +/* + * 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.mgt.core.config.cache; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement(name = "DeviceCacheConfiguration") +public class DeviceCacheConfiguration { + + private boolean isEnabled; + private int expiryTime; + + @XmlElement(name = "Enable", required = true) + public boolean isEnabled() { + return isEnabled; + } + + public void setEnabled(boolean enabled) { + isEnabled = enabled; + } + + @XmlElement(name = "ExpiryTime", required = true) + public int getExpiryTime() { + return expiryTime; + } + + public void setExpiryTime(int expiryTime) { + this.expiryTime = expiryTime; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index 1eb39fc502..14ac17dd95 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -158,7 +158,7 @@ public interface DeviceDAO { HashMap getDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementDAOException; /** - * This method is used to retrieve a device of a given id. + * This method is used to retrieve a device of a given tenant id. * * @param deviceId device id. * @param tenantId tenant id. diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 0c1a62471d..490424460c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -55,6 +55,7 @@ import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -150,6 +151,8 @@ public class DeviceManagementServiceComponent { GroupManagementDAOFactory.init(dsConfig); NotificationManagementDAOFactory.init(dsConfig); OperationManagementDAOFactory.init(dsConfig); + /*Initialize the device cache*/ + DeviceManagerUtil.initializeDeviceCache(); /* Initialize Operation Manager */ this.initOperationsManager(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java index 88883a4e26..90867dbc3d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/notification/mgt/NotificationManagementServiceImpl.java @@ -22,6 +22,7 @@ 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.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EntityDoesNotExistException; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -30,8 +31,8 @@ import org.wso2.carbon.device.mgt.common.notification.mgt.Notification; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementService; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationDAO; import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory; import org.wso2.carbon.device.mgt.core.notification.mgt.dao.util.NotificationDAOUtil; @@ -65,7 +66,7 @@ public class NotificationManagementServiceImpl implements NotificationManagement int notificationId; int tenantId = NotificationDAOUtil.getTenantId(); - Device device = this.getDevice(deviceId, tenantId); + Device device = this.getDevice(deviceId); if (device == null) { throw new EntityDoesNotExistException("No device is found with type '" + deviceId.getType() + "' and id '" + deviceId.getId() + "'"); @@ -87,19 +88,13 @@ public class NotificationManagementServiceImpl implements NotificationManagement return true; } - private Device getDevice(DeviceIdentifier deviceId, int tenantId) throws NotificationManagementException { + private Device getDevice(DeviceIdentifier deviceId) throws NotificationManagementException { Device device; try { - DeviceManagementDAOFactory.openConnection(); - device = deviceDAO.getDevice(deviceId, tenantId); - } catch (SQLException e) { - throw new NotificationManagementException("Error occurred while opening a connection to" + - " the data source", e); - } catch (DeviceManagementDAOException e) { - throw new NotificationManagementException("Error occurred while retriving device data for " + + device = DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId, false); + } catch (DeviceManagementException e) { + throw new NotificationManagementException("Error occurred while retrieving device data for " + " adding notification", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); } return device; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 7219440c91..dc096eb130 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -23,6 +23,7 @@ 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.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.InvalidDeviceException; import org.wso2.carbon.device.mgt.common.MonitoringOperation; @@ -293,19 +294,11 @@ public class OperationManagerImpl implements OperationManager { } private Device getDevice(DeviceIdentifier deviceId) throws OperationManagementException { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { - DeviceManagementDAOFactory.openConnection(); - return deviceDAO.getDevice(deviceId, tenantId); - } catch (SQLException e) { - throw new OperationManagementException("Error occurred while opening a connection the data " + - "source", e); - } catch (DeviceManagementDAOException e) { - OperationManagementDAOFactory.rollbackTransaction(); + return DeviceManagementDataHolder.getInstance().getDeviceManagementProvider().getDevice(deviceId, false); + } catch (DeviceManagementException e) { throw new OperationManagementException( - "Error occurred while retrieving device info", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); + "Error occurred while retrieving device info.", e); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 12b873a228..8969b0e0c3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -57,6 +57,7 @@ import org.wso2.carbon.device.mgt.common.push.notification.NotificationStrategy; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; +import org.wso2.carbon.device.mgt.core.cache.impl.DeviceCacheManagerImpl; import org.wso2.carbon.device.mgt.core.dao.ApplicationDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceDAO; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; @@ -282,8 +283,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv boolean status = deviceManager.modifyEnrollment(device); try { int tenantId = this.getTenantId(); + Device currentDevice = this.getDevice(deviceIdentifier, false); DeviceManagementDAOFactory.beginTransaction(); - Device currentDevice = deviceDAO.getDevice(deviceIdentifier, tenantId); device.setId(currentDevice.getId()); if (device.getEnrolmentInfo().getId() == 0) { device.getEnrolmentInfo().setId(currentDevice.getEnrolmentInfo().getId()); @@ -294,6 +295,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceDAO.updateDevice(device, tenantId); enrollmentDAO.updateEnrollment(device.getEnrolmentInfo()); DeviceManagementDAOFactory.commitTransaction(); + this.removeDeviceFromCache(deviceIdentifier); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while modifying the device " + @@ -335,9 +337,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } try { int tenantId = this.getTenantId(); - DeviceManagementDAOFactory.beginTransaction(); - Device device = deviceDAO.getDevice(deviceId, tenantId); + Device device = this.getDevice(deviceId, false); if (device == null) { if (log.isDebugEnabled()) { log.debug("Device not found for id '" + deviceId.getId() + "'"); @@ -353,10 +354,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.REMOVED); + DeviceManagementDAOFactory.beginTransaction(); enrollmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId); deviceDAO.updateDevice(device, tenantId); DeviceManagementDAOFactory.commitTransaction(); + this.removeDeviceFromCache(deviceId); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while dis-enrolling '" + deviceId.getType() + @@ -371,19 +374,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean isEnrolled(DeviceIdentifier deviceId) throws DeviceManagementException { - try { - DeviceManagementDAOFactory.openConnection(); - Device device = deviceDAO.getDevice(deviceId, this.getTenantId()); - if (device != null) { - return true; - } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the enrollment information device for" + - "id '" + deviceId.getId() + "'", e); - } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); + Device device = this.getDevice(deviceId, false); + if (device != null) { + return true; } return false; } @@ -582,25 +575,29 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, boolean requireDeviceInfo) throws DeviceManagementException { - Device device; - try { - DeviceManagementDAOFactory.openConnection(); - device = deviceDAO.getDevice(deviceId, this.getTenantId()); - if (device == null) { - String msg = "No device is found upon the type '" + deviceId.getType() + "' and id '" + - deviceId.getId() + "'"; - if (log.isDebugEnabled()) { - log.debug(msg); + int tenantId = this.getTenantId(); + Device device = this.getDeviceFromCache(deviceId); + if (device == null) { + try { + DeviceManagementDAOFactory.openConnection(); + device = deviceDAO.getDevice(deviceId, tenantId); + if (device == null) { + String msg = "No device is found upon the type '" + deviceId.getType() + "' and id '" + + deviceId.getId() + "'"; + if (log.isDebugEnabled()) { + log.debug(msg); + } + return null; } - return null; + this.addDeviceToCache(deviceId, device); + } catch (DeviceManagementDAOException e) { + throw new DeviceManagementException("Error occurred while obtaining the device for id " + + "'" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceId.getId() + "'", e); - } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); } if (requireDeviceInfo) { device = this.getAllDeviceInfo(device); @@ -690,25 +687,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDeviceWithTypeProperties(DeviceIdentifier deviceId) throws DeviceManagementException { - Device device; - try { - DeviceManagementDAOFactory.openConnection(); - device = deviceDAO.getDevice(deviceId, this.getTenantId()); - if (device == null) { - if (log.isDebugEnabled()) { - log.debug("No device is found upon the type '" + deviceId.getType() + "' and id '" + - deviceId.getId() + "'"); - } - return null; - } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceId.getId() + "'", e); - } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } + Device device = this.getDevice(deviceId, false); DeviceManager deviceManager = this.getDeviceManager(device.getType()); if (deviceManager == null) { @@ -883,14 +862,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv EnrolmentInfo.Status status) throws DeviceManagementException { try { boolean success = false; - DeviceManagementDAOFactory.beginTransaction(); int tenantId = this.getTenantId(); - Device device = deviceDAO.getDevice(deviceId, tenantId); + Device device = this.getDevice(deviceId, false); EnrolmentInfo enrolmentInfo = device.getEnrolmentInfo(); + DeviceManagementDAOFactory.beginTransaction(); if (enrolmentInfo != null) { success = enrollmentDAO.setStatus(enrolmentInfo.getId(), currentOwner, status, tenantId); } DeviceManagementDAOFactory.commitTransaction(); + this.removeDeviceFromCache(deviceId); return success; } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); @@ -899,7 +879,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throw new DeviceManagementException("Error occurred while initiating transaction", e); } finally { DeviceManagementDAOFactory.closeConnection(); - } } @@ -1210,7 +1189,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return this.getAllDevicesOfRole(role, true); } - //TODO FIX THIS @Override public List getAllDevicesOfRole(String role, boolean requireDeviceInfo) throws DeviceManagementException { List devices = new ArrayList<>(); @@ -1433,19 +1411,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean isEnrolled(DeviceIdentifier deviceId, String user) throws DeviceManagementException { - try { - DeviceManagementDAOFactory.openConnection(); - Device device = deviceDAO.getDevice(deviceId, this.getTenantId()); - if (device != null && device.getEnrolmentInfo().getOwner().equals(user)) { - return true; - } - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the enrollment information device for" + - "id '" + deviceId.getId() + "' and user : " + user, e); - } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); + Device device = this.getDevice(deviceId, false); + if (device != null && device.getEnrolmentInfo() != null && device.getEnrolmentInfo().getOwner().equals(user)) { + return true; } return false; } @@ -1726,4 +1694,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return device; } + + private Device getDeviceFromCache(DeviceIdentifier deviceIdentifier) { + return DeviceCacheManagerImpl.getInstance().getDeviceFromCache(deviceIdentifier, this.getTenantId()); + } + + private void addDeviceToCache(DeviceIdentifier deviceIdentifier, Device device) { + DeviceCacheManagerImpl.getInstance().addDeviceToCache(deviceIdentifier, device, this.getTenantId()); + } + + private void removeDeviceFromCache(DeviceIdentifier deviceIdentifier) { + DeviceCacheManagerImpl.getInstance().removeDeviceFromCache(deviceIdentifier, this.getTenantId()); + } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java index 6bc51da2ba..101e549c63 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusMonitoringTask.java @@ -21,6 +21,7 @@ package org.wso2.carbon.device.mgt.core.status.task.impl; import com.google.gson.Gson; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.TransactionManagementException; @@ -66,8 +67,10 @@ public class DeviceStatusMonitoringTask implements Task { public void execute() { List operationEnrolmentMappings = null; List enrolmentInfoTobeUpdated = new ArrayList<>(); + List identifiers = new ArrayList<>(); Map lastActivities = null; EnrolmentInfo enrolmentInfo; + DeviceIdentifier deviceIdentifier; try { operationEnrolmentMappings = this.getOperationEnrolmentMappings(); if (operationEnrolmentMappings != null && operationEnrolmentMappings.size() > 0) { @@ -87,12 +90,19 @@ public class DeviceStatusMonitoringTask implements Task { enrolmentInfo.setId(mapping.getEnrolmentId()); enrolmentInfo.setStatus(newStatus); enrolmentInfoTobeUpdated.add(enrolmentInfo); + + deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(mapping.getDeviceId()); + deviceIdentifier.setId(mapping.getDeviceType()); + identifiers.add(deviceIdentifier); } } if (enrolmentInfoTobeUpdated.size() > 0) { try { this.updateDeviceStatus(enrolmentInfoTobeUpdated); + //Remove updated entries from cache + //DeviceCacheManagerImpl.getInstance().removeDevicesFromCache(identifiers); } catch (DeviceStatusTaskException e) { log.error("Error occurred while updating non-responsive device-status of devices of type '" + deviceType + "'",e); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java index 1c224ebd80..e9bb2645ef 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/util/DeviceManagerUtil.java @@ -31,6 +31,8 @@ import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.notification.mgt.NotificationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException; +import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; @@ -49,6 +51,10 @@ import org.wso2.carbon.utils.CarbonUtils; import org.wso2.carbon.utils.ConfigurationContextService; import org.wso2.carbon.utils.NetworkUtils; +import javax.cache.Cache; +import javax.cache.CacheConfiguration; +import javax.cache.CacheManager; +import javax.cache.Caching; import javax.sql.DataSource; import javax.xml.XMLConstants; import javax.xml.parsers.DocumentBuilder; @@ -60,12 +66,15 @@ import java.util.HashMap; import java.util.Hashtable; import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; public final class DeviceManagerUtil { private static final Log log = LogFactory.getLog(DeviceManagerUtil.class); + private static boolean isDeviceCacheInistialized = false; + public static Document convertToDocument(File file) throws DeviceManagementException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); @@ -441,4 +450,57 @@ public final class DeviceManagerUtil { } return true; } -} + + private static CacheManager getCacheManager() { + return Caching.getCacheManagerFactory().getCacheManager(DeviceManagementConstants.DM_CACHE_MANAGER); + } + + public static void initializeDeviceCache() { + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + int deviceCacheExpiry = config.getDeviceCacheConfiguration().getExpiryTime(); + CacheManager manager = getCacheManager(); + if (config.getDeviceCacheConfiguration().isEnabled()) { + if(!isDeviceCacheInistialized) { + isDeviceCacheInistialized = true; + if (manager != null) { + if (deviceCacheExpiry > 0) { + manager.createCacheBuilder(DeviceManagementConstants.DEVICE_CACHE). + setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS, + deviceCacheExpiry)).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration. + Duration(TimeUnit.SECONDS, deviceCacheExpiry)).setStoreByValue(true).build(); + } else { + manager.getCache(DeviceManagementConstants.DEVICE_CACHE); + } + } else { + if (deviceCacheExpiry > 0) { + Caching.getCacheManager(). + createCacheBuilder(DeviceManagementConstants.DEVICE_CACHE). + setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS, + deviceCacheExpiry)).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration. + Duration(TimeUnit.SECONDS, deviceCacheExpiry)).setStoreByValue(true).build(); + } else { + Caching.getCacheManager().getCache(DeviceManagementConstants.DEVICE_CACHE); + } + } + } + } + } + + public static Cache getDeviceCache() { + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + CacheManager manager = getCacheManager(); + Cache deviceCache = null; + if (config.getDeviceCacheConfiguration().isEnabled()) { + if(!isDeviceCacheInistialized) { + initializeDeviceCache(); + } + if (manager != null) { + deviceCache = manager.getCache(DeviceManagementConstants.DEVICE_CACHE); + } else { + deviceCache = Caching.getCacheManager(DeviceManagementConstants.DM_CACHE_MANAGER). + getCache(DeviceManagementConstants.DEVICE_CACHE); + } + } + return deviceCache; + } +} \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index da1146f780..6dd0ebbc5a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -72,7 +72,7 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); try { - device = deviceManagementService.getDevice(deviceIdentifier); + device = deviceManagementService.getDevice(deviceIdentifier, false); if (device != null) { pipDevice.setDevice(device); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml index 98be922a1c..0cd1564505 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml @@ -24,27 +24,35 @@ jdbc/DM_DS - - 8 - 100 - 20 - 1000 - - - https://localhost:9443 - admin - admin - - - org.wso2.carbon.policy.mgt - true - 60000 - 5 - 8 - 20 - - Simple - + + 8 + 100 + 20 + 1000 + + + https://localhost:9443 + admin + admin + + + org.wso2.carbon.policy.mgt + true + 60000 + 5 + 8 + 20 + + Simple + + + + true + + + true + 300 + diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml index f1178cab4f..d4f79ad671 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/conf/cdm-config.xml @@ -70,5 +70,9 @@ true + + true + 600 +