Search in sources :

Example 1 with VirAttrCacheValue

use of org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue in project syncope by apache.

the class AbstractPropagationTaskExecutor method getRemoteObject.

/**
 * Get remote object for given task.
 *
 * @param connector connector facade proxy.
 * @param task current propagation task.
 * @param provision provision
 * @param latest 'FALSE' to retrieve object using old connObjectKey if not null.
 * @return remote connector object.
 */
protected ConnectorObject getRemoteObject(final PropagationTask task, final Connector connector, final Provision provision, final boolean latest) {
    String connObjectKey = latest || task.getOldConnObjectKey() == null ? task.getConnObjectKey() : task.getOldConnObjectKey();
    Set<MappingItem> linkingMappingItems = virSchemaDAO.findByProvision(provision).stream().map(schema -> schema.asLinkingMappingItem()).collect(Collectors.toSet());
    ConnectorObject obj = null;
    Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(provision);
    if (connObjectKeyItem.isPresent()) {
        try {
            obj = connector.getObject(new ObjectClass(task.getObjectClassName()), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKey), MappingUtils.buildOperationOptions(new IteratorChain<>(MappingUtils.getPropagationItems(provision.getMapping().getItems()).iterator(), linkingMappingItems.iterator())));
            for (MappingItem item : linkingMappingItems) {
                Attribute attr = obj.getAttributeByName(item.getExtAttrName());
                if (attr == null) {
                    virAttrCache.expire(task.getAnyType(), task.getEntityKey(), item.getIntAttrName());
                } else {
                    VirAttrCacheValue cacheValue = new VirAttrCacheValue();
                    cacheValue.setValues(attr.getValue());
                    virAttrCache.put(task.getAnyType(), task.getEntityKey(), item.getIntAttrName(), cacheValue);
                }
            }
        } catch (TimeoutException toe) {
            LOG.debug("Request timeout", toe);
            throw toe;
        } catch (RuntimeException ignore) {
            LOG.debug("While resolving {}", connObjectKey, ignore);
        }
    }
    return obj;
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) AuditElements(org.apache.syncope.common.lib.types.AuditElements) ResourceOperation(org.apache.syncope.common.lib.types.ResourceOperation) NotificationManager(org.apache.syncope.core.provisioning.api.notification.NotificationManager) StringUtils(org.apache.commons.lang3.StringUtils) PropagationTask(org.apache.syncope.core.persistence.api.entity.task.PropagationTask) VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) Attribute(org.identityconnectors.framework.common.objects.Attribute) PropagationTaskTO(org.apache.syncope.common.lib.to.PropagationTaskTO) GroupDAO(org.apache.syncope.core.persistence.api.dao.GroupDAO) AnyObjectDAO(org.apache.syncope.core.persistence.api.dao.AnyObjectDAO) ConnObjectUtils(org.apache.syncope.core.provisioning.java.utils.ConnObjectUtils) Map(java.util.Map) OrgUnit(org.apache.syncope.core.persistence.api.entity.resource.OrgUnit) ExceptionUtils2(org.apache.syncope.core.provisioning.api.utils.ExceptionUtils2) ExecTO(org.apache.syncope.common.lib.to.ExecTO) UserDAO(org.apache.syncope.core.persistence.api.dao.UserDAO) OrgUnitItem(org.apache.syncope.core.persistence.api.entity.resource.OrgUnitItem) Collection(java.util.Collection) Set(java.util.Set) PropagationActions(org.apache.syncope.core.provisioning.api.propagation.PropagationActions) Collectors(java.util.stream.Collectors) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) ImplementationManager(org.apache.syncope.core.spring.ImplementationManager) EntityFactory(org.apache.syncope.core.persistence.api.entity.EntityFactory) Connector(org.apache.syncope.core.provisioning.api.Connector) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) List(java.util.List) Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) AttributeUtil(org.identityconnectors.framework.common.objects.AttributeUtil) TaskUtilsFactory(org.apache.syncope.core.persistence.api.entity.task.TaskUtilsFactory) AuditManager(org.apache.syncope.core.provisioning.api.AuditManager) AttributeBuilder(org.identityconnectors.framework.common.objects.AttributeBuilder) ObjectClass(org.identityconnectors.framework.common.objects.ObjectClass) ConnectorFactory(org.apache.syncope.core.provisioning.api.ConnectorFactory) PropagationTaskExecutor(org.apache.syncope.core.provisioning.api.propagation.PropagationTaskExecutor) Optional(java.util.Optional) ExternalResourceDAO(org.apache.syncope.core.persistence.api.dao.ExternalResourceDAO) VirAttrCache(org.apache.syncope.core.provisioning.api.cache.VirAttrCache) POJOHelper(org.apache.syncope.core.provisioning.api.serialization.POJOHelper) PropagationTaskExecStatus(org.apache.syncope.common.lib.types.PropagationTaskExecStatus) TaskDataBinder(org.apache.syncope.core.provisioning.api.data.TaskDataBinder) ConnectorObjectBuilder(org.identityconnectors.framework.common.objects.ConnectorObjectBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) ConnectorException(org.identityconnectors.framework.common.exceptions.ConnectorException) ArrayList(java.util.ArrayList) TaskDAO(org.apache.syncope.core.persistence.api.dao.TaskDAO) HashSet(java.util.HashSet) Result(org.apache.syncope.common.lib.types.AuditElements.Result) TaskExec(org.apache.syncope.core.persistence.api.entity.task.TaskExec) TimeoutException(org.apache.syncope.core.provisioning.api.TimeoutException) Logger(org.slf4j.Logger) Uid(org.identityconnectors.framework.common.objects.Uid) PropagationException(org.apache.syncope.core.provisioning.api.propagation.PropagationException) IteratorChain(org.apache.syncope.common.lib.collections.IteratorChain) PropagationReporter(org.apache.syncope.core.provisioning.api.propagation.PropagationReporter) Name(org.identityconnectors.framework.common.objects.Name) MappingUtils(org.apache.syncope.core.provisioning.java.utils.MappingUtils) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) VirSchemaDAO(org.apache.syncope.core.persistence.api.dao.VirSchemaDAO) Collections(java.util.Collections) TraceLevel(org.apache.syncope.common.lib.types.TraceLevel) Transactional(org.springframework.transaction.annotation.Transactional) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) ObjectClass(org.identityconnectors.framework.common.objects.ObjectClass) Attribute(org.identityconnectors.framework.common.objects.Attribute) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) TimeoutException(org.apache.syncope.core.provisioning.api.TimeoutException)

