// Path: chili/src/main/java/me/defying/chili/module/ChiliException.java
// public class ChiliException extends Exception {
// 
//     /**
//      * Creates a new instance of <code>ChiliException</code> without detail
//      * message.
//      */
//     public ChiliException() {
//         // empty
//     }
// 
//     /**
//      * Constructs an instance of <code>ChiliException</code> with the
//      * specified detail message.
//      *
//      * @param message the detail message.
//      */
//     public ChiliException(final String message) {
//         super(message);
//     }
// 
//     /**
//      * Constructs an instance of <code>ChiliException</code> with the
//      * specified cause.
//      *
//      * @param cause the cause.
//      */
//     public ChiliException(final Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: chili/src/main/java/me/defying/chili/util/InvocationUtils.java
// public final class InvocationUtils {
// 
//     /**
//      * Prevent instantiation of utility class.
//      */
//     private InvocationUtils() {
//         // emtpy
//     }
// 
//     /**
//      * Returns the annotation for the specified annotation type.
//      *
//      * @param <T> the annotation type
//      * @param invocation the method invocation
//      * @param annotation the type of annotation
//      * @return the annotation for the specified annotation type
//      */
//     public static <T extends Annotation> T getAnnotation(final MethodInvocation invocation, final Class<T> annotation) {
//         return invocation.getMethod().getAnnotation(annotation);
//     }
// 
//     /**
//      * Returns the class annotation for the specified annotation type.
//      *
//      * @param <T> the annotation type
//      * @param invocation the method invocation
//      * @param annotation the type of annotation
//      * @return the class annotation for the specified annotation type
//      */
//     public static <T extends Annotation> T getClassAnnotation(final MethodInvocation invocation, final Class<T> annotation) {
//         return invocation.getThis().getClass().getSuperclass().getAnnotation(annotation);
//     }
// 
//     /**
//      * Returns the class being invoked.
//      *
//      * @param invocation the method invocation
//      * @return the class being invoked
//      */
//     public static Class getClass(final MethodInvocation invocation) {
//         return invocation.getThis().getClass().getSuperclass();
//     }
// 
//     /**
//      * Returns the method being invoked.
//      *
//      * @param invocation the method invocation
//      * @return the method being invoked
//      */
//     public static Method getMethod(final MethodInvocation invocation) {
//         return invocation.getMethod();
//     }
// 
//     /**
//      * Returns the arguments of the method invocation.
//      *
//      * @param invocation the method invocation
//      * @return the arguments of the method invocation
//      */
//     public static List<Object> getArguments(final MethodInvocation invocation) {
//         // get method arguments
//         final List<Object> arguments = new ArrayList(Arrays.asList(invocation.getArguments()));
// 
//         // if the method contains a varargs then the last parameter must be flattened
//         if (invocation.getMethod().isVarArgs() && !arguments.isEmpty()) {
//             final int last = arguments.size() - 1;
//             final Object varargs = arguments.remove(last);
//             arguments.addAll(convert(varargs));
//         }
// 
//         return arguments;
//     }
// 
//     /**
//      * Converts an {@code Object} array into a {@code List}.
//      *
//      * @param array the array of objects
//      * @return the list of objects
//      */
//     private static List<Object> convert(final Object array) {
//         final List<Object> result = new ArrayList<>();
//         final int length = Array.getLength(array);
// 
//         for (int i = 0; i < length; ++i) {
//             result.add(Array.get(array, i));
//         }
// 
//         return result;
//     }
// }


import me.defying.chili.Timeout;
import me.defying.chili.module.ChiliException;
import me.defying.chili.util.InvocationUtils;
import java.util.concurrent.Callable;
import com.google.common.util.concurrent.SimpleTimeLimiter;
import com.google.common.util.concurrent.TimeLimiter;
import com.google.common.util.concurrent.UncheckedTimeoutException;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/*
 * The MIT License (MIT)
 * Copyright (c) 2015 Rafael Marmelo
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package me.defying.chili.timeout;




/**
 * Guice interceptor for {@code Timeout} annotation.
 *
 * @author Rafael Marmelo
 * @since 1.1
 */
public class TimeoutInterceptor implements MethodInterceptor {

    /**
     * Constructs an instance of <code>TimeoutInterceptor</code>.
     */
    public TimeoutInterceptor() {
        // empty
    }

    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        // get annotation, class, method and arguments
        final Timeout annotation = InvocationUtils.getAnnotation(invocation, Timeout.class);

