Search in sources :

Example 1 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException in project Gaffer by gchq.

the class NamedOperationJCSCache method getAllNamedOperations.

/**
     * Gets all the keys from the cache and iterates through each ExtendedNamedOperation stored in the cache. Assuming
     * the user has read permission (therefore can execute the NamedOperation) it either adds a NamedOperation or
     * ExtendedNamedOperation to a set, depending on the simple flag which would ordinarily be set to true.
     *
     * @param user   The user making the request
     * @param simple flag determining whether to return a set of NamedOperations with a basic name and description or
     *               the full ExtendedNamedOperation with details of the opChain, and access controls
     * @return a set of NamedOperations
     */
@Override
public CloseableIterable<NamedOperation> getAllNamedOperations(final User user, final boolean simple) {
    Set keys = getCache().getGroupKeys(CACHE_GROUP);
    Set<NamedOperation> executables = new HashSet<>();
    for (final Object key : keys) {
        try {
            ExtendedNamedOperation op = getFromCache((String) key);
            if (op.hasReadAccess(user)) {
                if (simple) {
                    executables.add(op.getBasic());
                } else {
                    executables.add(op);
                }
            }
        } catch (CacheOperationFailedException e) {
            LOGGER.error(e.getMessage(), e);
        }
    }
    return new WrappedCloseableIterable<>(executables);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) NamedOperation(uk.gov.gchq.gaffer.named.operation.NamedOperation) HashSet(java.util.HashSet)

Example 2 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException in project Gaffer by gchq.

the class NamedOperationHandler method doOperation.

/**
     * Gets the requested NamedOperation, updates the input and the view, then executes the operation chain, bypassing
     * the Graph.executes graph hooks.
     *
     * @param operation the {@link Operation} to be executed
     * @param context   the operation chain context, containing the user who executed the operation
     * @param store     the {@link Store} the operation should be run on
     * @return an object - whatever the last operation in the chain returns
     * @throws OperationException thrown when the operation fails
     */
@Override
public Object doOperation(final NamedOperation operation, final Context context, final Store store) throws OperationException {
    try {
        if (cache == null) {
            throw new OperationException("Cache should be initialised in " + "resources/NamedOperationsDeclarations.json and referenced in store.properties");
        }
        ExtendedNamedOperation namedOperation = cache.getNamedOperation(operation.getOperationName(), context.getUser());
        OperationChain<?> operationChain = namedOperation.getOperationChain();
        operationChain = new OperationChain<>(exposeNamedOperations(operationChain, context.getUser(), cache));
        updateOperationInput(operationChain.getOperations().get(0), operation.getSeeds());
        operationChain = updateView(operation.getView(), operationChain);
        return store._execute(operationChain, context);
    } catch (CacheOperationFailedException e) {
        throw new OperationException(e.getMessage(), e);
    } catch (ClassCastException e) {
        throw new OperationException("Input type " + operation.getInput().getClass().getName() + " was not valid for the operation", e);
    }
}
Also used : ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Example 3 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException in project Gaffer by gchq.

the class NamedOperationJCSCache method getFromCache.

/**
     * Checks the supplied name is not null, an Object exists with this name in the cache, that Object is not null and
     * is an instance of ExtendedNamedOperation. If all these conditions are met, the ExtendedNamedOperation is returned, if any are not
     * met then an exception is thrown.
     *
     * @param name the key stored in the cache
     * @return a ExtendedNamedOperation stored against the key
     * @throws CacheOperationFailedException thrown when the operation fails
     */
public ExtendedNamedOperation getFromCache(final String name) throws CacheOperationFailedException {
    if (name == null) {
        throw new CacheOperationFailedException("Operation name cannot be null");
    }
    ExtendedNamedOperation namedOperation;
    Object obj = getCache().getFromGroup(name, CACHE_GROUP);
    if (obj != null && obj.getClass().equals(ExtendedNamedOperation.class)) {
        namedOperation = (ExtendedNamedOperation) obj;
        return namedOperation;
    }
    throw new CacheOperationFailedException("No ExtendedNamedOperation with a name '" + name + "' could be found in the cache");
}
Also used : CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException) ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation)

Example 4 with CacheOperationFailedException

use of uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException in project Gaffer by gchq.

the class AddNamedOperationHandler method doOperation.

/**
     * Adds a NamedOperation to a cache which must be specified in the operation declarations file. An
     * ExtendedNamedOperation is built using the fields on the AddNamedOperation. The operation name and operation chain
     * fields must be set and cannot be left empty, or the build() method will fail and a runtime exception will be
     * thrown. The handler then adds/overwrites the NamedOperation according toa an overwrite flag.
     * @param operation the {@link uk.gov.gchq.gaffer.operation.Operation} to be executed
     * @param context   the operation chain context, containing the user who executed the operation
     * @param store     the {@link Store} the operation should be run on
     * @return null (since the output is void)
     * @throws OperationException if the operation on the cache fails
     */
@Override
public Void doOperation(final AddNamedOperation operation, final Context context, final Store store) throws OperationException {
    try {
        if (cache == null) {
            throw new OperationException("Cache should be initialised in " + "resources/NamedOperationsDeclarations.json and referenced in store.properties");
        }
        validate(context.getUser(), operation.getOperationName(), operation.getOperationChain(), cache);
        ExtendedNamedOperation extendedNamedOperation = new ExtendedNamedOperation.Builder().operationChain(operation.getOperationChain()).operationName(operation.getOperationName()).creatorId(context.getUser().getUserId()).readers(operation.getReadAccessRoles()).writers(operation.getWriteAccessRoles()).description(operation.getDescription()).build();
        cache.addNamedOperation(extendedNamedOperation, operation.isOverwriteFlag(), context.getUser());
    } catch (CacheOperationFailedException e) {
        throw new OperationException(e.getMessage(), e);
    }
    return null;
}
Also used : ExtendedNamedOperation(uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation) CacheOperationFailedException(uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException) OperationException(uk.gov.gchq.gaffer.operation.OperationException)

Aggregations

ExtendedNamedOperation (uk.gov.gchq.gaffer.named.operation.ExtendedNamedOperation)4 CacheOperationFailedException (uk.gov.gchq.gaffer.named.operation.cache.CacheOperationFailedException)4 OperationException (uk.gov.gchq.gaffer.operation.OperationException)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 WrappedCloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable)1 NamedOperation (uk.gov.gchq.gaffer.named.operation.NamedOperation)1