forked from community/device-mgt-core
parent
09b1711b9c
commit
bc7e39c883
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
@ -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<GeoCacheKey, GeofenceData> 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<GeoCacheKey, GeofenceData> 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<GeoCacheKey, GeofenceData> 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<GeoCacheKey, GeofenceData> 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;
|
||||||
|
}
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue