001    /*****************************************************************************
002     * Copyright (c) PicoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
009     *****************************************************************************/
010    
011    package org.picocontainer.injectors;
012    
013    import org.picocontainer.Characteristics;
014    import org.picocontainer.ComponentAdapter;
015    import org.picocontainer.ComponentMonitor;
016    import org.picocontainer.LifecycleStrategy;
017    import org.picocontainer.Parameter;
018    import org.picocontainer.PicoCompositionException;
019    import org.picocontainer.behaviors.AbstractBehaviorFactory;
020    
021    import java.util.Properties;
022    
023    
024    /**
025     * A {@link org.picocontainer.InjectionFactory} for JavaBeans.
026     * The factory creates {@link SetterInjector}.
027     *
028     * @author Jörg Schaible
029     */
030    @SuppressWarnings("serial")
031    public class SetterInjection extends AbstractInjectionFactory {
032    
033        private final String prefix;
034        private String notThisOneThough;
035        private boolean optional;
036    
037        public SetterInjection(String prefix) {
038            this.prefix = prefix;
039        }
040    
041        public SetterInjection() {
042            this("set");
043        }
044    
045        /**
046         * Specify a prefix and an exclusion
047         * @param prefix the prefix like 'set'
048         * @param notThisOneThough to exclude, like 'setMetaClass' for Groovy
049         */
050        public SetterInjection(String prefix, String notThisOneThough) {
051            this(prefix);
052            this.notThisOneThough = notThisOneThough;
053        }
054    
055        /**
056         * Create a {@link SetterInjector}.
057         * 
058         * @param monitor
059         * @param lifecycleStrategy
060         * @param componentProperties
061         * @param componentKey The component's key
062         * @param componentImplementation The class of the bean.
063         * @param parameters Any parameters for the setters. If null the adapter
064         *            solves the dependencies for all setters internally. Otherwise
065         *            the number parameters must match the number of the setter.
066         * @return Returns a new {@link SetterInjector}.
067         * @throws PicoCompositionException if dependencies cannot be solved
068         */
069        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class<T> componentImplementation, Parameter... parameters)
070                throws PicoCompositionException {
071            boolean useNames = AbstractBehaviorFactory.arePropertiesPresent(componentProperties, Characteristics.USE_NAMES, true);
072            return wrapLifeCycle(monitor.newInjector(new SetterInjector(componentKey, componentImplementation, parameters, monitor, prefix, notThisOneThough != null ? notThisOneThough : "", optional, useNames)), lifecycleStrategy);
073        }
074    
075        public SetterInjection withInjectionOptional() {
076            optional = true;
077            return this;
078        }
079    }