        // do nothing
        if (annotation.time() <= 0) {
            return invocation.proceed();
        }

// Path: chili/src/main/java/me/defying/chili/module/ChiliException.java
// public class ChiliException extends Exception {
// 
//     /**
//      * Creates a new instance of <code>ChiliException</code> without detail
//      * message.
//      */
//     public ChiliException() {
//         // empty
//     }
// 
//     /**
//      * Constructs an instance of <code>ChiliException</code> with the
//      * specified detail message.
//      *
//      * @param message the detail message.
//      */
//     public ChiliException(final String message) {
//         super(message);
//     }
// 
//     /**
//      * Constructs an instance of <code>ChiliException</code> with the
//      * specified cause.
//      *
//      * @param cause the cause.
//      */
//     public ChiliException(final Throwable cause) {
//         super(cause);
//     }
// }
// 
// Path: chili/src/main/java/me/defying/chili/util/InvocationUtils.java
// public final class InvocationUtils {
// 
//     /**
//      * Prevent instantiation of utility class.
//      */
//     private InvocationUtils() {
//         // emtpy
//     }
// 
//     /**
//      * Returns the annotation for the specified annotation type.
//      *
//      * @param <T> the annotation type
//      * @param invocation the method invocation
//      * @param annotation the type of annotation
//      * @return the annotation for the specified annotation type
//      */
//     public static <T extends Annotation> T getAnnotation(final MethodInvocation invocation, final Class<T> annotation) {
//         return invocation.getMethod().getAnnotation(annotation);
//     }
// 
//     /**
//      * Returns the class annotation for the specified annotation type.
//      *
//      * @param <T> the annotation type
//      * @param invocation the method invocation
//      * @param annotation the type of annotation
//      * @return the class annotation for the specified annotation type
//      */
//     public static <T extends Annotation> T getClassAnnotation(final MethodInvocation invocation, final Class<T> annotation) {
//         return invocation.getThis().getClass().getSuperclass().getAnnotation(annotation);
//     }
// 
//     /**
//      * Returns the class being invoked.
//      *
//      * @param invocation the method invocation
//      * @return the class being invoked
//      */
//     public static Class getClass(final MethodInvocation invocation) {
//         return invocation.getThis().getClass().getSuperclass();
//     }
// 
//     /**
//      * Returns the method being invoked.
//      *
//      * @param invocation the method invocation
//      * @return the method being invoked
//      */
//     public static Method getMethod(final MethodInvocation invocation) {
//         return invocation.getMethod();
//     }
// 
//     /**
//      * Returns the arguments of the method invocation.
//      *
//      * @param invocation the method invocation
//      * @return the arguments of the method invocation
//      */
//     public static List<Object> getArguments(final MethodInvocation invocation) {
//         // get method arguments
//         final List<Object> arguments = new ArrayList(Arrays.asList(invocation.getArguments()));
// 
//         // if the method contains a varargs then the last parameter must be flattened
//         if (invocation.getMethod().isVarArgs() && !arguments.isEmpty()) {
//             final int last = arguments.size() - 1;
//             final Object varargs = arguments.remove(last);
//             arguments.addAll(convert(varargs));
//         }
// 
//         return arguments;
//     }
// 
//     /**
//      * Converts an {@code Object} array into a {@code List}.
//      *
//      * @param array the array of objects
//      * @return the list of objects
//      */
//     private static List<Object> convert(final Object array) {
//         final List<Object> result = new ArrayList<>();
//         final int length = Array.getLength(array);
// 
//         for (int i = 0; i < length; ++i) {
//             result.add(Array.get(array, i));
//         }
// 
//         return result;
//     }
// }


// Path: chili/src/main/java/me/defying/chili/timeout/TimeoutInterceptor.java
import me.defying.chili.Timeout;
import me.defying.chili.module.ChiliException;
import me.defying.chili.util.InvocationUtils;
import java.util.concurrent.Callable;
import com.google.common.util.concurrent.SimpleTimeLimiter;
import com.google.common.util.concurrent.TimeLimiter;
import com.google.common.util.concurrent.UncheckedTimeoutException;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/*
 * The MIT License (MIT)
 * Copyright (c) 2015 Rafael Marmelo
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package me.defying.chili.timeout;




/**
 * Guice interceptor for {@code Timeout} annotation.
 *
 * @author Rafael Marmelo
 * @since 1.1
 */
public class TimeoutInterceptor implements MethodInterceptor {

    /**
     * Constructs an instance of <code>TimeoutInterceptor</code>.
     */
    public TimeoutInterceptor() {
        // empty
    }

    @Override
    public Object invoke(final MethodInvocation invocation) throws Throwable {
        // get annotation, class, method and arguments
        final Timeout annotation = InvocationUtils.getAnnotation(invocation, Timeout.class);

        // do nothing
        if (annotation.time() <= 0) {
            return invocation.proceed();
        }

        final TimeLimiter limiter = new SimpleTimeLimiter();