From bc7e39c8833571f75ab037d3fac25987050a5013 Mon Sep 17 00:00:00 2001 From: Pahansith Date: Thu, 15 Oct 2020 10:05:37 +0530 Subject: [PATCH] Add geo fence search and geo cache --- .../service/api/GeoLocationBasedService.java | 13 +- .../impl/GeoLocationBasedServiceImpl.java | 39 ++++-- .../mgt/common/DeviceManagementConstants.java | 1 + .../service/GeoLocationProviderService.java | 23 ++- .../mgt/core/DeviceManagementConstants.java | 1 + .../device/mgt/core/cache/GeoCacheKey.java | 73 ++++++++++ .../mgt/core/cache/GeoCacheManager.java | 54 ++++++++ .../core/cache/impl/GeoCacheManagerImpl.java | 95 +++++++++++++ .../core/config/DeviceManagementConfig.java | 11 ++ .../cache/GeoFenceCacheConfiguration.java | 57 ++++++++ .../device/mgt/core/dao/GeofenceDAO.java | 23 ++- .../mgt/core/dao/impl/GeofenceDAOImpl.java | 73 ++++++++-- .../GeoLocationProviderServiceImpl.java | 131 ++++++++++++++++-- .../mgt/core/util/DeviceManagerUtil.java | 59 ++++++++ .../repository/conf/cdm-config.xml | 8 ++ .../mgt/core/util/PolicyManagerUtil.java | 3 +- .../src/main/resources/conf/cdm-config.xml | 8 ++ 17 files changed, 631 insertions(+), 41 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheKey.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheManager.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/GeoCacheManagerImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/GeoFenceCacheConfiguration.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java index d0e87c6c402..a8358ad8317 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/GeoLocationBasedService.java @@ -965,9 +965,18 @@ public interface GeoLocationBasedService { response = Response.class) }) Response getGeofence( + @ApiParam( + name = "offset", + value = "The starting pagination index for the complete list of qualified items.") @QueryParam("offset") int offset, - @DefaultValue("10") - @QueryParam("limit") int limit); + @ApiParam( + name = "limit", + value = "Provide how many device details you require from the starting pagination index/offset.") + @QueryParam("limit") int limit, + @ApiParam( + name = "name", + value = "Geo Fence name") + @QueryParam("name") String name); @DELETE diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java index 726afa5129a..aa41922d5a6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/GeoLocationBasedServiceImpl.java @@ -29,9 +29,9 @@ import org.wso2.carbon.analytics.dataservice.commons.SortType; import org.wso2.carbon.analytics.datasource.commons.Record; import org.wso2.carbon.analytics.datasource.commons.exception.AnalyticsException; import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.PaginationResult; @@ -45,7 +45,6 @@ import org.wso2.carbon.device.mgt.core.geo.geoHash.geoHashStrategy.GeoHashLength import org.wso2.carbon.device.mgt.core.geo.geoHash.geoHashStrategy.ZoomGeoHashLengthStrategy; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.device.mgt.jaxrs.beans.GeofenceList; import org.wso2.carbon.device.mgt.jaxrs.beans.GeofenceWrapper; import org.wso2.carbon.device.mgt.jaxrs.service.api.GeoLocationBasedService; import org.wso2.carbon.device.mgt.jaxrs.service.impl.util.RequestValidationUtil; @@ -621,7 +620,7 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { public Response getGeofence(@PathParam("fenceId") int fenceId) { try { GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); - GeofenceData geofenceData = geoService.getGeofence(fenceId); + GeofenceData geofenceData = geoService.getGeoFences(fenceId); if (geofenceData == null) { String msg = "No valid Geofence found for ID " + fenceId; return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); @@ -656,20 +655,21 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { @Consumes("application/json") @Produces("application/json") public Response getGeofence(@QueryParam("offset") int offset, - @DefaultValue("10") - @QueryParam("limit") int limit) { + @QueryParam("limit") int limit, + @QueryParam("name") String name) { try { - PaginationRequest request = new PaginationRequest(offset, limit); GeoLocationProviderService geoService = DeviceMgtAPIUtils.getGeoService(); - List geofence = geoService.getGeofence(request); - List geofenceList = new ArrayList<>(); - for (GeofenceData geofenceData : geofence) { - geofenceList.add(getMappedResponseBean(geofenceData)); + if (offset != 0 && limit != 0) { + PaginationRequest request = new PaginationRequest(offset, limit); + if (name != null && !name.isEmpty()) { + request.setProperty(DeviceManagementConstants.GeoServices.FENCE_NAME, name); + } + return getResponse(geoService.getGeoFences(request)); + } + if (name != null && !name.isEmpty()) { + return getResponse(geoService.getGeoFences(name)); } - PaginationResult paginationResult = new PaginationResult(); - paginationResult.setData(geofenceList); - paginationResult.setRecordsTotal(geofenceList.size()); - return Response.status(Response.Status.OK).entity(paginationResult).build(); + return getResponse(geoService.getGeoFences()); } catch (GeoLocationBasedServiceException e) { String msg = "Failed to retrieve geofence data"; log.error(msg, e); @@ -677,6 +677,17 @@ public class GeoLocationBasedServiceImpl implements GeoLocationBasedService { } } + private Response getResponse(List fencesList) { + List geofenceList = new ArrayList<>(); + for (GeofenceData geofenceData : fencesList) { + geofenceList.add(getMappedResponseBean(geofenceData)); + } + PaginationResult paginationResult = new PaginationResult(); + paginationResult.setData(geofenceList); + paginationResult.setRecordsTotal(geofenceList.size()); + return Response.status(Response.Status.OK).entity(paginationResult).build(); + } + @DELETE @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java index 492288febb7..95cc1a02674 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManagementConstants.java @@ -124,6 +124,7 @@ public final class DeviceManagementConstants { public static final String DAS_HOST_NAME = "${iot.analytics.host}"; public static final String DEFAULT_HTTP_PROTOCOL = "https"; public static final String DAS_URL = DEFAULT_HTTP_PROTOCOL + "://" + DAS_HOST_NAME + ":" + DAS_PORT; + public static final String FENCE_NAME = "name"; } public static final class OTPProperties { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java index 1449ca6b044..60bb198a245 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/geo/service/GeoLocationProviderService.java @@ -86,7 +86,7 @@ public interface GeoLocationProviderService { * @return Extracted geofence data * @throws GeoLocationBasedServiceException error occurs while retrieving a geofence */ - GeofenceData getGeofence(int fenceId) throws GeoLocationBasedServiceException; + GeofenceData getGeoFences(int fenceId) throws GeoLocationBasedServiceException; /** * Get paginated geofence list @@ -94,13 +94,28 @@ public interface GeoLocationProviderService { * @return List of Geofences retrieved * @throws GeoLocationBasedServiceException error occurs while retrieving geofences */ - List getGeofence(PaginationRequest request) throws GeoLocationBasedServiceException; + List getGeoFences(PaginationRequest request) throws GeoLocationBasedServiceException; + + /** + * Search geo fences using the fence name + * @param name searching name of the fence + * @return list of fences found for the specific name + * @throws GeoLocationBasedServiceException for errors occur while querying geo fences + */ + List getGeoFences(String name) throws GeoLocationBasedServiceException; + + /** + * Get all geo fences of the tenant + * @return list of the all geo fences of the tenant + * @throws GeoLocationBasedServiceException for errors occur while querying geo fences + */ + List getGeoFences() throws GeoLocationBasedServiceException; /** * Delete Geofence with ID * @param fenceId Id of the fence which should be deleted * @return true if deletion success. false if not record found for the used Id - * @throws GeoLocationBasedServiceException + * @throws GeoLocationBasedServiceException for errors occur while deleting geo fences */ boolean deleteGeofenceData(int fenceId) throws GeoLocationBasedServiceException; @@ -109,7 +124,7 @@ public interface GeoLocationProviderService { * @param geofenceData Bean with updated geofence data * @param fenceId Id of the fence which should be updated * @return true if update success. false if not a record found for the used Id - * @throws GeoLocationBasedServiceException + * @throws GeoLocationBasedServiceException for errors occur while updating geo fences */ boolean updateGeofence(GeofenceData geofenceData, int fenceId) throws GeoLocationBasedServiceException; } 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 d8b4ac5cb05..3f309a001be 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 @@ -42,6 +42,7 @@ 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 String GEOFENCE_CACHE = "GEOFENCE_CACHE"; public static final String ENROLLMENT_NOTIFICATION_API_ENDPOINT = "/api/device-mgt/enrollment-notification"; public static final String URL_SEPERATOR = "/"; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheKey.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheKey.java new file mode 100644 index 00000000000..dcac42c7b0e --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheKey.java @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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; + +public class GeoCacheKey { + private int fenceId; + private int tenantId; + private volatile int hashCode; + + public GeoCacheKey(int fenceId, int tenantId) { + this.fenceId = fenceId; + this.tenantId = tenantId; + } + + public GeoCacheKey() { + } + + public int getFenceId() { + return fenceId; + } + + public void setFenceId(int fenceId) { + this.fenceId = fenceId; + } + + 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 (!GeoCacheKey.class.isAssignableFrom(obj.getClass())) { + return false; + } + final GeoCacheKey other = (GeoCacheKey) obj; + String thisId = this.fenceId + "-" + "_" + this.tenantId; + String otherId = other.fenceId + "-" + "_" + other.tenantId; + return thisId.equals(otherId); + } + + @Override + public int hashCode() { + if (hashCode == 0) { + hashCode = Objects.hash(fenceId, 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/GeoCacheManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheManager.java new file mode 100644 index 00000000000..1b3bd9397d3 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/GeoCacheManager.java @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.geo.service.GeofenceData; + +public interface GeoCacheManager { + /** + * Add geo fences to the cache + * @param geofenceData adding fence object + * @param fenceId id of the fence + * @param tenantId id of the tenant + */ + void addFenceToCache(GeofenceData geofenceData, int fenceId, int tenantId); + + /** + * Update geo fences already in cache + * @param geofenceData updating geo fence object + * @param fenceId id of the fence + * @param tenantId id of the tenant + */ + void updateGeoFenceInCache(GeofenceData geofenceData, int fenceId, int tenantId); + + /** + * Remove geo fence from cache + * @param fenceId id of the fence + * @param tenantId id of the tenant + */ + void removeFenceFromCache(int fenceId, int tenantId); + + /** + * Get geo fence data from the cache + * @param fenceId id of the retrieving fence object + * @param tenantId tenant id of the fence created + * @return GeofenceData object if the cache have the specific object or null if there is no entry + */ + GeofenceData getGeoFenceFromCache(int fenceId, 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/GeoCacheManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/GeoCacheManagerImpl.java new file mode 100644 index 00000000000..770e31bdcc0 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/GeoCacheManagerImpl.java @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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.wso2.carbon.device.mgt.common.geo.service.GeofenceData; +import org.wso2.carbon.device.mgt.core.cache.GeoCacheKey; +import org.wso2.carbon.device.mgt.core.cache.GeoCacheManager; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; + +import javax.cache.Cache; + +public class GeoCacheManagerImpl implements GeoCacheManager { + + private static GeoCacheManager geoCacheManager; + private GeoCacheManagerImpl() {} + + public static GeoCacheManager getInstance() { + if (geoCacheManager == null) { + synchronized (GeoCacheManagerImpl.class) { + if (geoCacheManager == null) { + geoCacheManager = new GeoCacheManagerImpl(); + } + } + } + return geoCacheManager; + } + + @Override + public void addFenceToCache(GeofenceData geofenceData, int fenceId, int tenantId) { + Cache lCache = DeviceManagerUtil.getGeoCache(); + if (lCache != null) { + GeoCacheKey cacheKey = getCacheKey(fenceId, tenantId); + if (lCache.containsKey(cacheKey)) { + this.updateGeoFenceInCache(geofenceData, fenceId, tenantId); + } else { + lCache.put(cacheKey, geofenceData); + } + } + } + + @Override + public void removeFenceFromCache(int fenceId, int tenantId) { + Cache lCache = DeviceManagerUtil.getGeoCache(); + if (lCache != null) { + GeoCacheKey cacheKey = getCacheKey(fenceId, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.remove(cacheKey); + } + } + } + + @Override + public void updateGeoFenceInCache(GeofenceData geofenceData, int fenceId, int tenantId) { + Cache lCache = DeviceManagerUtil.getGeoCache(); + if (lCache != null) { + GeoCacheKey cacheKey = getCacheKey(fenceId, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.replace(cacheKey, geofenceData); + } + } + } + + @Override + public GeofenceData getGeoFenceFromCache(int fenceId, int tenantId) { + GeofenceData geofenceData = null; + Cache lCache = DeviceManagerUtil.getGeoCache(); + if (lCache != null) { + geofenceData = lCache.get(getCacheKey(fenceId, tenantId)); + } + return geofenceData; + } + + private GeoCacheKey getCacheKey(int fenceId, int tenantId) { + GeoCacheKey geoCacheKey = new GeoCacheKey(); + geoCacheKey.setFenceId(fenceId); + geoCacheKey.setTenantId(tenantId); + return geoCacheKey; + } +} 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 cf589679881..2a967238e10 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 @@ -23,6 +23,7 @@ import org.wso2.carbon.device.mgt.core.config.analytics.OperationAnalyticsConfig import org.wso2.carbon.device.mgt.core.config.archival.ArchivalConfiguration; import org.wso2.carbon.device.mgt.core.config.cache.CertificateCacheConfiguration; import org.wso2.carbon.device.mgt.core.config.cache.DeviceCacheConfiguration; +import org.wso2.carbon.device.mgt.core.config.cache.GeoFenceCacheConfiguration; import org.wso2.carbon.device.mgt.core.config.geo.location.GeoLocationConfiguration; import org.wso2.carbon.device.mgt.core.config.identity.IdentityConfigurations; import org.wso2.carbon.device.mgt.core.config.keymanager.KeyManagerConfigurations; @@ -54,6 +55,7 @@ public final class DeviceManagementConfig { private PullNotificationConfiguration pullNotificationConfiguration; private DeviceStatusTaskConfig deviceStatusTaskConfig; private DeviceCacheConfiguration deviceCacheConfiguration; + private GeoFenceCacheConfiguration geoFenceCacheConfiguration; private CertificateCacheConfiguration certificateCacheConfiguration; private OperationAnalyticsConfiguration operationAnalyticsConfiguration; private GeoLocationConfiguration geoLocationConfiguration; @@ -154,6 +156,15 @@ public final class DeviceManagementConfig { this.deviceCacheConfiguration = deviceCacheConfiguration; } + @XmlElement(name = "GeoFenceCacheConfiguration", required = true) + public GeoFenceCacheConfiguration getGeoFenceCacheConfiguration() { + return geoFenceCacheConfiguration; + } + + public void setGeoFenceCacheConfiguration(GeoFenceCacheConfiguration geoFenceCacheConfiguration) { + this.geoFenceCacheConfiguration = geoFenceCacheConfiguration; + } + @XmlElement(name = "CertificateCacheConfiguration", required = true) public CertificateCacheConfiguration getCertificateCacheConfiguration() { return certificateCacheConfiguration; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/GeoFenceCacheConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/GeoFenceCacheConfiguration.java new file mode 100644 index 00000000000..5a27eb74308 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/config/cache/GeoFenceCacheConfiguration.java @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2020, Entgra (Pvt) Ltd. (http://www.entgra.io) All Rights Reserved. + * + * Entgra (Pvt) Ltd. 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 = "GeoFenceCacheConfiguration") +public class GeoFenceCacheConfiguration { + + private boolean isEnabled; + private int expiryTime; + private long capacity; + + @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; + } + + @XmlElement(name = "Capacity", required = true) + public long getCapacity() { + return capacity; + } + + public void setCapacity(long capacity) { + this.capacity = capacity; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java index 27e8d72f1be..0801edd6d95 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/GeofenceDAO.java @@ -34,7 +34,7 @@ public interface GeofenceDAO { * @return created row count * @throws DeviceManagementDAOException error occurs while saving the data */ - int saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException; + GeofenceData saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException; /** * Retrieve a geofence record for specified Id @@ -51,7 +51,26 @@ public interface GeofenceDAO { * @return List of geofences retrieved * @throws DeviceManagementDAOException error occurs while reading the data */ - List getGeofencesOfTenant(PaginationRequest request, int tenantId) + List getGeoFencesOfTenant(PaginationRequest request, int tenantId) + throws DeviceManagementDAOException; + + /** + * Search geofence by fence name of a specific tenant + * @param fenceName searching name + * @param tenantId searching tenant + * @return list of found fences + * @throws DeviceManagementDAOException + */ + List getGeoFencesOfTenant(String fenceName, int tenantId) + throws DeviceManagementDAOException; + + /** + * Get all fences of the specific tenant + * @param tenantId tenant id of the fences + * @return list of the fences owned by the tenant + * @throws DeviceManagementDAOException + */ + List getGeoFencesOfTenant(int tenantId) throws DeviceManagementDAOException; /** diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java index 68223d9806c..452c2ecc3a8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/GeofenceDAOImpl.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.PaginationRequest; import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException; import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; @@ -31,6 +32,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.sql.Timestamp; import java.util.ArrayList; import java.util.Date; @@ -39,7 +41,7 @@ import java.util.List; public class GeofenceDAOImpl implements GeofenceDAO { private static final Log log = LogFactory.getLog(GeofenceDAOImpl.class); @Override - public int saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException { + public GeofenceData saveGeofence(GeofenceData geofenceData) throws DeviceManagementDAOException { try { Connection conn = this.getConnection(); String sql = "INSERT INTO DM_GEOFENCE(" + @@ -54,7 +56,7 @@ public class GeofenceDAOImpl implements GeofenceDAO { "OWNER, " + "TENANT_ID) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - try (PreparedStatement stmt = conn.prepareStatement(sql)) { + try (PreparedStatement stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) { stmt.setString(1, geofenceData.getFenceName()); stmt.setString(2, geofenceData.getDescription()); stmt.setDouble(3, geofenceData.getLatitude()); @@ -65,7 +67,13 @@ public class GeofenceDAOImpl implements GeofenceDAO { stmt.setTimestamp(8, new Timestamp(new Date().getTime())); stmt.setString(9, geofenceData.getOwner()); stmt.setInt(10, geofenceData.getTenantId()); - return stmt.executeUpdate(); + if (stmt.executeUpdate() > 0) { + ResultSet generatedKeys = stmt.getGeneratedKeys(); + if (generatedKeys.next()) { + geofenceData.setId(generatedKeys.getInt(1)); + } + } + return geofenceData; } } catch (SQLException e) { String msg = "Error occurred while creating Geofence for the tenant id "+geofenceData.getTenantId(); @@ -110,7 +118,59 @@ public class GeofenceDAOImpl implements GeofenceDAO { } @Override - public List getGeofencesOfTenant(PaginationRequest request, int tenantId) + public List getGeoFencesOfTenant(PaginationRequest request, int tenantId) + throws DeviceManagementDAOException { + try { + boolean isNameProvided = false; + List geofenceData; + Connection conn = this.getConnection(); + String sql = "SELECT " + + "ID, " + + "FENCE_NAME, " + + "DESCRIPTION, " + + "LATITUDE, " + + "LONGITUDE, " + + "RADIUS, " + + "GEO_JSON, " + + "FENCE_SHAPE, " + + "OWNER, " + + "TENANT_ID " + + "FROM DM_GEOFENCE " + + "WHERE TENANT_ID = ? "; + + if (request.getProperty(DeviceManagementConstants.GeoServices.FENCE_NAME) != null) { + sql += "AND FENCE_NAME LIKE ?"; + isNameProvided = true; + } + sql += "LIMIT ? OFFSET ?"; + int index = 1; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(index++, tenantId); + if (isNameProvided) { + stmt.setString(index++, request.getProperty(DeviceManagementConstants.GeoServices.FENCE_NAME).toString() + "%"); + } + stmt.setInt(index++, request.getRowCount()); + stmt.setInt(index, request.getStartIndex()); + try (ResultSet rst = stmt.executeQuery()) { + geofenceData = extractGeofenceData(rst); + } + } + return geofenceData; + } catch (SQLException e) { + String msg = "Error occurred while retrieving Geofence of the tenant " + tenantId; + log.error(msg, e); + throw new DeviceManagementDAOException(msg, e); + } + } + + @Override + public List getGeoFencesOfTenant(String fenceName, int tenantId) + throws DeviceManagementDAOException { + return null; + } + + @Override + public List getGeoFencesOfTenant(int tenantId) throws DeviceManagementDAOException { try { List geofenceData; @@ -127,12 +187,9 @@ public class GeofenceDAOImpl implements GeofenceDAO { "OWNER, " + "TENANT_ID " + "FROM DM_GEOFENCE " + - "WHERE TENANT_ID = ? " + - "LIMIT ? OFFSET ?"; + "WHERE TENANT_ID = ? "; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, tenantId); - stmt.setInt(2, request.getRowCount()); - stmt.setInt(3, request.getStartIndex()); try (ResultSet rst = stmt.executeQuery()) { geofenceData = extractGeofenceData(rst); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java index d54818ca9c8..d8409ef112e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/geo/service/GeoLocationProviderServiceImpl.java @@ -38,7 +38,6 @@ import org.wso2.carbon.core.util.Utils; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants.GeoServices; import org.wso2.carbon.device.mgt.common.PaginationRequest; -import org.wso2.carbon.device.mgt.common.PaginationResult; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; import org.wso2.carbon.device.mgt.common.geo.service.Alert; import org.wso2.carbon.device.mgt.common.geo.service.GeoFence; @@ -46,6 +45,7 @@ import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationProviderService; import org.wso2.carbon.device.mgt.common.geo.service.GeoLocationBasedServiceException; import org.wso2.carbon.device.mgt.common.geo.service.AlertAlreadyExistException; import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; +import org.wso2.carbon.device.mgt.core.cache.impl.GeoCacheManagerImpl; 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.dao.GeofenceDAO; @@ -1249,8 +1249,10 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean createGeofence(GeofenceData geofenceData) throws GeoLocationBasedServiceException { + int tenantId; try { - geofenceData.setTenantId(DeviceManagementDAOUtil.getTenantId()); + tenantId = DeviceManagementDAOUtil.getTenantId(); + geofenceData.setTenantId(tenantId); geofenceData.setOwner(PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername()); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving tenant Id"; @@ -1260,7 +1262,10 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic try { DeviceManagementDAOFactory.beginTransaction(); - return geofenceDAO.saveGeofence(geofenceData) > 0; + geofenceData = geofenceDAO.saveGeofence(geofenceData); + GeoCacheManagerImpl.getInstance() + .addFenceToCache(geofenceData, geofenceData.getId(), tenantId); + return true; } catch (TransactionManagementException e) { String msg = "Failed to begin transaction for saving geofence"; log.error(msg, e); @@ -1276,10 +1281,28 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } @Override - public GeofenceData getGeofence(int fenceId) throws GeoLocationBasedServiceException { + public GeofenceData getGeoFences(int fenceId) throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + GeofenceData geofenceData = GeoCacheManagerImpl.getInstance() + .getGeoFenceFromCache(fenceId, tenantId); + if (geofenceData != null) { + return geofenceData; + } + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant Id"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + try { DeviceManagementDAOFactory.openConnection(); - return geofenceDAO.getGeofence(fenceId); + GeofenceData geofence = geofenceDAO.getGeofence(fenceId); + if (geofence != null) { + GeoCacheManagerImpl.getInstance().addFenceToCache(geofence, fenceId, tenantId); + } + return geofence; } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving Geofence data with ID "+fenceId; log.error(msg, e); @@ -1294,7 +1317,37 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } @Override - public List getGeofence(PaginationRequest request) throws GeoLocationBasedServiceException { + public List getGeoFences(PaginationRequest request) throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + + try { + if (log.isDebugEnabled()) { + log.debug("Retrieving geofence data for the tenant " + tenantId); + } + DeviceManagementDAOFactory.openConnection(); + return geofenceDAO.getGeoFencesOfTenant(request, tenantId); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving geofence data for the tenant " + tenantId; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } catch (SQLException e) { + String msg = "Failed to open the DB connection to retrieve Geofence"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + } + + @Override + public List getGeoFences(String fenceName) throws GeoLocationBasedServiceException { int tenantId; try { tenantId = DeviceManagementDAOUtil.getTenantId(); @@ -1309,7 +1362,7 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic log.debug("Retrieving geofence data for the tenant " + tenantId); } DeviceManagementDAOFactory.openConnection(); - return geofenceDAO.getGeofencesOfTenant(request, tenantId); + return geofenceDAO.getGeoFencesOfTenant(fenceName, tenantId); } catch (DeviceManagementDAOException e) { String msg = "Error occurred while retrieving geofence data for the tenant " + tenantId; log.error(msg, e); @@ -1323,17 +1376,64 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic } } + @Override + public List getGeoFences() throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + + try { + if (log.isDebugEnabled()) { + log.debug("Retrieving all fence data for the tenant " + tenantId); + } + DeviceManagementDAOFactory.openConnection(); + List geoFencesOfTenant = geofenceDAO.getGeoFencesOfTenant(tenantId); + for (GeofenceData geofenceData : geoFencesOfTenant) { + GeoCacheManagerImpl.getInstance() + .addFenceToCache(geofenceData, geofenceData.getId(), tenantId); + } + return geoFencesOfTenant; + + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving geofence data for the tenant " + tenantId; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } catch (SQLException e) { + String msg = "Failed to open the DB connection to retrieve Geofence"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } finally { + DeviceManagementDAOFactory.closeConnection(); + } + + } + @Override public boolean deleteGeofenceData(int fenceId) throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + try { DeviceManagementDAOFactory.beginTransaction(); if (geofenceDAO.deleteGeofenceById(fenceId) > 0) { DeviceManagementDAOFactory.commitTransaction(); + GeoCacheManagerImpl.getInstance().removeFenceFromCache(fenceId, tenantId); return true; } - DeviceManagementDAOFactory.rollbackTransaction(); return false; } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while deleting geofence"; log.error(msg, e); throw new GeoLocationBasedServiceException(msg, e); @@ -1349,9 +1449,22 @@ public class GeoLocationProviderServiceImpl implements GeoLocationProviderServic @Override public boolean updateGeofence(GeofenceData geofenceData, int fenceId) throws GeoLocationBasedServiceException { + int tenantId; + try { + tenantId = DeviceManagementDAOUtil.getTenantId(); + } catch (DeviceManagementDAOException e) { + String msg = "Error occurred while retrieving tenant id while get geofence data"; + log.error(msg, e); + throw new GeoLocationBasedServiceException(msg, e); + } + try { DeviceManagementDAOFactory.beginTransaction(); - return geofenceDAO.updateGeofence(geofenceData, fenceId) > 0; + if (geofenceDAO.updateGeofence(geofenceData, fenceId) > 0) { + GeoCacheManagerImpl.getInstance().updateGeoFenceInCache(geofenceData, fenceId, tenantId); + return true; + } + return false; } catch (TransactionManagementException e) { String msg = "Failed to begin transaction for saving geofence"; log.error(msg, 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 9404e5de86c..6a64c5b3d99 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 @@ -67,6 +67,7 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; +import org.wso2.carbon.device.mgt.common.geo.service.GeofenceData; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException; import org.wso2.carbon.device.mgt.common.exceptions.MetadataManagementException; @@ -75,6 +76,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementExcept import org.wso2.carbon.device.mgt.common.type.mgt.DeviceTypeMetaDefinition; import org.wso2.carbon.device.mgt.core.DeviceManagementConstants; import org.wso2.carbon.device.mgt.core.cache.DeviceCacheKey; +import org.wso2.carbon.device.mgt.core.cache.GeoCacheKey; 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; @@ -132,6 +134,7 @@ public final class DeviceManagerUtil { public static final String GENERAL_CONFIG_RESOURCE_PATH = "general"; private static boolean isDeviceCacheInitialized = false; + private static boolean isGeoFenceCacheInitialized = false; public static Document convertToDocument(File file) throws DeviceManagementException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); @@ -641,6 +644,44 @@ public final class DeviceManagerUtil { } } + public static void initializeGeofenceCache() { + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + int geoCacheExpiry = config.getGeoFenceCacheConfiguration().getExpiryTime(); + long geoCacheCapacity = config.getGeoFenceCacheConfiguration().getCapacity(); + CacheManager manager = getCacheManager(); + if (config.getGeoFenceCacheConfiguration().isEnabled()) { + if(!isGeoFenceCacheInitialized) { + isGeoFenceCacheInitialized = true; + if (manager != null) { + if (geoCacheExpiry > 0) { + manager.createCacheBuilder(DeviceManagementConstants.GEOFENCE_CACHE). + setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS, + geoCacheExpiry)).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration. + Duration(TimeUnit.SECONDS, geoCacheExpiry)).setStoreByValue(true).build(); + if(geoCacheCapacity > 0 ) { + ((CacheImpl) manager.getCache(DeviceManagementConstants.GEOFENCE_CACHE)). + setCapacity(geoCacheCapacity); + } + } else { + manager.getCache(DeviceManagementConstants.GEOFENCE_CACHE); + } + } else { + if (geoCacheExpiry > 0) { + Caching.getCacheManager(). + createCacheBuilder(DeviceManagementConstants.GEOFENCE_CACHE). + setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration(TimeUnit.SECONDS, + geoCacheExpiry)).setExpiry(CacheConfiguration.ExpiryType.ACCESSED, new CacheConfiguration. + Duration(TimeUnit.SECONDS, geoCacheExpiry)).setStoreByValue(true).build(); + ((CacheImpl)(manager.getCache(DeviceManagementConstants.GEOFENCE_CACHE))). + setCapacity(geoCacheCapacity); + } else { + Caching.getCacheManager().getCache(DeviceManagementConstants.GEOFENCE_CACHE); + } + } + } + } + } + public static Cache getDeviceCache() { DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); CacheManager manager = getCacheManager(); @@ -659,6 +700,24 @@ public final class DeviceManagerUtil { return deviceCache; } + public static Cache getGeoCache() { + DeviceManagementConfig config = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(); + CacheManager manager = getCacheManager(); + Cache geoCache = null; + if (config.getGeoFenceCacheConfiguration().isEnabled()) { + if(!isGeoFenceCacheInitialized) { + initializeGeofenceCache(); + } + if (manager != null) { + geoCache = manager.getCache(DeviceManagementConstants.GEOFENCE_CACHE); + } else { + geoCache = Caching.getCacheManager(DeviceManagementConstants.GEOFENCE_CACHE) + .getCache(DeviceManagementConstants.GEOFENCE_CACHE); + } + } + return geoCache; + } + /** * Create an app and get app registration token from the application registration endpoint * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml index 7b57ee7414f..723662a8cda 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/carbon-home/repository/conf/cdm-config.xml @@ -89,6 +89,14 @@ server environment--> 10000 + + false + 600 + + 10000 + false 86400 diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 4ca080d7f26..51ab59b4ebd 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -40,7 +40,6 @@ import org.apache.commons.lang.StringUtils; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; -import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; @@ -205,7 +204,7 @@ public class PolicyManagerUtil { if (log.isDebugEnabled()) { log.debug("Retrieving geofence with ID " + fenceId); } - GeofenceData geofence = geoLocationProviderService.getGeofence(fenceId); + GeofenceData geofence = geoLocationProviderService.getGeoFences(fenceId); if (geofence != null) { JsonObject operationPayload = new JsonObject(); operationPayload.addProperty("fenceId", geofence.getId()); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml index bef658a6157..cbb8cc6905d 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/cdm-config.xml @@ -89,6 +89,14 @@ server environment--> 10000 + + true + 600 + + 10000 + true 86400