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

org.nanocontainer.aop.dynaop.ContainerSuppliedMixinFactory 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.MixinFactory;
import dynaop.Proxy;
import org.picocontainer.ComponentAdapter;
import org.picocontainer.PicoContainer;
import org.picocontainer.defaults.ConstructorInjectionComponentAdapter;

import java.util.Properties;

/**
 * Manufactures mixins from a PicoContainer. Useful when a mixin
 * has dependencies on other components in the PicoContainer.
 *
 * @author Stephen Molitor
 * @version $Revision: 3144 $
 */
class ContainerSuppliedMixinFactory implements MixinFactory {

    private final PicoContainer pico;
    private final Class mixinClass;

    /**
     * Creates a new ContainerSuppliedMixinFactory that will
     * manufacture mixins by retrieving them from the PicoContainer
     * using a given component key.
     *
     * @param pico              the PicoContainer to retrieve the mixin from.
     * @param mixinComponentKey the component key that will be used to retrieve
     *                          the mixin object from the pico container.
     */
    ContainerSuppliedMixinFactory(PicoContainer pico, Class mixinClass) {
        this.pico = pico;
        this.mixinClass = mixinClass;
    }

    /**
     * Manufactures a Mixin by retrieving it from the
     * PicoContainer.
     *
     * @param proxy the proxy that the interceptor will wrap.
     * @return the Mixin object.
     * @throws NullPointerException if the mixin can not be found in the pico
     *                              container.
     */
    public Object create(Proxy proxy) throws NullPointerException {
        Object mixin = pico.getComponentInstanceOfType(mixinClass);
        if (mixin == null) {
            ComponentAdapter adapter = new ConstructorInjectionComponentAdapter(mixinClass, mixinClass);
            mixin = adapter.getComponentInstance(pico);
        }
        return mixin;
    }

    /**
     * Gets properties. Useful for debugging.
     *
     * @return an empty Properties object.
     */
    public Properties getProperties() {
        Properties properties = new Properties();
        properties.setProperty("advice", "mixin");
        return properties;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy