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