| 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.identity.IdentityArchiveService; |
| 24 | |
import org.kuali.rice.kim.api.identity.entity.EntityDefault; |
| 25 | |
import org.kuali.rice.kim.api.identity.principal.Principal; |
| 26 | |
import org.kuali.rice.kim.bo.entity.impl.KimEntityDefaultInfoCacheImpl; |
| 27 | |
import org.kuali.rice.kim.util.KimConstants; |
| 28 | |
import org.kuali.rice.krad.service.BusinessObjectService; |
| 29 | |
import org.kuali.rice.krad.service.KRADServiceLocatorInternal; |
| 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.getPropertyValueAsString(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.getPropertyValueAsString(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 EntityDefault getEntityDefaultFromArchive( 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 = businessObjectService.findByPrimaryKey(KimEntityDefaultInfoCacheImpl.class, criteria); |
| 109 | 0 | return (cachedValue == null) ? null : cachedValue.convertCacheToEntityDefaultInfo(); |
| 110 | |
} |
| 111 | |
|
| 112 | |
@Override |
| 113 | |
public EntityDefault getEntityDefaultFromArchiveByPrincipalId(String principalId) { |
| 114 | 0 | Map<String,String> criteria = new HashMap<String, String>(1); |
| 115 | 0 | criteria.put("principalId", principalId); |
| 116 | 0 | KimEntityDefaultInfoCacheImpl cachedValue = businessObjectService.findByPrimaryKey(KimEntityDefaultInfoCacheImpl.class, criteria); |
| 117 | 0 | return (cachedValue == null) ? null : cachedValue.convertCacheToEntityDefaultInfo(); |
| 118 | |
} |
| 119 | |
|
| 120 | |
@Override |
| 121 | |
public EntityDefault getEntityDefaultFromArchiveByPrincipalName(String principalName) { |
| 122 | 0 | Map<String,String> criteria = new HashMap<String, String>(1); |
| 123 | 0 | criteria.put("principalName", principalName); |
| 124 | 0 | Collection<KimEntityDefaultInfoCacheImpl> entities = businessObjectService.findMatching(KimEntityDefaultInfoCacheImpl.class, criteria); |
| 125 | 0 | return (entities == null || entities.isEmpty()) ? null : entities.iterator().next().convertCacheToEntityDefaultInfo(); |
| 126 | |
} |
| 127 | |
|
| 128 | |
@Override |
| 129 | |
public void saveEntityDefaultToArchive(EntityDefault entity) { |
| 130 | |
|
| 131 | 0 | if (getMaxWriteQueueSize() <= writeQueue.offerAndGetSize(entity) && |
| 132 | |
writer.requestSubmit()) { |
| 133 | 0 | KSBServiceLocator.getThreadPool().execute(maxQueueSizeExceededWriter); |
| 134 | |
} |
| 135 | 0 | } |
| 136 | |
|
| 137 | |
public void setBusinessObjectService(BusinessObjectService businessObjectService) { |
| 138 | 0 | this.businessObjectService = businessObjectService; |
| 139 | 0 | } |
| 140 | |
|
| 141 | |
public void setKualiConfigurationService( |
| 142 | |
ConfigurationService kualiConfigurationService) { |
| 143 | 0 | this.kualiConfigurationService = kualiConfigurationService; |
| 144 | 0 | } |
| 145 | |
|
| 146 | |
|
| 147 | |
@Override |
| 148 | |
public void afterPropertiesSet() throws Exception { |
| 149 | 0 | LOG.info("scheduling writer..."); |
| 150 | 0 | KSBServiceLocator.getScheduledPool().scheduleAtFixedRate(scheduledWriter, |
| 151 | |
getExecutionIntervalSeconds(), getExecutionIntervalSeconds(), TimeUnit.SECONDS); |
| 152 | 0 | } |
| 153 | |
|
| 154 | |
|
| 155 | |
@Override |
| 156 | |
public void destroy() throws Exception { |
| 157 | 0 | KSBServiceLocator.getThreadPool().execute(shutdownWriter); |
| 158 | 0 | } |
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
|
| 164 | |
|
| 165 | |
|
| 166 | |
|
| 167 | 0 | private class EntityArchiveWriter implements Callable { |
| 168 | |
|
| 169 | |
|
| 170 | 0 | AtomicBoolean currentlySubmitted = new AtomicBoolean(false); |
| 171 | |
|
| 172 | 0 | private final Comparator<Comparable> nullSafeComparator = new Comparator<Comparable>() { |
| 173 | |
@Override |
| 174 | |
public int compare(Comparable i1, Comparable i2) { |
| 175 | 0 | if (i1 != null && i2 != null) { |
| 176 | 0 | return i1.compareTo(i2); |
| 177 | 0 | } else if (i1 == null) { |
| 178 | 0 | if (i2 == null) { |
| 179 | 0 | return 0; |
| 180 | |
} else { |
| 181 | 0 | return -1; |
| 182 | |
} |
| 183 | |
} else { |
| 184 | 0 | return 1; |
| 185 | |
} |
| 186 | |
}; |
| 187 | |
}; |
| 188 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | 0 | private final Comparator<EntityDefault> kediComparator = new Comparator<EntityDefault>() { |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
@Override |
| 198 | |
public int compare(EntityDefault o1, EntityDefault o2) { |
| 199 | 0 | String entityId1 = (o1 == null) ? null : o1.getEntityId(); |
| 200 | 0 | String entityId2 = (o2 == null) ? null : o2.getEntityId(); |
| 201 | |
|
| 202 | 0 | int result = nullSafeComparator.compare(entityId1, entityId2); |
| 203 | |
|
| 204 | 0 | if (result == 0) { |
| 205 | 0 | result = getPrincipalIdsString(o1).compareTo(getPrincipalIdsString(o2)); |
| 206 | |
} |
| 207 | |
|
| 208 | 0 | return result; |
| 209 | |
} |
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
private String getPrincipalIdsString(EntityDefault entity) { |
| 218 | 0 | String result = ""; |
| 219 | 0 | if (entity != null) { |
| 220 | 0 | List<Principal> principals = entity.getPrincipals(); |
| 221 | 0 | if (principals != null) { |
| 222 | 0 | if (principals.size() == 1) { |
| 223 | 0 | result = principals.get(0).getPrincipalId(); |
| 224 | |
} else { |
| 225 | 0 | String [] ids = new String [principals.size()]; |
| 226 | 0 | int insertIndex = 0; |
| 227 | 0 | for (Principal principal : principals) { |
| 228 | 0 | ids[insertIndex++] = principal.getPrincipalId(); |
| 229 | |
} |
| 230 | 0 | Arrays.sort(ids); |
| 231 | 0 | result = StringUtils.join(ids, "\n"); |
| 232 | |
} |
| 233 | |
} |
| 234 | |
} |
| 235 | 0 | return result; |
| 236 | |
} |
| 237 | |
}; |
| 238 | |
|
| 239 | |
public boolean requestSubmit() { |
| 240 | 0 | return currentlySubmitted.compareAndSet(false, true); |
| 241 | |
} |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
@Override |
| 248 | |
public Object call() { |
| 249 | |
try { |
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | 0 | PlatformTransactionManager transactionManager = KRADServiceLocatorInternal.getTransactionManager(); |
| 254 | 0 | TransactionTemplate template = new TransactionTemplate(transactionManager); |
| 255 | 0 | template.execute(new TransactionCallback() { |
| 256 | |
@Override |
| 257 | |
public Object doInTransaction(TransactionStatus status) { |
| 258 | 0 | EntityDefault entity = null; |
| 259 | 0 | ArrayList<EntityDefault> entitiesToInsert = new ArrayList<EntityDefault>(getMaxWriteQueueSize()); |
| 260 | 0 | Set<String> deduper = new HashSet<String>(getMaxWriteQueueSize()); |
| 261 | |
|
| 262 | |
|
| 263 | 0 | while (entitiesToInsert.size() < getMaxWriteQueueSize() && null != (entity = writeQueue.poll())) { |
| 264 | 0 | if (deduper.add(entity.getEntityId())) { |
| 265 | 0 | entitiesToInsert.add(entity); |
| 266 | |
} |
| 267 | |
} |
| 268 | |
|
| 269 | 0 | Collections.sort(entitiesToInsert, kediComparator); |
| 270 | |
|
| 271 | 0 | for (EntityDefault entityToInsert : entitiesToInsert) { |
| 272 | 0 | businessObjectService.save( new KimEntityDefaultInfoCacheImpl( entityToInsert ) ); |
| 273 | |
} |
| 274 | 0 | return null; |
| 275 | |
} |
| 276 | |
}); |
| 277 | |
} finally { |
| 278 | 0 | currentlySubmitted.compareAndSet(true, false); |
| 279 | 0 | } |
| 280 | |
|
| 281 | 0 | return Boolean.TRUE; |
| 282 | |
} |
| 283 | |
} |
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | |
|
| 291 | |
|
| 292 | |
|
| 293 | 0 | private static class WriteQueue { |
| 294 | 0 | AtomicInteger writeQueueSize = new AtomicInteger(0); |
| 295 | 0 | ConcurrentLinkedQueue<EntityDefault> queue = new ConcurrentLinkedQueue<EntityDefault>(); |
| 296 | |
|
| 297 | |
public int offerAndGetSize(EntityDefault entity) { |
| 298 | 0 | queue.add(entity); |
| 299 | 0 | return writeQueueSize.incrementAndGet(); |
| 300 | |
} |
| 301 | |
|
| 302 | |
private EntityDefault poll() { |
| 303 | 0 | EntityDefault result = queue.poll(); |
| 304 | 0 | if (result != null) { writeQueueSize.decrementAndGet(); } |
| 305 | 0 | return result; |
| 306 | |
} |
| 307 | |
} |
| 308 | |
|
| 309 | |
|
| 310 | |
|
| 311 | |
|
| 312 | |
|
| 313 | |
|
| 314 | |
|
| 315 | |
private static class PreLogCallableWrapper<A> implements Callable<A> { |
| 316 | |
|
| 317 | |
private final Callable inner; |
| 318 | |
private final Level level; |
| 319 | |
private final String message; |
| 320 | |
|
| 321 | 0 | public PreLogCallableWrapper(Callable inner, Level level, String message) { |
| 322 | 0 | this.inner = inner; |
| 323 | 0 | this.level = level; |
| 324 | 0 | this.message = message; |
| 325 | 0 | } |
| 326 | |
|
| 327 | |
|
| 328 | |
|
| 329 | |
|
| 330 | |
|
| 331 | |
|
| 332 | |
@Override |
| 333 | |
@SuppressWarnings("unchecked") |
| 334 | |
public A call() throws Exception { |
| 335 | 0 | LOG.log(level, message); |
| 336 | 0 | return (A)inner.call(); |
| 337 | |
} |
| 338 | |
} |
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | 0 | private static class CallableAdapter implements Runnable { |
| 347 | |
|
| 348 | |
private final Callable callable; |
| 349 | |
|
| 350 | 0 | public CallableAdapter(Callable callable) { |
| 351 | 0 | this.callable = callable; |
| 352 | 0 | } |
| 353 | |
|
| 354 | |
@Override |
| 355 | |
public void run() { |
| 356 | |
try { |
| 357 | 0 | callable.call(); |
| 358 | 0 | } catch (Exception e) { |
| 359 | 0 | throw new RuntimeException(e); |
| 360 | 0 | } |
| 361 | 0 | } |
| 362 | |
} |
| 363 | |
} |