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.containers;
011    
012    import java.io.IOException;
013    import java.io.LineNumberReader;
014    import java.io.StringReader;
015    import java.util.List;
016    
017    import org.picocontainer.ComponentAdapter;
018    import org.picocontainer.DefaultPicoContainer;
019    import org.picocontainer.MutablePicoContainer;
020    import org.picocontainer.PicoCompositionException;
021    import org.picocontainer.PicoContainer;
022    
023    /**
024     * CommandLineArgumentsPicoContainer configured itself from array of strings
025     * which are most likely coming in as command line arguments
026     * 
027     */
028    @SuppressWarnings("serial")
029    public class CommandLinePicoContainer extends AbstractDelegatingPicoContainer {
030        public CommandLinePicoContainer(String separator, String[] arguments) {
031            this(separator,arguments,null);
032        }
033    
034        public CommandLinePicoContainer(String separator, String[] arguments, PicoContainer parent ) {
035            super(new DefaultPicoContainer(parent));
036            for (String argument : arguments) {
037                processArgument(argument, separator);
038            }
039        }
040        public CommandLinePicoContainer(String separator, StringReader argumentsProps) throws IOException {
041            this(separator, argumentsProps, new String[0]);
042        }
043        
044        public CommandLinePicoContainer(String separator, StringReader argumentProperties, String[] arguments) throws IOException{
045            this(separator,argumentProperties,arguments,null);
046        }
047    
048        public CommandLinePicoContainer(String separator, StringReader argumentProperties, String[] arguments, PicoContainer parent)
049            throws IOException {
050            super(new DefaultPicoContainer(parent));
051            
052            LineNumberReader lnr = new LineNumberReader(argumentProperties);
053            String line = lnr.readLine();
054            while (line != null) {
055                processArgument(line, separator);
056                line = lnr.readLine();
057            }
058            for (String argument : arguments) {
059                processArgument(argument, separator);
060            }
061        }
062        
063        public CommandLinePicoContainer(String[] arguments) {
064            this("=", arguments);
065        }
066    
067        public CommandLinePicoContainer(String[] arguments, PicoContainer parent) {
068            this("=", arguments,parent);
069        }
070    
071        private void addConfig(String key, Object val) {
072            if (getDelegate().getComponent(key) != null) {
073                getDelegate().removeComponent(key);
074            }
075            getDelegate().addConfig(key, val);
076        }
077    
078        public <T> T getComponent(Class<T> componentType) {
079            return null;
080        }
081    
082        public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType) {
083            return null;
084        }
085    
086        public PicoContainer getParent() {
087            return new EmptyPicoContainer();
088        }
089    
090        private void processArgument(String argument, String separator) {
091            String[] kvs = argument.split(separator);
092            if (kvs.length == 2) {
093                addConfig(kvs[0], kvs[1]);
094            } else if (kvs.length == 1) {
095                addConfig(kvs[0], "true");
096            } else if (kvs.length > 2) {
097                throw new PicoCompositionException(
098                    "Argument name'"+separator+"'value pair '" + argument + "' has too many '"+separator+"' characters");
099            }
100        }
101        
102        public MutablePicoContainer getDelegate() {
103            return (MutablePicoContainer) super.getDelegate();
104        }
105    
106        public void setName(String s) {
107            ((DefaultPicoContainer)getDelegate()).setName(s);
108        }
109    
110    }