1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.domain.dao;
17
18 import org.acegisecurity.domain.PersistableEntity;
19
20 import org.springframework.util.Assert;
21
22 import java.lang.reflect.Method;
23 import java.util.Collection;
24 import java.util.Iterator;
25
26
27 /***
28 * Convenience methods that support eviction of <code>PersistableEntity</code>s
29 * from those objects that implement {@link EvictionCapable}.
30 *
31 * @author Ben Alex
32 * @version $Id: EvictionUtils.java,v 1.6 2005/11/17 00:55:47 benalex Exp $
33 */
34 public class EvictionUtils {
35
36
37 /***
38 * Evicts the <code>PersistableEntity</code> using the passed
39 * <code>Object</code> (provided that the passed <code>Object</code>
40 * implements <code>EvictionCapable</code>).
41 *
42 * @param daoOrServices the potential source for
43 * <code>EvictionCapable</code> services (never <code>null</code>)
44 * @param entity to evict (can be <code>null</code>)
45 */
46 public static void evictIfRequired(Object daoOrServices,
47 PersistableEntity entity) {
48 EvictionCapable evictor = getEvictionCapable(daoOrServices);
49
50 if (evictor != null && entity != null) {
51 evictor.evict(entity);
52 }
53 }
54
55 /***
56 * Evicts the <code>PersistableEntity</code> using the passed
57 * <code>Object</code> (provided that the passed <code>Object</code>
58 * implements <code>EvictionCapable</code>), along with expressly
59 * evicting every <code>PersistableEntity</code> returned by the
60 * <code>PersistableEntity</code>'s getters.
61 *
62 * @param daoOrServices the potential source for
63 * <code>EvictionCapable</code> services (never <code>null</code>)
64 * @param entity to evict includnig its getter results (can be <code>null</code>)
65 */
66 public static void evictPopulatedIfRequired(Object daoOrServices,
67 PersistableEntity entity) {
68 EvictionCapable evictor = getEvictionCapable(daoOrServices);
69
70 if (evictor != null && entity != null) {
71 evictor.evict(entity);
72
73 Method[] methods = entity.getClass().getMethods();
74 for (int i = 0; i < methods.length; i++) {
75 if (methods[i].getName().startsWith("get") && methods[i].getParameterTypes().length == 0) {
76 try {
77 Object result = methods[i].invoke(entity, new Object[] {});
78 if (result instanceof PersistableEntity) {
79 evictor.evict((PersistableEntity) result);
80 }
81 } catch (Exception ignored) {}
82 }
83 }
84
85 }
86 }
87
88 /***
89 * Evicts each <code>PersistableEntity</code> element of the passed
90 * <code>Collection</code> using the passed <code>Object</code> (provided
91 * that the passed <code>Object</code> implements
92 * <code>EvictionCapable</code>).
93 *
94 * @param daoOrServices the potential source for
95 * <code>EvictionCapable</code> services (never <code>null</code>)
96 * @param collection whose members to evict (never <code>null</code>)
97 */
98 public static void evictIfRequired(Object daoOrServices,
99 Collection<? extends Object> collection) {
100 Assert.notNull(collection, "Cannot evict a null Collection");
101
102 if (getEvictionCapable(daoOrServices) == null) {
103
104 return;
105 }
106
107 Iterator<? extends Object> iter = collection.iterator();
108
109 while (iter.hasNext()) {
110 Object obj = iter.next();
111
112 if (obj instanceof PersistableEntity) {
113 evictIfRequired(daoOrServices, (PersistableEntity) obj);
114 }
115 }
116 }
117
118 /***
119 * Obtain the <code>EvictionCapable</code> from the passed argument, or
120 * <code>null</code>.
121 *
122 * @param daoOrServices to check if provides eviction services
123 *
124 * @return the <code>EvictionCapable</code> object or <code>null</code> if
125 * the object does not provide eviction services
126 */
127 private static EvictionCapable getEvictionCapable(Object daoOrServices) {
128 Assert.notNull(daoOrServices,
129 "Cannot evict if the object that may provide EvictionCapable is null");
130
131 if (daoOrServices instanceof EvictionCapable) {
132 return (EvictionCapable) daoOrServices;
133 } else {
134 return null;
135 }
136 }
137 }