001/*
002 * Copyright 2002-2016 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.cache.interceptor;
018
019/**
020 * Abstract the invocation of a cache operation.
021 *
022 * <p>Does not provide a way to transmit checked exceptions but
023 * provide a special exception that should be used to wrap any
024 * exception that was thrown by the underlying invocation.
025 * Callers are expected to handle this issue type specifically.
026 *
027 * @author Stephane Nicoll
028 * @since 4.1
029 */
030@FunctionalInterface
031public interface CacheOperationInvoker {
032
033        /**
034         * Invoke the cache operation defined by this instance. Wraps any exception
035         * that is thrown during the invocation in a {@link ThrowableWrapper}.
036         * @return the result of the operation
037         * @throws ThrowableWrapper if an error occurred while invoking the operation
038         */
039        Object invoke() throws ThrowableWrapper;
040
041
042        /**
043         * Wrap any exception thrown while invoking {@link #invoke()}.
044         */
045        @SuppressWarnings("serial")
046        class ThrowableWrapper extends RuntimeException {
047
048                private final Throwable original;
049
050                public ThrowableWrapper(Throwable original) {
051                        super(original.getMessage(), original);
052                        this.original = original;
053                }
054
055                public Throwable getOriginal() {
056                        return this.original;
057                }
058        }
059
060}