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.behaviors;
011    
012    import org.picocontainer.ComponentAdapter;
013    import org.picocontainer.Parameter;
014    import org.picocontainer.PicoCompositionException;
015    import org.picocontainer.ComponentMonitor;
016    import org.picocontainer.ComponentFactory;
017    import org.picocontainer.LifecycleStrategy;
018    import org.picocontainer.BehaviorFactory;
019    import org.picocontainer.injectors.AdaptiveInjection;
020    
021    import java.io.Serializable;
022    import java.util.Properties;
023    import java.util.Enumeration;
024    
025    public class AbstractBehaviorFactory implements ComponentFactory, Serializable, BehaviorFactory {
026    
027        private ComponentFactory delegate;
028    
029        public ComponentFactory wrap(ComponentFactory delegate) {
030            this.delegate = delegate;
031            return this;
032        }
033        
034        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor,
035                LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey,
036                Class<T> componentImplementation, Parameter... parameters) throws PicoCompositionException {
037            if (delegate == null) {
038                delegate = new AdaptiveInjection();
039            }
040            return delegate.createComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, componentKey,
041                    componentImplementation, parameters);
042        }
043    
044    
045        public <T> ComponentAdapter<T> addComponentAdapter(ComponentMonitor componentMonitor,
046                                                    LifecycleStrategy lifecycleStrategy,
047                                                    Properties componentProperties,
048                                                    ComponentAdapter<T> adapter) {        
049            if (delegate != null && delegate instanceof BehaviorFactory) {
050                return ((BehaviorFactory) delegate).addComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, adapter);
051            }
052            return adapter;
053        }
054    
055        public static boolean removePropertiesIfPresent(Properties current, Properties present) {
056            Enumeration<?> keys = present.keys();
057            while (keys.hasMoreElements()) {
058                String key = (String)keys.nextElement();
059                String presentValue = present.getProperty(key);
060                String currentValue = current.getProperty(key);
061                if (currentValue == null) {
062                    return false;
063                }
064                if (!presentValue.equals(currentValue)) {
065                    return false;
066                }
067            }
068            keys = present.keys();
069            while (keys.hasMoreElements()) {
070                Object key = keys.nextElement();
071                current.remove(key);
072            }
073            return true;
074        }
075    }