001: /*
002: * $Id: DelegatingSettings.java 478167 2006-11-22 14:19:53Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.config;
022:
023: import java.util.HashSet;
024: import java.util.Iterator;
025: import java.util.Set;
026:
027: /**
028: * DelegatingSettings stores an internal list of {@link Settings} objects
029: * to update settings or retrieve settings values.
030: * <p>
031: * Each time a Settings method is called (get, set, list, and so forth),
032: * this class goes through the list of Settings objects
033: * and calls that method for each delegate,
034: * withholding any exception until all delegates have been called.
035: *
036: */
037: class DelegatingSettings extends Settings {
038:
039: /**
040: * The Settings objects.
041: */
042: Settings[] delegates;
043:
044: /**
045: * Creates a new DelegatingSettings object utilizing the list of {@link Settings} objects.
046: *
047: * @param delegates The Settings objects to use as delegates
048: */
049: public DelegatingSettings(Settings[] delegates) {
050: this .delegates = delegates;
051: }
052:
053: // See superclass for Javadoc
054: public void setImpl(String name, String value)
055: throws IllegalArgumentException,
056: UnsupportedOperationException {
057: IllegalArgumentException e = null;
058:
059: for (Settings delegate : delegates) {
060: try {
061: delegate.getImpl(name); // Throws exception if not found
062: delegate.setImpl(name, value); // Found it
063: return; // Done
064: } catch (IllegalArgumentException ex) {
065: e = ex;
066:
067: // Try next delegate
068: }
069: }
070:
071: throw e;
072: }
073:
074: // See superclass for Javadoc
075: public String getImpl(String name) throws IllegalArgumentException {
076:
077: IllegalArgumentException e = null;
078:
079: for (Settings delegate : delegates) {
080: try {
081: return delegate.getImpl(name); // Throws exception if not found
082: } catch (IllegalArgumentException ex) {
083: e = ex;
084:
085: // Try next delegate
086: }
087: }
088:
089: throw e;
090: }
091:
092: // See superclass for Javadoc
093: public boolean isSetImpl(String aName) {
094: for (Settings delegate : delegates) {
095: if (delegate.isSetImpl(aName)) {
096: return true;
097: }
098: }
099:
100: return false;
101: }
102:
103: // See superclass for Javadoc
104: public Iterator listImpl() {
105: boolean workedAtAll = false;
106:
107: Set<Object> settingList = new HashSet<Object>();
108: UnsupportedOperationException e = null;
109:
110: for (Settings delegate : delegates) {
111: try {
112: Iterator list = delegate.listImpl();
113:
114: while (list.hasNext()) {
115: settingList.add(list.next());
116: }
117:
118: workedAtAll = true;
119: } catch (UnsupportedOperationException ex) {
120: e = ex;
121:
122: // Try next delegate
123: }
124: }
125:
126: if (!workedAtAll) {
127: throw (e == null) ? new UnsupportedOperationException() : e;
128: } else {
129: return settingList.iterator();
130: }
131: }
132: }
|