Example 2 with VirAttrCacheValue

use of org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue in project syncope by apache.

the class AbstractPullResultHandler method doHandle.

/**
 * Look into SyncDelta and take necessary profile.getActions() (create / update / delete) on any object(s).
 *
 * @param delta returned by the underlying profile.getConnector()
 * @param provision provisioning info
 * @throws JobExecutionException in case of pull failure.
 */
protected void doHandle(final SyncDelta delta, final Provision provision) throws JobExecutionException {
    AnyUtils anyUtils = getAnyUtils();
    LOG.debug("Process {} for {} as {}", delta.getDeltaType(), delta.getUid().getUidValue(), delta.getObject().getObjectClass());
    SyncDelta processed = delta;
    for (PullActions action : profile.getActions()) {
        processed = action.preprocess(profile, processed);
    }
    LOG.debug("Transformed {} for {} as {}", processed.getDeltaType(), processed.getUid().getUidValue(), processed.getObject().getObjectClass());
    try {
        List<String> anyKeys = pullUtils.match(processed.getObject(), provision, anyUtils);
        LOG.debug("Match(es) found for {} as {}: {}", processed.getUid().getUidValue(), processed.getObject().getObjectClass(), anyKeys);
        if (anyKeys.size() > 1) {
            switch(profile.getResAct()) {
                case IGNORE:
                    throw new IllegalStateException("More than one match " + anyKeys);
                case FIRSTMATCH:
                    anyKeys = anyKeys.subList(0, 1);
                    break;
                case LASTMATCH:
                    anyKeys = anyKeys.subList(anyKeys.size() - 1, anyKeys.size());
                    break;
                default:
            }
        }
        if (SyncDeltaType.CREATE_OR_UPDATE == processed.getDeltaType()) {
            if (anyKeys.isEmpty()) {
                switch(profile.getTask().getUnmatchingRule()) {
                    case ASSIGN:
                        profile.getResults().addAll(assign(processed, provision, anyUtils));
                        break;
                    case PROVISION:
                        profile.getResults().addAll(provision(processed, provision, anyUtils));
                        break;
                    case IGNORE:
                        profile.getResults().addAll(ignore(processed, null, provision, false));
                        break;
                    default:
                }
            } else {
                // update VirAttrCache
                for (VirSchema virSchema : virSchemaDAO.findByProvision(provision)) {
                    Attribute attr = processed.getObject().getAttributeByName(virSchema.getExtAttrName());
                    for (String anyKey : anyKeys) {
                        if (attr == null) {
                            virAttrCache.expire(provision.getAnyType().getKey(), anyKey, virSchema.getKey());
                        } else {
                            VirAttrCacheValue cacheValue = new VirAttrCacheValue();
                            cacheValue.setValues(attr.getValue());
                            virAttrCache.put(provision.getAnyType().getKey(), anyKey, virSchema.getKey(), cacheValue);
                        }
                    }
                }
                switch(profile.getTask().getMatchingRule()) {
                    case UPDATE:
                        profile.getResults().addAll(update(processed, anyKeys, provision));
                        break;
                    case DEPROVISION:
                        profile.getResults().addAll(deprovision(processed, anyKeys, provision, false));
                        break;
                    case UNASSIGN:
                        profile.getResults().addAll(deprovision(processed, anyKeys, provision, true));
                        break;
                    case LINK:
                        profile.getResults().addAll(link(processed, anyKeys, provision, false));
                        break;
                    case UNLINK:
                        profile.getResults().addAll(link(processed, anyKeys, provision, true));
                        break;
                    case IGNORE:
                        profile.getResults().addAll(ignore(processed, anyKeys, provision, true));
                        break;
                    default:
                }
            }
        } else if (SyncDeltaType.DELETE == processed.getDeltaType()) {
            if (anyKeys.isEmpty()) {
                finalize(ResourceOperation.DELETE.name().toLowerCase(), Result.SUCCESS, null, null, processed);
                LOG.debug("No match found for deletion");
            } else {
                profile.getResults().addAll(delete(processed, anyKeys, provision));
            }
        }
    } catch (IllegalStateException | IllegalArgumentException e) {
        LOG.warn(e.getMessage());
    }
}
Also used : SyncDelta(org.identityconnectors.framework.common.objects.SyncDelta) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) Attribute(org.identityconnectors.framework.common.objects.Attribute) PullActions(org.apache.syncope.core.provisioning.api.pushpull.PullActions) VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) AnyUtils(org.apache.syncope.core.persistence.api.entity.AnyUtils)

Example 3 with VirAttrCacheValue

use of org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue in project syncope by apache.

the class VirAttrHandlerImpl method getValues.

private Map<VirSchema, List<String>> getValues(final Any<?> any, final Set<VirSchema> schemas) {
    Set<ExternalResource> ownedResources = anyUtilsFactory.getInstance(any).getAllResources(any);
    Map<VirSchema, List<String>> result = new HashMap<>();
    Map<Provision, Set<VirSchema>> toRead = new HashMap<>();
    for (VirSchema schema : schemas) {
        if (ownedResources.contains(schema.getProvision().getResource())) {
            VirAttrCacheValue virAttrCacheValue = virAttrCache.get(any.getType().getKey(), any.getKey(), schema.getKey());
            if (virAttrCache.isValidEntry(virAttrCacheValue)) {
                LOG.debug("Values for {} found in cache: {}", schema, virAttrCacheValue);
                result.put(schema, virAttrCacheValue.getValues());
            } else {
                Set<VirSchema> schemasToRead = toRead.get(schema.getProvision());
                if (schemasToRead == null) {
                    schemasToRead = new HashSet<>();
                    toRead.put(schema.getProvision(), schemasToRead);
                }
                schemasToRead.add(schema);
            }
        } else {
            LOG.debug("Not considering {} since {} is not assigned to {}", schema, any, schema.getProvision().getResource());
        }
    }
    for (Map.Entry<Provision, Set<VirSchema>> entry : toRead.entrySet()) {
        LOG.debug("About to read from {}: {}", entry.getKey(), entry.getValue());
        Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(entry.getKey());
        String connObjectKeyValue = connObjectKeyItem.isPresent() ? mappingManager.getConnObjectKeyValue(any, entry.getKey()).orElse(null) : null;
        if (!connObjectKeyItem.isPresent() || connObjectKeyValue == null) {
            LOG.error("No ConnObjectKey or value found for {}, ignoring...", entry.getKey());
        } else {
            Set<MappingItem> linkingMappingItems = new HashSet<>();
            linkingMappingItems.add(connObjectKeyItem.get());
            linkingMappingItems.addAll(entry.getValue().stream().map(schema -> schema.asLinkingMappingItem()).collect(Collectors.toSet()));
            Connector connector = connFactory.getConnector(entry.getKey().getResource());
            try {
                ConnectorObject connectorObject = connector.getObject(entry.getKey().getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue), MappingUtils.buildOperationOptions(linkingMappingItems.iterator()));
                if (connectorObject == null) {
                    LOG.debug("No read from {} with filter '{} == {}'", entry.getKey(), connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue);
                } else {
                    entry.getValue().forEach(schema -> {
                        Attribute attr = connectorObject.getAttributeByName(schema.getExtAttrName());
                        if (attr != null) {
                            VirAttrCacheValue virAttrCacheValue = new VirAttrCacheValue();
                            virAttrCacheValue.setValues(attr.getValue());
                            virAttrCache.put(any.getType().getKey(), any.getKey(), schema.getKey(), virAttrCacheValue);
                            LOG.debug("Values for {} set in cache: {}", schema, virAttrCacheValue);
                            result.put(schema, virAttrCacheValue.getValues());
                        }
                    });
                }
            } catch (Exception e) {
                LOG.error("Error reading from {}", entry.getKey(), e);
            }
        }
    }
    return result;
}
Also used : Provision(org.apache.syncope.core.persistence.api.entity.resource.Provision) Connector(org.apache.syncope.core.provisioning.api.Connector) MappingItem(org.apache.syncope.core.persistence.api.entity.resource.MappingItem) HashSet(java.util.HashSet) Set(java.util.Set) VirSchema(org.apache.syncope.core.persistence.api.entity.VirSchema) HashMap(java.util.HashMap) Attribute(org.identityconnectors.framework.common.objects.Attribute) ConnectorObject(org.identityconnectors.framework.common.objects.ConnectorObject) ExternalResource(org.apache.syncope.core.persistence.api.entity.resource.ExternalResource) List(java.util.List) VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 4 with VirAttrCacheValue

use of org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue in project syncope by apache.

the class MemoryVirAttrCache method free.

/**
 * Remove expired entries if exist. If required, one entry at least (the latest recently used) will be taken off.
 * This method is not thread safe: the caller have to take care to synchronize the call.
 */
private void free() {
    final Set<VirAttrCacheKey> toBeRemoved = new HashSet<>();
    Map.Entry<VirAttrCacheKey, VirAttrCacheValue> latest = null;
    for (Map.Entry<VirAttrCacheKey, VirAttrCacheValue> entry : cache.entrySet()) {
        if (isValidEntry(entry.getValue())) {
            final Date date = entry.getValue().getLastAccessDate();
            if (latest == null || latest.getValue().getLastAccessDate().after(date)) {
                latest = entry;
            }
        } else {
            toBeRemoved.add(entry.getKey());
        }
    }
    if (toBeRemoved.isEmpty() && latest != null) {
        // remove the oldest entry
        cache.remove(latest.getKey());
    } else {
        // remove expired entries
        cache.keySet().removeAll(toBeRemoved);
    }
}
Also used : VirAttrCacheValue(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue) VirAttrCacheKey(org.apache.syncope.core.provisioning.api.cache.VirAttrCacheKey) Map(java.util.Map) HashMap(java.util.HashMap) Date(java.util.Date) HashSet(java.util.HashSet)

Aggregations

VirAttrCacheValue (org.apache.syncope.core.provisioning.api.cache.VirAttrCacheValue)4 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Attribute (org.identityconnectors.framework.common.objects.Attribute)3 Date (java.util.Date)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Set (java.util.Set)2 VirSchema (org.apache.syncope.core.persistence.api.entity.VirSchema)2 ExternalResource (org.apache.syncope.core.persistence.api.entity.resource.ExternalResource)2 MappingItem (org.apache.syncope.core.persistence.api.entity.resource.MappingItem)2 Provision (org.apache.syncope.core.persistence.api.entity.resource.Provision)2 Connector (org.apache.syncope.core.provisioning.api.Connector)2 ConnectorObject (org.identityconnectors.framework.common.objects.ConnectorObject)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Optional (java.util.Optional)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1