Search in sources :

Example 1 with Interceptor

use of org.exoplatform.container.spi.Interceptor in project kernel by exoplatform.

the class DefaultInterceptorChainFactory method getDynamicInterceptors.

/**
 * Gives the dynamic {@link Interceptor} from the last to the head
 */
protected List<Interceptor> getDynamicInterceptors(ExoContainer holder, ExoContainer parent) {
    List<Interceptor> list = new ArrayList<Interceptor>();
    ServiceLoader<Interceptor> loader = ServiceLoader.load(Interceptor.class);
    for (Iterator<Interceptor> it = loader.iterator(); it.hasNext(); ) {
        Interceptor interceptor = it.next();
        interceptor.setHolder(holder);
        interceptor.setParent(parent);
        list.add(interceptor);
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) Interceptor(org.exoplatform.container.spi.Interceptor)

Example 2 with Interceptor

use of org.exoplatform.container.spi.Interceptor in project kernel by exoplatform.

the class DefaultInterceptorChainFactory method getStaticInterceptors.

/**
 * Gives the static {@link Interceptor} from the last to the head
 */
protected List<Interceptor> getStaticInterceptors(ExoContainer holder, ExoContainer parent) {
    List<Interceptor> list = new ArrayList<Interceptor>(4);
    list.add(new ConcurrentContainer(holder, parent));
    list.add(new CachingContainer());
    list.add(new ManageableContainer(holder, parent));
    return list;
}
Also used : ManageableContainer(org.exoplatform.container.management.ManageableContainer) ArrayList(java.util.ArrayList) Interceptor(org.exoplatform.container.spi.Interceptor)

Example 3 with Interceptor

use of org.exoplatform.container.spi.Interceptor in project kernel by exoplatform.

the class DefaultInterceptorChainFactory method resolveNext.

/**
 * Resolves the next dynamic {@link Interceptor} using the annotation {@link Before} and {@link After}
 * @param alreadyResolved the list of {@link Interceptor} already resolved
 * @param iter the Iterator containing the remaining dynamic {@link Interceptor} to resolve
 * @param resolveIfAbsent indicates if the interceptor must be resolved if the referred interceptor could not
 * be found
 */
protected void resolveNext(List<Interceptor> alreadyResolved, Iterator<Interceptor> iter, boolean resolveIfAbsent) {
    Interceptor it = iter.next();
    Before b = it.getClass().getAnnotation(Before.class);
    if (b != null) {
        // An annotation Before has been defined
        String id = b.value();
        if (id == null || (id = id.trim()).isEmpty()) {
            // No id set
            if (PropertyManager.isDevelopping()) {
                // NOSONAR
                System.out.println("WARN: No value set for the annotation Before of the interceptor " + it.getClass());
            }
            alreadyResolved.add(it);
            iter.remove();
            return;
        }
        // The id has been set
        for (int i = 0, length = alreadyResolved.size(); i < length; i++) {
            Interceptor interceptor = alreadyResolved.get(i);
            if (id.equals(interceptor.getId())) {
                // The id has been found
                if (i < length - 1) {
                    alreadyResolved.add(i + 1, it);
                } else {
                    alreadyResolved.add(it);
                }
                iter.remove();
                return;
            }
        }
        if (resolveIfAbsent) {
            if (PropertyManager.isDevelopping()) {
                System.out.println(// NOSONAR
                "WARN: Could not find the interceptor of " + id + " required by the interceptor " + it.getClass());
            }
            alreadyResolved.add(it);
            iter.remove();
        }
        return;
    }
    After a = it.getClass().getAnnotation(After.class);
    if (a != null) {
        // An annotation After has been defined
        String id = a.value();
        if (id == null || (id = id.trim()).isEmpty()) {
            // No id set
            if (PropertyManager.isDevelopping()) {
                // NOSONAR
                System.out.println("WARN: No value set for the annotation After of the interceptor " + it.getClass());
            }
            alreadyResolved.add(it);
            iter.remove();
            return;
        }
        // The id has been set
        for (int i = 0, length = alreadyResolved.size(); i < length; i++) {
            Interceptor interceptor = alreadyResolved.get(i);
            if (id.equals(interceptor.getId())) {
                // The id has been found
                alreadyResolved.add(i, it);
                iter.remove();
                return;
            }
        }
        if (resolveIfAbsent) {
            if (PropertyManager.isDevelopping()) {
                System.out.println(// NOSONAR
                "WARN: Could not find the interceptor of " + id + " required by the interceptor " + it.getClass());
            }
            alreadyResolved.add(it);
            iter.remove();
        }
        return;
    }
    // No annotation has been defined
    alreadyResolved.add(it);
    iter.remove();
}
Also used : Before(org.exoplatform.container.spi.Before) After(org.exoplatform.container.spi.After) Interceptor(org.exoplatform.container.spi.Interceptor)

Example 4 with Interceptor

use of org.exoplatform.container.spi.Interceptor in project kernel by exoplatform.

the class DefaultInterceptorChainFactory method getInterceptorChain.

/**
 * {@inheritDoc}
 */
public Interceptor getInterceptorChain(ExoContainer holder, ExoContainer parent) {
    if (chain == null) {
        synchronized (this) {
            if (chain == null) {
                List<Interceptor> staticInts = getStaticInterceptors(holder, parent);
                List<Interceptor> dynamicInts = getDynamicInterceptors(holder, parent);
                List<Interceptor> interceptors = resolve(staticInts, dynamicInts);
                Interceptor result = null;
                List<Class<? extends Interceptor>> chain = new LinkedList<Class<? extends Interceptor>>();
                StringBuilder sb = null;
                boolean isDevelopping = PropertyManager.isDevelopping();
                if (isDevelopping) {
                    sb = new StringBuilder();
                }
                for (int i = 0, length = interceptors.size(); i < length; i++) {
                    Interceptor it = interceptors.get(i);
                    it.setSuccessor(result);
                    chain.add(it.getClass());
                    if (isDevelopping) {
                        sb.insert(0, "-> " + it.getClass().getName() + " ");
                    }
                    result = it;
                }
                if (isDevelopping) {
                    // NOSONAR
                    System.out.println("The interceptor chain used is " + sb);
                }
                this.chain = chain;
                return result;
            }
        }
    }
    Interceptor result = null;
    for (Iterator<Class<? extends Interceptor>> iter = chain.iterator(); iter.hasNext(); ) {
        Class<? extends Interceptor> iClass = iter.next();
        try {
            Interceptor it = iClass.cast(iClass.newInstance());
            it.setHolder(holder);
            it.setParent(parent);
            it.setSuccessor(result);
            result = it;
        } catch (Exception e) {
            System.out.println(// NOSONAR
            "ERROR: Cannot instantiate inteceptor of class " + iClass + ": " + e.getLocalizedMessage());
        }
    }
    return result;
}
Also used : Interceptor(org.exoplatform.container.spi.Interceptor) LinkedList(java.util.LinkedList)

Example 5 with Interceptor

use of org.exoplatform.container.spi.Interceptor in project kernel by exoplatform.

the class MTInterceptorChainFactory method getStaticInterceptors.

/**
 * {@inheritDoc}
 */
protected List<Interceptor> getStaticInterceptors(ExoContainer holder, ExoContainer parent) {
    List<Interceptor> list = new ArrayList<Interceptor>(4);
    list.add(new ConcurrentContainerMT(holder, parent));
    list.add(new CachingContainerMT());
    list.add(new ManageableContainer(holder, parent));
    return list;
}
Also used : ManageableContainer(org.exoplatform.container.management.ManageableContainer) ArrayList(java.util.ArrayList) Interceptor(org.exoplatform.container.spi.Interceptor)

Aggregations

Interceptor (org.exoplatform.container.spi.Interceptor)5 ArrayList (java.util.ArrayList)3 ManageableContainer (org.exoplatform.container.management.ManageableContainer)2 LinkedList (java.util.LinkedList)1 After (org.exoplatform.container.spi.After)1 Before (org.exoplatform.container.spi.Before)1