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     * Original code by                                                          *
009     *****************************************************************************/
010    package org.picocontainer.injectors;
011    
012    import org.picocontainer.ComponentAdapter;
013    import org.picocontainer.ComponentMonitor;
014    import org.picocontainer.LifecycleStrategy;
015    import org.picocontainer.Parameter;
016    import org.picocontainer.PicoCompositionException;
017    import static org.picocontainer.Characteristics.immutable;
018    
019    import java.util.Properties;
020    
021    /**
022     * A {@link org.picocontainer.InjectionFactory} for named fields.
023     *
024     * Use like so: pico.as(injectionFieldNames("field1", "field2")).addComponent(...)
025     *
026     * The factory creates {@link org.picocontainer.injectors.NamedFieldInjector}.
027     *
028     * @author Paul Hammant
029     */
030    @SuppressWarnings("serial")
031    public class NamedFieldInjection extends AbstractInjectionFactory {
032    
033    
034        private static final String INJECTION_FIELD_NAMES = "injectionFieldNames";
035    
036        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor monitor,
037                                                       LifecycleStrategy lifecycleStrategy,
038                                                       Properties componentProperties,
039                                                       Object componentKey,
040                                                       Class<T> componentImplementation,
041                                                       Parameter... parameters) throws PicoCompositionException {
042            String fieldNames = (String) componentProperties.remove(INJECTION_FIELD_NAMES);
043            if (fieldNames == null) {
044                fieldNames = "";
045            }
046            return wrapLifeCycle(monitor.newInjector(new NamedFieldInjector(componentKey, componentImplementation, parameters, monitor,
047                    fieldNames)), lifecycleStrategy);
048        }
049    
050        public static Properties injectionFieldNames(String... fieldNames) {
051            StringBuilder sb = new StringBuilder();
052            for (int i = 0; i < fieldNames.length; i++) {
053                sb.append(" ").append(fieldNames[i]);
054            }
055            Properties retVal = new Properties();
056            return immutable(INJECTION_FIELD_NAMES, sb.toString().trim());
057        }
058    
059    }