001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.config;
006:
007: import java.util.HashSet;
008: import java.util.Iterator;
009: import java.util.Set;
010:
011: /**
012: * A Configuration implementation which stores an internal list of configuration objects. Each time
013: * a config method is called (get, set, list, etc..) this class will go through the list of configurations
014: * and call the method until successful.
015: *
016: * @author Rickard Öberg
017: * @author Jason Carreira
018: * @author Bill Lynch (docs)
019: */
020: public class DelegatingConfiguration extends Configuration {
021:
022: Configuration[] configList;
023:
024: /**
025: * Creates a new DelegatingConfiguration object given a list of {@link Configuration} implementations.
026: *
027: * @param aConfigList a list of Configuration implementations.
028: */
029: public DelegatingConfiguration(Configuration[] aConfigList) {
030: configList = aConfigList;
031: }
032:
033: /**
034: * Sets the given property - calls setImpl(String, Object) method on config objects in the config
035: * list until successful.
036: *
037: * @see #set(String, Object)
038: */
039: public void setImpl(String name, Object value)
040: throws IllegalArgumentException,
041: UnsupportedOperationException {
042: // Determine which config to use by using get
043: // Delegate to the other configurations
044: IllegalArgumentException e = null;
045:
046: for (int i = 0; i < configList.length; i++) {
047: try {
048: configList[i].getImpl(name);
049:
050: // Found it, now try setting
051: configList[i].setImpl(name, value);
052:
053: // Worked, now return
054: return;
055: } catch (IllegalArgumentException ex) {
056: e = ex;
057:
058: // Try next config
059: }
060: }
061:
062: throw e;
063: }
064:
065: /**
066: * Gets the specified property - calls getImpl(String) method on config objects in config list
067: * until successful.
068: *
069: * @see #get(String)
070: */
071: public Object getImpl(String name) throws IllegalArgumentException {
072: // Delegate to the other configurations
073: IllegalArgumentException e = null;
074:
075: for (int i = 0; i < configList.length; i++) {
076: try {
077: return configList[i].getImpl(name);
078: } catch (IllegalArgumentException ex) {
079: e = ex;
080:
081: // Try next config
082: }
083: }
084:
085: throw e;
086: }
087:
088: /**
089: * Determines if a paramter has been set - calls the isSetImpl(String) method on each config object
090: * in config list. Returns <tt>true</tt> when one of the config implementations returns true. Returns
091: * <tt>false</tt> otherwise.
092: *
093: * @see #isSet(String)
094: */
095: public boolean isSetImpl(String aName) {
096: for (int i = 0; i < configList.length; i++) {
097: if (configList[i].isSetImpl(aName)) {
098: return true;
099: }
100: }
101:
102: return false;
103: }
104:
105: /**
106: * Returns a list of all property names - returns a list of all property names in all config
107: * objects in config list.
108: *
109: * @see #list()
110: */
111: public Iterator listImpl() {
112: boolean workedAtAll = false;
113:
114: Set settingList = new HashSet();
115: UnsupportedOperationException e = null;
116:
117: for (int i = 0; i < configList.length; i++) {
118: try {
119: Iterator list = configList[i].listImpl();
120:
121: while (list.hasNext()) {
122: settingList.add(list.next());
123: }
124:
125: workedAtAll = true;
126: } catch (UnsupportedOperationException ex) {
127: e = ex;
128:
129: // Try next config
130: }
131: }
132:
133: if (!workedAtAll) {
134: throw (e == null) ? new UnsupportedOperationException() : e;
135: } else {
136: return settingList.iterator();
137: }
138: }
139: }
|