001    /*******************************************************************************
002     * Copyright (C) PicoContainer Organization. All rights reserved. *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD * style
005     * license a copy of which has been included with this distribution in * the
006     * LICENSE.txt file. * * Original code by *
007     ******************************************************************************/
008    package org.picocontainer.behaviors;
009    
010    import java.io.Serializable;
011    import java.util.Enumeration;
012    import java.util.Properties;
013    
014    import org.picocontainer.BehaviorFactory;
015    import org.picocontainer.ComponentAdapter;
016    import org.picocontainer.ComponentFactory;
017    import org.picocontainer.ComponentMonitor;
018    import org.picocontainer.LifecycleStrategy;
019    import org.picocontainer.Parameter;
020    import org.picocontainer.PicoCompositionException;
021    import org.picocontainer.PicoContainer;
022    import org.picocontainer.PicoVisitor;
023    import org.picocontainer.injectors.AdaptingInjection;
024    
025    @SuppressWarnings("serial")
026    public class AbstractBehaviorFactory implements ComponentFactory, Serializable, BehaviorFactory {
027    
028        private ComponentFactory delegate;
029    
030    
031        public ComponentFactory wrap(ComponentFactory delegate) {
032            this.delegate = delegate;
033            return this;
034        }
035    
036        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor,
037                LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey,
038                Class<T> componentImplementation, Parameter... parameters) throws PicoCompositionException {
039            if (delegate == null) {
040                delegate = new AdaptingInjection();
041            }
042            return delegate.createComponentAdapter(componentMonitor, lifecycleStrategy, componentProperties, componentKey,
043                    componentImplementation, parameters);
044        }
045    
046        public void verify(PicoContainer container) {
047            delegate.verify(container);
048        }
049    
050        public void accept(PicoVisitor visitor) {
051            visitor.visitComponentFactory(this);
052            if (delegate != null) {
053                delegate.accept(visitor);
054            }
055        }
056    
057    
058        public <T> ComponentAdapter<T> addComponentAdapter(ComponentMonitor componentMonitor,
059                LifecycleStrategy lifecycleStrategy, Properties componentProperties, ComponentAdapter<T> adapter) {
060            if (delegate != null && delegate instanceof BehaviorFactory) {
061                return ((BehaviorFactory) delegate).addComponentAdapter(componentMonitor, lifecycleStrategy,
062                        componentProperties, adapter);
063            }
064            return adapter;
065        }
066    
067        public static boolean arePropertiesPresent(Properties current, Properties present) {
068            Enumeration<?> keys = present.keys();
069            while (keys.hasMoreElements()) {
070                String key = (String) keys.nextElement();
071                String presentValue = present.getProperty(key);
072                String currentValue = current.getProperty(key);
073                if (currentValue == null) {
074                    return false;
075                }
076                if (!presentValue.equals(currentValue)) {
077                    return false;
078                }
079            }
080            return true;
081        }
082    
083        public static boolean removePropertiesIfPresent(Properties current, Properties present) {
084            if (!arePropertiesPresent(current, present)) {
085                return false;
086            }
087            Enumeration<?> keys = present.keys();
088            while (keys.hasMoreElements()) {
089                Object key = keys.nextElement();
090                current.remove(key);
091            }
092            return true;
093        }
094    
095        protected void mergeProperties(Properties into, Properties from) {
096            Enumeration<?> e = from.propertyNames();
097            while (e.hasMoreElements()) {
098                String s = (String) e.nextElement();
099                into.setProperty(s, from.getProperty(s));
100            }
101    
102        }
103    
104    }