All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.social.srp.SocialResourcePrefetch Maven / Gradle / Ivy

/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2012 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.cq.social.srp;

import java.lang.reflect.InvocationTargetException;
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.concurrent.Callable;

import com.adobe.cq.social.srp.SocialResourceProvider.PrefetchPerResult;

/**
 * Manages prefetching of additional resources and information for fetched Resources.
 */
public class SocialResourcePrefetch {

    private static ThreadLocal> stackThreadLocal =
        new ThreadLocal>();

    private static Deque getStack() {
        Deque stack = stackThreadLocal.get();
        if (stack == null) {
            stack = new LinkedList();
            stackThreadLocal.set(stack);
        }
        return stack;
    }

    /**
     * Add a PrefetchPerChild specification to the thread's prefetch stack and invoke code which will use that
     * prefetch specification implictly. Code will continue to use any previously specified prefetch specifications as
     * well.
     * @param call The code to run with the specified prefetching
     * @param prefetch the specification of what to prefetch.
     * @param  Type
     * @return the caller's choice of return type.
     * @throws InvocationTargetException if the called code throws any exception.
     */
    public static  T call(final PrefetchPerResult prefetch, final Callable call)
        throws InvocationTargetException {
        boolean pushed = false;

        final Deque stack = getStack();
        try {
            stack.push(prefetch);
            pushed = true;
            return call.call();
        } catch (final Exception e) {
            throw new InvocationTargetException(e);
        } finally {
            if (pushed) {
                stack.pop();
            }
            // Don't leave any pollution on the thread.
            if (stack.size() == 0) {
                stackThreadLocal.remove();
            }
        }
    }

    /**
     * Add a PrefetchPerChild specification to the thread's prefetch stack and invoke code which will use that
     * prefetch specification implictly. Code will continue to use any previously specified prefetch specifications as
     * well.
     * @param prefetch the specification of what to prefetch.
     * @param runnable The code to run with the specified prefetching
     * @throws InvocationTargetException if the called code throws any exception.
     */
    public static void call(final PrefetchPerResult prefetch, final Runnable runnable)
        throws InvocationTargetException {
        call(prefetch, new Callable() {

            @Override
            public Void call() throws Exception {
                runnable.run();
                return Void.TYPE.newInstance();
            }

        });
    }

    /**
     * Get all the prefetch specifications (PrefetchPerChild) in effect.
     * @return an Iterator which will return the prefetch specifications in order from most recently to least recently
     *         applied.
     */
    public static Iterator getPrefetches() {
        return getStack().iterator();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy