1 25 package org.ofbiz.entityext.cache; 26 27 import java.util.Map ; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.base.util.UtilMisc; 31 import org.ofbiz.entity.GenericDelegator; 32 import org.ofbiz.entity.GenericEntity; 33 import org.ofbiz.entity.GenericEntityException; 34 import org.ofbiz.entity.GenericPK; 35 import org.ofbiz.entity.GenericValue; 36 import org.ofbiz.entity.condition.EntityCondition; 37 import org.ofbiz.entity.util.DistributedCacheClear; 38 import org.ofbiz.entityext.EntityServiceFactory; 39 import org.ofbiz.service.DispatchContext; 40 import org.ofbiz.service.GenericServiceException; 41 import org.ofbiz.service.LocalDispatcher; 42 import org.ofbiz.service.ServiceUtil; 43 44 51 public class EntityCacheServices implements DistributedCacheClear { 52 53 public static final String module = EntityCacheServices.class.getName(); 54 55 protected GenericDelegator delegator = null; 56 protected LocalDispatcher dispatcher = null; 57 protected String userLoginId = null; 58 59 public EntityCacheServices() {} 60 61 public void setDelegator(GenericDelegator delegator, String userLoginId) { 62 this.delegator = delegator; 63 this.dispatcher = EntityServiceFactory.getLocalDispatcher(delegator); 64 this.userLoginId = userLoginId; 65 } 66 67 public GenericValue getAuthUserLogin() { 68 GenericValue userLogin = null; 69 try { 70 userLogin = delegator.findByPrimaryKeyCache("UserLogin", UtilMisc.toMap("userLoginId", userLoginId)); 71 } catch (GenericEntityException e) { 72 Debug.logError(e, "Error finding the userLogin for distributed cache clear", module); 73 } 74 return userLogin; 75 } 76 77 public void distributedClearCacheLine(GenericValue value) { 78 if (this.dispatcher == null) { 80 Debug.logWarning("No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear", module); 81 return; 82 } 83 84 GenericValue userLogin = getAuthUserLogin(); 85 if (userLogin == null) { 86 Debug.logWarning("The userLogin for distributed cache clear was not found with userLoginId [" + userLoginId + "], not clearing remote caches.", module); 87 return; 88 } 89 90 try { 91 this.dispatcher.runAsync("distributedClearCacheLineByValue", UtilMisc.toMap("value", value, "userLogin", userLogin), false); 92 } catch (GenericServiceException e) { 93 Debug.logError(e, "Error running the distributedClearCacheLineByValue service", module); 94 } 95 } 96 97 public void distributedClearCacheLineFlexible(GenericEntity dummyPK) { 98 if (this.dispatcher == null) { 100 Debug.logWarning("No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear", module); 101 return; 102 } 103 104 GenericValue userLogin = getAuthUserLogin(); 105 if (userLogin == null) { 106 Debug.logWarning("The userLogin for distributed cache clear was not found with userLoginId [" + userLoginId + "], not clearing remote caches.", module); 107 return; 108 } 109 110 try { 111 this.dispatcher.runAsync("distributedClearCacheLineByDummyPK", UtilMisc.toMap("dummyPK", dummyPK, "userLogin", userLogin), false); 112 } catch (GenericServiceException e) { 113 Debug.logError(e, "Error running the distributedClearCacheLineByDummyPK service", module); 114 } 115 } 116 117 public void distributedClearCacheLineByCondition(String entityName, EntityCondition condition) { 118 if (this.dispatcher == null) { 120 Debug.logWarning("No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear", module); 121 return; 122 } 123 124 GenericValue userLogin = getAuthUserLogin(); 125 if (userLogin == null) { 126 Debug.logWarning("The userLogin for distributed cache clear was not found with userLoginId [" + userLoginId + "], not clearing remote caches.", module); 127 return; 128 } 129 130 try { 131 this.dispatcher.runAsync("distributedClearCacheLineByCondition", UtilMisc.toMap("entityName", entityName, "condition", condition), false); 132 } catch (GenericServiceException e) { 133 Debug.logError(e, "Error running the distributedClearCacheLineByCondition service", module); 134 } 135 } 136 137 public void distributedClearCacheLine(GenericPK primaryKey) { 138 if (this.dispatcher == null) { 140 Debug.logWarning("No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear", module); 141 return; 142 } 143 144 GenericValue userLogin = getAuthUserLogin(); 145 if (userLogin == null) { 146 Debug.logWarning("The userLogin for distributed cache clear was not found with userLoginId [" + userLoginId + "], not clearing remote caches.", module); 147 return; 148 } 149 150 try { 151 this.dispatcher.runAsync("distributedClearCacheLineByPrimaryKey", UtilMisc.toMap("primaryKey", primaryKey, "userLogin", userLogin), false); 152 } catch (GenericServiceException e) { 153 Debug.logError(e, "Error running the distributedClearCacheLineByPrimaryKey service", module); 154 } 155 } 156 157 public void clearAllCaches() { 158 if (this.dispatcher == null) { 159 Debug.logWarning("No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed clear all caches", module); 160 return; 161 } 162 163 GenericValue userLogin = getAuthUserLogin(); 164 if (userLogin == null) { 165 Debug.logWarning("The userLogin for distributed cache clear was not found with userLoginId [" + userLoginId + "], not clearing remote caches.", module); 166 return; 167 } 168 169 try { 170 this.dispatcher.runAsync("distributedClearAllEntityCaches", UtilMisc.toMap("userLogin", userLogin), false); 171 } catch (GenericServiceException e) { 172 Debug.logError(e, "Error running the distributedClearAllCaches service", module); 173 } 174 } 175 176 182 public static Map clearAllEntityCaches(DispatchContext dctx, Map context) { 183 GenericDelegator delegator = dctx.getDelegator(); 184 Boolean distributeBool = (Boolean ) context.get("distribute"); 185 boolean distribute = false; 186 if (distributeBool != null) distribute = distributeBool.booleanValue(); 187 188 delegator.clearAllCaches(distribute); 189 190 return ServiceUtil.returnSuccess(); 191 } 192 193 199 public static Map clearCacheLine(DispatchContext dctx, Map context) { 200 GenericDelegator delegator = dctx.getDelegator(); 201 Boolean distributeBool = (Boolean ) context.get("distribute"); 202 boolean distribute = false; 203 if (distributeBool != null) distribute = distributeBool.booleanValue(); 204 205 if (context.containsKey("value")) { 206 GenericValue value = (GenericValue) context.get("value"); 207 if (Debug.infoOn()) Debug.logInfo("Got a clear cache line by value service call; entityName: " + value.getEntityName(), module); 208 if (Debug.verboseOn()) Debug.logVerbose("Got a clear cache line by value service call; value: " + value, module); 209 delegator.clearCacheLine(value, distribute); 210 } else if (context.containsKey("dummyPK")) { 211 GenericEntity dummyPK = (GenericEntity) context.get("dummyPK"); 212 if (Debug.infoOn()) Debug.logInfo("Got a clear cache line by dummyPK service call; entityName: " + dummyPK.getEntityName(), module); 213 if (Debug.verboseOn()) Debug.logVerbose("Got a clear cache line by dummyPK service call; dummyPK: " + dummyPK, module); 214 delegator.clearCacheLineFlexible(dummyPK, distribute); 215 } else if (context.containsKey("primaryKey")) { 216 GenericPK primaryKey = (GenericPK) context.get("primaryKey"); 217 if (Debug.infoOn()) Debug.logInfo("Got a clear cache line by primaryKey service call; entityName: " + primaryKey.getEntityName(), module); 218 if (Debug.verboseOn()) Debug.logVerbose("Got a clear cache line by primaryKey service call; primaryKey: " + primaryKey, module); 219 delegator.clearCacheLine(primaryKey, distribute); 220 } else if (context.containsKey("condition")) { 221 String entityName = (String ) context.get("entityName"); 222 EntityCondition condition = (EntityCondition) context.get("condition"); 223 if (Debug.infoOn()) Debug.logInfo("Got a clear cache line by condition service call; entityName: " + entityName, module); 224 if (Debug.verboseOn()) Debug.logVerbose("Got a clear cache line by condition service call; condition: " + condition, module); 225 delegator.clearCacheLineByCondition(entityName, condition, distribute); 226 } 227 return ServiceUtil.returnSuccess(); 228 } 229 } 230 | Popular Tags |