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
013 import org.picocontainer.PicoContainer;
014 import org.picocontainer.ComponentAdapter;
015 import org.picocontainer.PicoVisitor;
016 import org.picocontainer.PicoCompositionException;
017 import org.picocontainer.DefaultPicoContainer;
018 import org.picocontainer.MutablePicoContainer;
019 import org.picocontainer.ParameterName;
020
021 import java.io.Serializable;
022 import java.io.StringReader;
023 import java.io.LineNumberReader;
024 import java.io.IOException;
025 import java.util.List;
026 import java.util.Collection;
027
028 public class ArgumentativePicoContainer implements PicoContainer, Serializable {
029
030 private final MutablePicoContainer delegate = new DefaultPicoContainer();
031
032 public ArgumentativePicoContainer(String[] arguments) {
033 this("=", arguments);
034 }
035
036 public ArgumentativePicoContainer(String separator, String[] arguments) {
037 for (String argument : arguments) {
038 processArgument(argument, separator);
039 }
040 }
041
042 public ArgumentativePicoContainer(String separator, StringReader argumentProperties, String[] arguments)
043 throws IOException {
044 LineNumberReader lnr = new LineNumberReader(argumentProperties);
045 String line = lnr.readLine();
046 while (line != null) {
047 processArgument(line, separator);
048 line = lnr.readLine();
049 }
050 for (String argument : arguments) {
051 processArgument(argument, separator);
052 }
053 }
054
055 private void processArgument(String argument, String separator) {
056 String[] kvs = argument.split(separator);
057 if (kvs.length == 2) {
058 addConfig(kvs[0], getValue(kvs[1]));
059 } else if (kvs.length == 1) {
060 addConfig(kvs[0], true);
061 } else if (kvs.length > 2) {
062 throw new PicoCompositionException(
063 "Argument name=value pair '" + argument + "' has too many '=' characters");
064 }
065 }
066
067 private void addConfig(String key, Object val) {
068 if (delegate.getComponent(key) != null) {
069 delegate.removeComponent(key);
070 }
071 delegate.addConfig(key, val);
072 }
073
074 public ArgumentativePicoContainer(String separator, StringReader argumentsProps) throws IOException {
075 this(separator, argumentsProps, new String[0]);
076 }
077
078 private Object getValue(String s) {
079 if (s.equals("true")) {
080 return true;
081 } else if (s.equals("false")) {
082 return false;
083 }
084 try {
085 return Integer.parseInt(s);
086 } catch (NumberFormatException e) {
087 }
088 try {
089 return Long.parseLong(s);
090 } catch (NumberFormatException e) {
091 }
092
093 return s;
094 }
095
096 public Object getComponent(Object componentKeyOrType) {
097 return delegate.getComponent(componentKeyOrType);
098 }
099
100 public <T> T getComponent(Class<T> componentType) {
101 return null;
102 }
103
104 public List getComponents() {
105 return delegate.getComponents();
106 }
107
108 public PicoContainer getParent() {
109 return new EmptyPicoContainer();
110 }
111
112 public ComponentAdapter<?> getComponentAdapter(Object componentKey) {
113 return delegate.getComponentAdapter(componentKey);
114 }
115
116 public <T> ComponentAdapter<T> getComponentAdapter(Class<T> componentType, ParameterName componentParameterName) {
117 return delegate.getComponentAdapter(componentType, componentParameterName);
118 }
119
120 public Collection<ComponentAdapter<?>> getComponentAdapters() {
121 return delegate.getComponentAdapters();
122 }
123
124 public <T> List<ComponentAdapter<T>> getComponentAdapters(Class<T> componentType) {
125 return null;
126 }
127
128 public <T> List<T> getComponents(Class<T> componentType) {
129 return delegate.getComponents(componentType);
130 }
131
132 public void accept(PicoVisitor visitor) {
133 delegate.accept(visitor);
134
135 }
136 }