001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entityext.cache;
019:
020: import java.util.Map;
021:
022: import org.ofbiz.base.util.Debug;
023: import org.ofbiz.base.util.UtilMisc;
024: import org.ofbiz.entity.GenericDelegator;
025: import org.ofbiz.entity.GenericEntity;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericPK;
028: import org.ofbiz.entity.GenericValue;
029: import org.ofbiz.entity.condition.EntityCondition;
030: import org.ofbiz.entity.util.DistributedCacheClear;
031: import org.ofbiz.entityext.EntityServiceFactory;
032: import org.ofbiz.service.DispatchContext;
033: import org.ofbiz.service.GenericServiceException;
034: import org.ofbiz.service.LocalDispatcher;
035: import org.ofbiz.service.ServiceUtil;
036:
037: /**
038: * Entity Engine Cache Services
039: */
040: public class EntityCacheServices implements DistributedCacheClear {
041:
042: public static final String module = EntityCacheServices.class
043: .getName();
044:
045: protected GenericDelegator delegator = null;
046: protected LocalDispatcher dispatcher = null;
047: protected String userLoginId = null;
048:
049: public EntityCacheServices() {
050: }
051:
052: public void setDelegator(GenericDelegator delegator,
053: String userLoginId) {
054: this .delegator = delegator;
055: this .dispatcher = EntityServiceFactory
056: .getLocalDispatcher(delegator);
057: this .userLoginId = userLoginId;
058: }
059:
060: public GenericValue getAuthUserLogin() {
061: GenericValue userLogin = null;
062: try {
063: userLogin = delegator.findByPrimaryKeyCache("UserLogin",
064: UtilMisc.toMap("userLoginId", userLoginId));
065: } catch (GenericEntityException e) {
066: Debug
067: .logError(
068: e,
069: "Error finding the userLogin for distributed cache clear",
070: module);
071: }
072: return userLogin;
073: }
074:
075: public void distributedClearCacheLine(GenericValue value) {
076: // Debug.logInfo("running distributedClearCacheLine for value: " + value, module);
077: if (this .dispatcher == null) {
078: Debug
079: .logWarning(
080: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear",
081: module);
082: return;
083: }
084:
085: GenericValue userLogin = getAuthUserLogin();
086: if (userLogin == null) {
087: Debug.logWarning(
088: "The userLogin for distributed cache clear was not found with userLoginId ["
089: + userLoginId
090: + "], not clearing remote caches.", module);
091: return;
092: }
093:
094: try {
095: this .dispatcher.runAsync(
096: "distributedClearCacheLineByValue", UtilMisc.toMap(
097: "value", value, "userLogin", userLogin),
098: false);
099: } catch (GenericServiceException e) {
100: Debug
101: .logError(
102: e,
103: "Error running the distributedClearCacheLineByValue service",
104: module);
105: }
106: }
107:
108: public void distributedClearCacheLineFlexible(GenericEntity dummyPK) {
109: // Debug.logInfo("running distributedClearCacheLineFlexible for dummyPK: " + dummyPK, module);
110: if (this .dispatcher == null) {
111: Debug
112: .logWarning(
113: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear",
114: module);
115: return;
116: }
117:
118: GenericValue userLogin = getAuthUserLogin();
119: if (userLogin == null) {
120: Debug.logWarning(
121: "The userLogin for distributed cache clear was not found with userLoginId ["
122: + userLoginId
123: + "], not clearing remote caches.", module);
124: return;
125: }
126:
127: try {
128: this .dispatcher.runAsync(
129: "distributedClearCacheLineByDummyPK", UtilMisc
130: .toMap("dummyPK", dummyPK, "userLogin",
131: userLogin), false);
132: } catch (GenericServiceException e) {
133: Debug
134: .logError(
135: e,
136: "Error running the distributedClearCacheLineByDummyPK service",
137: module);
138: }
139: }
140:
141: public void distributedClearCacheLineByCondition(String entityName,
142: EntityCondition condition) {
143: // Debug.logInfo("running distributedClearCacheLineByCondition for (name, condition): " + entityName + ", " + condition + ")", module);
144: if (this .dispatcher == null) {
145: Debug
146: .logWarning(
147: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear",
148: module);
149: return;
150: }
151:
152: GenericValue userLogin = getAuthUserLogin();
153: if (userLogin == null) {
154: Debug.logWarning(
155: "The userLogin for distributed cache clear was not found with userLoginId ["
156: + userLoginId
157: + "], not clearing remote caches.", module);
158: return;
159: }
160:
161: try {
162: this .dispatcher.runAsync(
163: "distributedClearCacheLineByCondition", UtilMisc
164: .toMap("entityName", entityName,
165: "condition", condition,
166: "userLogin", userLogin), false);
167: } catch (GenericServiceException e) {
168: Debug
169: .logError(
170: e,
171: "Error running the distributedClearCacheLineByCondition service",
172: module);
173: }
174: }
175:
176: public void distributedClearCacheLine(GenericPK primaryKey) {
177: // Debug.logInfo("running distributedClearCacheLine for primaryKey: " + primaryKey, module);
178: if (this .dispatcher == null) {
179: Debug
180: .logWarning(
181: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear",
182: module);
183: return;
184: }
185:
186: GenericValue userLogin = getAuthUserLogin();
187: if (userLogin == null) {
188: Debug.logWarning(
189: "The userLogin for distributed cache clear was not found with userLoginId ["
190: + userLoginId
191: + "], not clearing remote caches.", module);
192: return;
193: }
194:
195: try {
196: this .dispatcher.runAsync(
197: "distributedClearCacheLineByPrimaryKey", UtilMisc
198: .toMap("primaryKey", primaryKey,
199: "userLogin", userLogin), false);
200: } catch (GenericServiceException e) {
201: Debug
202: .logError(
203: e,
204: "Error running the distributedClearCacheLineByPrimaryKey service",
205: module);
206: }
207: }
208:
209: public void clearAllCaches() {
210: if (this .dispatcher == null) {
211: Debug
212: .logWarning(
213: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed clear all caches",
214: module);
215: return;
216: }
217:
218: GenericValue userLogin = getAuthUserLogin();
219: if (userLogin == null) {
220: Debug.logWarning(
221: "The userLogin for distributed cache clear was not found with userLoginId ["
222: + userLoginId
223: + "], not clearing remote caches.", module);
224: return;
225: }
226:
227: try {
228: this .dispatcher.runAsync("distributedClearAllEntityCaches",
229: UtilMisc.toMap("userLogin", userLogin), false);
230: } catch (GenericServiceException e) {
231: Debug
232: .logError(
233: e,
234: "Error running the distributedClearAllCaches service",
235: module);
236: }
237: }
238:
239: /**
240: * Clear All Entity Caches Service
241: *@param ctx The DispatchContext that this service is operating in
242: *@param context Map containing the input parameters
243: *@return Map with the result of the service, the output parameters
244: */
245: public static Map clearAllEntityCaches(DispatchContext dctx,
246: Map context) {
247: GenericDelegator delegator = dctx.getDelegator();
248: Boolean distributeBool = (Boolean) context.get("distribute");
249: boolean distribute = false;
250: if (distributeBool != null)
251: distribute = distributeBool.booleanValue();
252:
253: delegator.clearAllCaches(distribute);
254:
255: return ServiceUtil.returnSuccess();
256: }
257:
258: /**
259: * Clear Cache Line Service: one of the following context parameters is required: value, dummyPK or primaryKey
260: *@param ctx The DispatchContext that this service is operating in
261: *@param context Map containing the input parameters
262: *@return Map with the result of the service, the output parameters
263: */
264: public static Map clearCacheLine(DispatchContext dctx, Map context) {
265: GenericDelegator delegator = dctx.getDelegator();
266: Boolean distributeBool = (Boolean) context.get("distribute");
267: boolean distribute = false;
268: if (distributeBool != null)
269: distribute = distributeBool.booleanValue();
270:
271: if (context.containsKey("value")) {
272: GenericValue value = (GenericValue) context.get("value");
273: if (Debug.infoOn())
274: Debug.logInfo(
275: "Got a clear cache line by value service call; entityName: "
276: + value.getEntityName(), module);
277: if (Debug.verboseOn())
278: Debug.logVerbose(
279: "Got a clear cache line by value service call; value: "
280: + value, module);
281: delegator.clearCacheLine(value, distribute);
282: } else if (context.containsKey("dummyPK")) {
283: GenericEntity dummyPK = (GenericEntity) context
284: .get("dummyPK");
285: if (Debug.infoOn())
286: Debug.logInfo(
287: "Got a clear cache line by dummyPK service call; entityName: "
288: + dummyPK.getEntityName(), module);
289: if (Debug.verboseOn())
290: Debug.logVerbose(
291: "Got a clear cache line by dummyPK service call; dummyPK: "
292: + dummyPK, module);
293: delegator.clearCacheLineFlexible(dummyPK, distribute);
294: } else if (context.containsKey("primaryKey")) {
295: GenericPK primaryKey = (GenericPK) context
296: .get("primaryKey");
297: if (Debug.infoOn())
298: Debug.logInfo(
299: "Got a clear cache line by primaryKey service call; entityName: "
300: + primaryKey.getEntityName(), module);
301: if (Debug.verboseOn())
302: Debug.logVerbose(
303: "Got a clear cache line by primaryKey service call; primaryKey: "
304: + primaryKey, module);
305: delegator.clearCacheLine(primaryKey, distribute);
306: } else if (context.containsKey("condition")) {
307: String entityName = (String) context.get("entityName");
308: EntityCondition condition = (EntityCondition) context
309: .get("condition");
310: if (Debug.infoOn())
311: Debug.logInfo(
312: "Got a clear cache line by condition service call; entityName: "
313: + entityName, module);
314: if (Debug.verboseOn())
315: Debug.logVerbose(
316: "Got a clear cache line by condition service call; condition: "
317: + condition, module);
318: delegator.clearCacheLineByCondition(entityName, condition,
319: distribute);
320: }
321: return ServiceUtil.returnSuccess();
322: }
323: }
|