1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.kuali.rice.kim.service.impl; |
18 | |
|
19 | |
import org.apache.commons.lang.StringUtils; |
20 | |
import org.apache.log4j.Level; |
21 | |
import org.apache.log4j.Logger; |
22 | |
import org.kuali.rice.core.api.config.property.ConfigurationService; |
23 | |
import org.kuali.rice.kim.api.entity.principal.Principal; |
24 | |
import org.kuali.rice.kim.api.entity.services.IdentityArchiveService; |
25 | |
import org.kuali.rice.kim.bo.entity.dto.KimEntityDefaultInfo; |
26 | |
import org.kuali.rice.kim.bo.entity.impl.KimEntityDefaultInfoCacheImpl; |
27 | |
import org.kuali.rice.kim.util.KimConstants; |
28 | |
import org.kuali.rice.kns.service.BusinessObjectService; |
29 | |
import org.kuali.rice.kns.service.KNSServiceLocatorInternal; |
30 | |
import org.kuali.rice.ksb.service.KSBServiceLocator; |
31 | |
import org.springframework.beans.factory.DisposableBean; |
32 | |
import org.springframework.beans.factory.InitializingBean; |
33 | |
import org.springframework.transaction.PlatformTransactionManager; |
34 | |
import org.springframework.transaction.TransactionStatus; |
35 | |
import org.springframework.transaction.support.TransactionCallback; |
36 | |
import org.springframework.transaction.support.TransactionTemplate; |
37 | |
|
38 | |
import java.util.ArrayList; |
39 | |
import java.util.Arrays; |
40 | |
import java.util.Collection; |
41 | |
import java.util.Collections; |
42 | |
import java.util.Comparator; |
43 | |
import java.util.HashMap; |
44 | |
import java.util.HashSet; |
45 | |
import java.util.List; |
46 | |
import java.util.Map; |
47 | |
import java.util.Set; |
48 | |
import java.util.concurrent.Callable; |
49 | |
import java.util.concurrent.ConcurrentLinkedQueue; |
50 | |
import java.util.concurrent.TimeUnit; |
51 | |
import java.util.concurrent.atomic.AtomicBoolean; |
52 | |
import java.util.concurrent.atomic.AtomicInteger; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 0 | public class IdentityArchiveServiceImpl implements IdentityArchiveService, InitializingBean, DisposableBean { |
61 | 0 | private static final Logger LOG = Logger.getLogger( IdentityArchiveServiceImpl.class ); |
62 | |
|
63 | |
private BusinessObjectService businessObjectService; |
64 | |
private ConfigurationService kualiConfigurationService; |
65 | |
|
66 | |
private static final String EXEC_INTERVAL_SECS = "kim.identityArchiveServiceImpl.executionIntervalSeconds"; |
67 | |
private static final String MAX_WRITE_QUEUE_SIZE = "kim.identityArchiveServiceImpl.maxWriteQueueSize"; |
68 | |
private static final int EXECUTION_INTERVAL_SECONDS_DEFAULT = 600; |
69 | |
private static final int MAX_WRITE_QUEUE_SIZE_DEFAULT = 300; |
70 | |
|
71 | 0 | private final WriteQueue writeQueue = new WriteQueue(); |
72 | 0 | private final EntityArchiveWriter writer = new EntityArchiveWriter(); |
73 | |
|
74 | |
|
75 | 0 | private final Runnable maxQueueSizeExceededWriter = |
76 | |
new CallableAdapter(new PreLogCallableWrapper<Boolean>(writer, Level.DEBUG, "max size exceeded, flushing write queue")); |
77 | |
|
78 | |
|
79 | 0 | private final Runnable scheduledWriter = |
80 | |
new CallableAdapter(new PreLogCallableWrapper<Boolean>(writer, Level.DEBUG, "scheduled write out, flushing write queue")); |
81 | |
|
82 | |
|
83 | 0 | private final Runnable shutdownWriter = |
84 | |
new CallableAdapter(new PreLogCallableWrapper<Boolean>(writer, Level.DEBUG, "rice is shutting down, flushing write queue")); |
85 | |
|
86 | |
private int getExecutionIntervalSeconds() { |
87 | 0 | final String prop = kualiConfigurationService.getPropertyString(EXEC_INTERVAL_SECS); |
88 | |
try { |
89 | 0 | return Integer.valueOf(prop).intValue(); |
90 | 0 | } catch (NumberFormatException e) { |
91 | 0 | return EXECUTION_INTERVAL_SECONDS_DEFAULT; |
92 | |
} |
93 | |
} |
94 | |
|
95 | |
private int getMaxWriteQueueSize() { |
96 | 0 | final String prop = kualiConfigurationService.getPropertyString(MAX_WRITE_QUEUE_SIZE); |
97 | |
try { |
98 | 0 | return Integer.valueOf(prop).intValue(); |
99 | 0 | } catch (NumberFormatException e) { |
100 | 0 | return MAX_WRITE_QUEUE_SIZE_DEFAULT; |
101 | |
} |
102 | |
} |
103 | |
|
104 | |
@Override |
105 | |
public KimEntityDefaultInfo getEntityDefaultInfoFromArchive( String entityId ) { |
106 | 0 | Map<String,String> criteria = new HashMap<String, String>(1); |
107 | 0 | criteria.put(KimConstants.PrimaryKeyConstants.ENTITY_ID, entityId); |
108 | 0 | KimEntityDefaultInfoCacheImpl cachedValue = getBusinessObjectService().findByPrimaryKey(KimEntityDefaultInfoCacheImpl.class, criteria); |
109 | 0 | return (cachedValue == null) ? null : cachedValue.convertCacheToEntityDefaultInfo(); |
110 | |
} |
111 | |
|
112 | |
@Override |
113 | |
public KimEntityDefaultInfo getEntityDefaultInfoFromArchiveByPrincipalId( String principalId ) { |
114 | 0 | Map<String,String> criteria = new HashMap<String, String>(1); |
115 | 0 | criteria.put("principalId", principalId); |
116 | 0 | KimEntityDefaultInfoCacheImpl cachedValue = getBusinessObjectService().findByPrimaryKey(KimEntityDefaultInfoCacheImpl.class, criteria); |
117 | 0 | return (cachedValue == null) ? null : cachedValue.convertCacheToEntityDefaultInfo(); |
118 | |
} |
119 | |
|
120 | |
@Override |
121 | |
public KimEntityDefaultInfo getEntityDefaultInfoFromArchiveByPrincipalName( String principalName ) { |
122 | 0 | Map<String,String> criteria = new HashMap<String, String>(1); |
123 | 0 | criteria.put("principalName", principalName); |
124 | 0 | Collection<KimEntityDefaultInfoCacheImpl> entities = getBusinessObjectService().findMatching(KimEntityDefaultInfoCacheImpl.class, criteria); |
125 | 0 | return (entities == null || entities.size() == 0) ? null : entities.iterator().next().convertCacheToEntityDefaultInfo(); |
126 | |
} |
127 | |
|
128 | |
@Override |
129 | |
public void saveDefaultInfoToArchive( KimEntityDefaultInfo entity ) { |
130 | |
|
131 | 0 | if (getMaxWriteQueueSize() <= writeQueue.offerAndGetSize(entity) && |
132 | |
writer.requestSubmit()) { |
133 | 0 | KSBServiceLocator.getThreadPool().execute(maxQueueSizeExceededWriter); |
134 | |
} |
135 | 0 | } |
136 | |
|
137 | |
public BusinessObjectService getBusinessObjectService() { |
138 | 0 | return this.businessObjectService; |
139 | |
} |
140 | |
|
141 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
142 | 0 | this.businessObjectService = businessObjectService; |
143 | 0 | } |
144 | |
|
145 | |
public ConfigurationService getKualiConfigurationService() { |
146 | 0 | return this.kualiConfigurationService; |
147 | |
} |
148 | |
|
149 | |
public void setKualiConfigurationService( |
150 | |
ConfigurationService kualiConfigurationService) { |
151 | 0 | this.kualiConfigurationService = kualiConfigurationService; |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
@Override |
156 | |
public void afterPropertiesSet() throws Exception { |
157 | 0 | LOG.info("scheduling writer..."); |
158 | 0 | KSBServiceLocator.getScheduledPool().scheduleAtFixedRate(scheduledWriter, |
159 | |
getExecutionIntervalSeconds(), getExecutionIntervalSeconds(), TimeUnit.SECONDS); |
160 | 0 | } |
161 | |
|
162 | |
|
163 | |
@Override |
164 | |
public void destroy() throws Exception { |
165 | 0 | KSBServiceLocator.getThreadPool().execute(shutdownWriter); |
166 | 0 | } |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | 0 | private class EntityArchiveWriter implements Callable { |
176 | |
|
177 | |
|
178 | 0 | AtomicBoolean currentlySubmitted = new AtomicBoolean(false); |
179 | |
|
180 | 0 | private final Comparator<Comparable> nullSafeComparator = new Comparator<Comparable>() { |
181 | |
@Override |
182 | |
public int compare(Comparable i1, Comparable i2) { |
183 | 0 | if (i1 != null && i2 != null) { |
184 | 0 | return i1.compareTo(i2); |
185 | 0 | } else if (i1 == null) { |
186 | 0 | if (i2 == null) { |
187 | 0 | return 0; |
188 | |
} else { |
189 | 0 | return -1; |
190 | |
} |
191 | |
} else { |
192 | 0 | return 1; |
193 | |
} |
194 | |
}; |
195 | |
}; |
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | 0 | private final Comparator<KimEntityDefaultInfo> kediComparator = new Comparator<KimEntityDefaultInfo>() { |
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
@Override |
206 | |
public int compare(KimEntityDefaultInfo o1, KimEntityDefaultInfo o2) { |
207 | 0 | String entityId1 = (o1 == null) ? null : o1.getEntityId(); |
208 | 0 | String entityId2 = (o2 == null) ? null : o2.getEntityId(); |
209 | |
|
210 | 0 | int result = nullSafeComparator.compare(entityId1, entityId2); |
211 | |
|
212 | 0 | if (result == 0) { |
213 | 0 | result = getPrincipalIdsString(o1).compareTo(getPrincipalIdsString(o2)); |
214 | |
} |
215 | |
|
216 | 0 | return result; |
217 | |
} |
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
private String getPrincipalIdsString(KimEntityDefaultInfo entity) { |
226 | 0 | String result = ""; |
227 | 0 | if (entity != null) { |
228 | 0 | List<Principal> principals = entity.getPrincipals(); |
229 | 0 | if (principals != null) { |
230 | 0 | if (principals.size() == 1) { |
231 | 0 | result = principals.get(0).getPrincipalId(); |
232 | |
} else { |
233 | 0 | String [] ids = new String [principals.size()]; |
234 | 0 | int insertIndex = 0; |
235 | 0 | for (Principal principal : principals) { |
236 | 0 | ids[insertIndex++] = principal.getPrincipalId(); |
237 | |
} |
238 | 0 | Arrays.sort(ids); |
239 | 0 | result = StringUtils.join(ids, "\n"); |
240 | |
} |
241 | |
} |
242 | |
} |
243 | 0 | return result; |
244 | |
} |
245 | |
}; |
246 | |
|
247 | |
public boolean requestSubmit() { |
248 | 0 | return currentlySubmitted.compareAndSet(false, true); |
249 | |
} |
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
@Override |
256 | |
public Object call() { |
257 | |
try { |
258 | |
|
259 | |
|
260 | |
|
261 | 0 | PlatformTransactionManager transactionManager = KNSServiceLocatorInternal.getTransactionManager(); |
262 | 0 | TransactionTemplate template = new TransactionTemplate(transactionManager); |
263 | 0 | template.execute(new TransactionCallback() { |
264 | |
@Override |
265 | |
public Object doInTransaction(TransactionStatus status) { |
266 | 0 | KimEntityDefaultInfo entity = null; |
267 | 0 | ArrayList<KimEntityDefaultInfo> entitiesToInsert = new ArrayList<KimEntityDefaultInfo>(getMaxWriteQueueSize()); |
268 | 0 | Set<String> deduper = new HashSet<String>(getMaxWriteQueueSize()); |
269 | |
|
270 | |
|
271 | 0 | while (entitiesToInsert.size() < getMaxWriteQueueSize() && null != (entity = writeQueue.poll())) { |
272 | 0 | if (deduper.add(entity.getEntityId())) { |
273 | 0 | entitiesToInsert.add(entity); |
274 | |
} |
275 | |
} |
276 | |
|
277 | 0 | Collections.sort(entitiesToInsert, kediComparator); |
278 | |
|
279 | 0 | for (KimEntityDefaultInfo entityToInsert : entitiesToInsert) { |
280 | 0 | getBusinessObjectService().save( new KimEntityDefaultInfoCacheImpl( entityToInsert ) ); |
281 | |
} |
282 | 0 | return null; |
283 | |
} |
284 | |
}); |
285 | |
} finally { |
286 | 0 | currentlySubmitted.compareAndSet(true, false); |
287 | 0 | } |
288 | |
|
289 | 0 | return Boolean.TRUE; |
290 | |
} |
291 | |
} |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | 0 | private static class WriteQueue { |
302 | 0 | AtomicInteger writeQueueSize = new AtomicInteger(0); |
303 | 0 | ConcurrentLinkedQueue<KimEntityDefaultInfo> queue = new ConcurrentLinkedQueue<KimEntityDefaultInfo>(); |
304 | |
|
305 | |
public int offerAndGetSize(KimEntityDefaultInfo entity) { |
306 | 0 | queue.add(entity); |
307 | 0 | return writeQueueSize.incrementAndGet(); |
308 | |
} |
309 | |
|
310 | |
private KimEntityDefaultInfo poll() { |
311 | 0 | KimEntityDefaultInfo result = queue.poll(); |
312 | 0 | if (result != null) { writeQueueSize.decrementAndGet(); } |
313 | 0 | return result; |
314 | |
} |
315 | |
} |
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
|
323 | |
private static class PreLogCallableWrapper<A> implements Callable<A> { |
324 | |
|
325 | |
private final Callable inner; |
326 | |
private final Level level; |
327 | |
private final String message; |
328 | |
|
329 | 0 | public PreLogCallableWrapper(Callable inner, Level level, String message) { |
330 | 0 | this.inner = inner; |
331 | 0 | this.level = level; |
332 | 0 | this.message = message; |
333 | 0 | } |
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
@Override |
341 | |
@SuppressWarnings("unchecked") |
342 | |
public A call() throws Exception { |
343 | 0 | LOG.log(level, message); |
344 | 0 | return (A)inner.call(); |
345 | |
} |
346 | |
} |
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | 0 | private static class CallableAdapter implements Runnable { |
355 | |
|
356 | |
private final Callable callable; |
357 | |
|
358 | 0 | public CallableAdapter(Callable callable) { |
359 | 0 | this.callable = callable; |
360 | 0 | } |
361 | |
|
362 | |
@Override |
363 | |
public void run() { |
364 | |
try { |
365 | 0 | callable.call(); |
366 | 0 | } catch (Exception e) { |
367 | 0 | throw new RuntimeException(e); |
368 | 0 | } |
369 | 0 | } |
370 | |
} |
371 | |
} |