KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entityext > cache > EntityCacheServices


1 /*
2  * $Id: EntityCacheServices.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entityext.cache;
26
27 import java.util.Map JavaDoc;
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 /**
45  * Entity Engine Cache Services
46  *
47  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
48  * @version $Rev: 5462 $
49  * @since 2.0
50  */

51 public class EntityCacheServices implements DistributedCacheClear {
52     
53     public static final String JavaDoc module = EntityCacheServices.class.getName();
54
55     protected GenericDelegator delegator = null;
56     protected LocalDispatcher dispatcher = null;
57     protected String JavaDoc userLoginId = null;
58
59     public EntityCacheServices() {}
60
61     public void setDelegator(GenericDelegator delegator, String JavaDoc 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         // Debug.logInfo("running distributedClearCacheLine for value: " + value, module);
79
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         // Debug.logInfo("running distributedClearCacheLineFlexible for dummyPK: " + dummyPK, module);
99
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 JavaDoc entityName, EntityCondition condition) {
118         // Debug.logInfo("running distributedClearCacheLineByCondition for (name, condition): " + entityName + ", " + condition + ")", module);
119
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         // Debug.logInfo("running distributedClearCacheLine for primaryKey: " + primaryKey, module);
139
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     /**
177      * Clear All Entity Caches Service
178      *@param ctx The DispatchContext that this service is operating in
179      *@param context Map containing the input parameters
180      *@return Map with the result of the service, the output parameters
181      */

182     public static Map JavaDoc clearAllEntityCaches(DispatchContext dctx, Map JavaDoc context) {
183         GenericDelegator delegator = dctx.getDelegator();
184         Boolean JavaDoc distributeBool = (Boolean JavaDoc) 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     /**
194      * Clear Cache Line Service: one of the following context parameters is required: value, dummyPK or primaryKey
195      *@param ctx The DispatchContext that this service is operating in
196      *@param context Map containing the input parameters
197      *@return Map with the result of the service, the output parameters
198      */

199     public static Map JavaDoc clearCacheLine(DispatchContext dctx, Map JavaDoc context) {
200         GenericDelegator delegator = dctx.getDelegator();
201         Boolean JavaDoc distributeBool = (Boolean JavaDoc) 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 JavaDoc entityName = (String JavaDoc) 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