001: /*
002: * $Id: EntityCacheServices.java,v 1.1 2003/08/17 06:44:25 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.entityext.cache;
026:
027: import java.util.Map;
028:
029: import org.ofbiz.base.util.Debug;
030: import org.ofbiz.base.util.UtilMisc;
031: import org.ofbiz.entity.GenericDelegator;
032: import org.ofbiz.entity.GenericEntity;
033: import org.ofbiz.entity.GenericEntityException;
034: import org.ofbiz.entity.GenericPK;
035: import org.ofbiz.entity.GenericValue;
036: import org.ofbiz.entity.util.DistributedCacheClear;
037: import org.ofbiz.entityext.EntityServiceFactory;
038: import org.ofbiz.service.DispatchContext;
039: import org.ofbiz.service.GenericServiceException;
040: import org.ofbiz.service.LocalDispatcher;
041: import org.ofbiz.service.ServiceUtil;
042:
043: /**
044: * Entity Engine Cache Services
045: *
046: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
047: * @version $Revision: 1.1 $
048: * @since 2.0
049: */
050: public class EntityCacheServices implements DistributedCacheClear {
051:
052: public static final String module = EntityCacheServices.class
053: .getName();
054:
055: protected GenericDelegator delegator = null;
056: protected LocalDispatcher dispatcher = null;
057: protected String userLoginId = null;
058:
059: public EntityCacheServices() {
060: }
061:
062: public void setDelegator(GenericDelegator delegator,
063: String userLoginId) {
064: this .delegator = delegator;
065: this .dispatcher = EntityServiceFactory
066: .getLocalDispatcher(delegator);
067: this .userLoginId = userLoginId;
068: }
069:
070: public GenericValue getAuthUserLogin() {
071: GenericValue userLogin = null;
072: try {
073: userLogin = delegator.findByPrimaryKeyCache("UserLogin",
074: UtilMisc.toMap("userLoginId", userLoginId));
075: } catch (GenericEntityException e) {
076: Debug
077: .logError(
078: e,
079: "Error finding the userLogin for distributed cache clear",
080: module);
081: }
082: return userLogin;
083: }
084:
085: public void distributedClearCacheLine(GenericValue value) {
086: // Debug.logInfo("running distributedClearCacheLine for value: " + value, module);
087: if (this .dispatcher == null) {
088: Debug
089: .logWarning(
090: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear",
091: module);
092: return;
093: }
094:
095: GenericValue userLogin = getAuthUserLogin();
096: if (userLogin == null) {
097: Debug.logWarning(
098: "The userLogin for distributed cache clear was not found with userLoginId ["
099: + userLoginId
100: + "], not clearing remote caches.", module);
101: return;
102: }
103:
104: try {
105: this .dispatcher.runAsync(
106: "distributedClearCacheLineByValue", UtilMisc.toMap(
107: "value", value, "userLogin", userLogin),
108: false);
109: } catch (GenericServiceException e) {
110: Debug
111: .logError(
112: e,
113: "Error running the distributedClearCacheLineByValue service",
114: module);
115: }
116: }
117:
118: public void distributedClearCacheLineFlexible(GenericEntity dummyPK) {
119: // Debug.logInfo("running distributedClearCacheLineFlexible for dummyPK: " + dummyPK, module);
120: if (this .dispatcher == null) {
121: Debug
122: .logWarning(
123: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear",
124: module);
125: return;
126: }
127:
128: GenericValue userLogin = getAuthUserLogin();
129: if (userLogin == null) {
130: Debug.logWarning(
131: "The userLogin for distributed cache clear was not found with userLoginId ["
132: + userLoginId
133: + "], not clearing remote caches.", module);
134: return;
135: }
136:
137: try {
138: this .dispatcher.runAsync(
139: "distributedClearCacheLineByDummyPK", UtilMisc
140: .toMap("dummyPK", dummyPK, "userLogin",
141: userLogin), false);
142: } catch (GenericServiceException e) {
143: Debug
144: .logError(
145: e,
146: "Error running the distributedClearCacheLineByDummyPK service",
147: module);
148: }
149: }
150:
151: public void distributedClearCacheLine(GenericPK primaryKey) {
152: // Debug.logInfo("running distributedClearCacheLine for primaryKey: " + primaryKey, module);
153: if (this .dispatcher == null) {
154: Debug
155: .logWarning(
156: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed cache clear",
157: module);
158: return;
159: }
160:
161: GenericValue userLogin = getAuthUserLogin();
162: if (userLogin == null) {
163: Debug.logWarning(
164: "The userLogin for distributed cache clear was not found with userLoginId ["
165: + userLoginId
166: + "], not clearing remote caches.", module);
167: return;
168: }
169:
170: try {
171: this .dispatcher.runAsync(
172: "distributedClearCacheLineByPrimaryKey", UtilMisc
173: .toMap("primaryKey", primaryKey,
174: "userLogin", userLogin), false);
175: } catch (GenericServiceException e) {
176: Debug
177: .logError(
178: e,
179: "Error running the distributedClearCacheLineByPrimaryKey service",
180: module);
181: }
182: }
183:
184: public void clearAllCaches() {
185: if (this .dispatcher == null) {
186: Debug
187: .logWarning(
188: "No dispatcher is available, somehow the setDelegator (which also creates a dispatcher) was not called, not running distributed clear all caches",
189: module);
190: return;
191: }
192:
193: GenericValue userLogin = getAuthUserLogin();
194: if (userLogin == null) {
195: Debug.logWarning(
196: "The userLogin for distributed cache clear was not found with userLoginId ["
197: + userLoginId
198: + "], not clearing remote caches.", module);
199: return;
200: }
201:
202: try {
203: this .dispatcher.runAsync("distributedClearAllEntityCaches",
204: UtilMisc.toMap("userLogin", userLogin), false);
205: } catch (GenericServiceException e) {
206: Debug
207: .logError(
208: e,
209: "Error running the distributedClearAllCaches service",
210: module);
211: }
212: }
213:
214: /**
215: * Clear All Entity Caches Service
216: *@param ctx The DispatchContext that this service is operating in
217: *@param context Map containing the input parameters
218: *@return Map with the result of the service, the output parameters
219: */
220: public static Map clearAllEntityCaches(DispatchContext dctx,
221: Map context) {
222: GenericDelegator delegator = dctx.getDelegator();
223: Boolean distributeBool = (Boolean) context.get("distribute");
224: boolean distribute = false;
225: if (distributeBool != null)
226: distribute = distributeBool.booleanValue();
227:
228: delegator.clearAllCaches(distribute);
229:
230: return ServiceUtil.returnSuccess();
231: }
232:
233: /**
234: * Clear Cache Line Service: one of the following context parameters is required: value, dummyPK or primaryKey
235: *@param ctx The DispatchContext that this service is operating in
236: *@param context Map containing the input parameters
237: *@return Map with the result of the service, the output parameters
238: */
239: public static Map clearCacheLine(DispatchContext dctx, Map context) {
240: GenericDelegator delegator = dctx.getDelegator();
241: Boolean distributeBool = (Boolean) context.get("distribute");
242: boolean distribute = false;
243: if (distributeBool != null)
244: distribute = distributeBool.booleanValue();
245:
246: if (context.containsKey("value")) {
247: GenericValue value = (GenericValue) context.get("value");
248: if (Debug.infoOn())
249: Debug.logInfo(
250: "Got a clear cache line by value service call; entityName: "
251: + value.getEntityName(), module);
252: if (Debug.verboseOn())
253: Debug.logVerbose(
254: "Got a clear cache line by value service call; value: "
255: + value, module);
256: delegator.clearCacheLine(value, distribute);
257: } else if (context.containsKey("dummyPK")) {
258: GenericEntity dummyPK = (GenericEntity) context
259: .get("dummyPK");
260: if (Debug.infoOn())
261: Debug.logInfo(
262: "Got a clear cache line by dummyPK service call; entityName: "
263: + dummyPK.getEntityName(), module);
264: if (Debug.verboseOn())
265: Debug.logVerbose(
266: "Got a clear cache line by dummyPK service call; dummyPK: "
267: + dummyPK, module);
268: delegator.clearCacheLineFlexible(dummyPK, distribute);
269: } else if (context.containsKey("primaryKey")) {
270: GenericPK primaryKey = (GenericPK) context
271: .get("primaryKey");
272: if (Debug.infoOn())
273: Debug.logInfo(
274: "Got a clear cache line by primaryKey service call; entityName: "
275: + primaryKey.getEntityName(), module);
276: if (Debug.verboseOn())
277: Debug.logVerbose(
278: "Got a clear cache line by primaryKey service call; primaryKey: "
279: + primaryKey, module);
280: delegator.clearCacheLine(primaryKey, distribute);
281: }
282: return ServiceUtil.returnSuccess();
283: }
284: }
|