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

org.nanocontainer.aop.dynaop.ContainerSuppliedInterceptorFactory Maven / Gradle / Ivy

/*****************************************************************************
 * Copyright (c) PicoContainer Organization. All rights reserved.            *
 * ------------------------------------------------------------------------- *
 * The software in this package is published under the terms of the BSD      *
 * style license a copy of which has been included with this distribution in *
 * the LICENSE.txt file.                                                     *
 *                                                                           *
 * Idea by Rachel Davies, Original code by various                           *
 *****************************************************************************/
package org.nanocontainer.aop.dynaop;

import dynaop.Interceptor;
import dynaop.InterceptorFactory;
import dynaop.Proxy;
import org.aopalliance.intercept.MethodInterceptor;
import org.picocontainer.PicoContainer;

import java.util.Properties;

/**
 * Manufactures interceptors from a PicoContainer. Useful when
 * an interceptor has dependencies on other components in the
 * PicoContainer.
 *
 * @author Stephen Molitor
 * @version $Revision: 3144 $
 */
class ContainerSuppliedInterceptorFactory implements InterceptorFactory {

    private final PicoContainer pico;
    private final Object interceptorComponentKey;

    /**
     * Creates a new ContainerSuppliedInterceptorFactory that
     * will manufacture interceptors by retrieving them from the
     * PicoContainer using a given component key.
     *
     * @param pico                    the PicoContainer to retrieve the interceptor
     *                                from.
     * @param interceptorComponentKey the component key that will be used to
     *                                retrieve the interceptor from the pico container.
     */
    ContainerSuppliedInterceptorFactory(PicoContainer pico, Object interceptorComponentKey) {
        this.pico = pico;
        this.interceptorComponentKey = interceptorComponentKey;
    }

    /**
     * Manufactures an Interceptor by retrieving it from the
     * PicoContainer.
     *
     * @param proxy not used.
     * @return the Interceptor object.
     * @throws NullPointerException if the interceptor can not be found in the
     *                              pico container.
     */
    public Interceptor create(Proxy proxy) throws NullPointerException {
        MethodInterceptor methodInterceptor = (MethodInterceptor) pico.getComponentInstance(interceptorComponentKey);
        if (methodInterceptor == null) {
            throw new NullPointerException("Interceptor with component key " + interceptorComponentKey
                    + " + not found in PicoContainer");
        }
        return new MethodInterceptorAdapter(methodInterceptor);
    }

    /**
     * Gets properties. Useful for debugging.
     *
     * @return a Properties object.
     */
    public Properties getProperties() {
        Properties properties = new Properties();
        properties.setProperty("advice", "method interceptor");
        properties.setProperty("scope", "per-instance");
        return properties;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy