1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.config;
23
24 import java.util.HashSet;
25 import java.util.Iterator;
26 import java.util.Set;
27
28
29 /***
30 * DelegatingSettings stores an internal list of {@link Settings} objects
31 * to update settings or retrieve settings values.
32 * <p>
33 * Each time a Settings method is called (get, set, list, and so forth),
34 * this class goes through the list of Settings objects
35 * and calls that method for each delegate,
36 * withholding any exception until all delegates have been called.
37 *
38 */
39 class DelegatingSettings extends Settings {
40
41 /***
42 * The Settings objects.
43 */
44 Settings[] delegates;
45
46 /***
47 * Creates a new DelegatingSettings object utilizing the list of {@link Settings} objects.
48 *
49 * @param delegates The Settings objects to use as delegates
50 */
51 public DelegatingSettings(Settings[] delegates) {
52 this.delegates = delegates;
53 }
54
55
56 public void setImpl(String name, String value) throws IllegalArgumentException, UnsupportedOperationException {
57 IllegalArgumentException e = null;
58
59 for (Settings delegate : delegates) {
60 try {
61 delegate.getImpl(name);
62 delegate.setImpl(name, value);
63 return;
64 } catch (IllegalArgumentException ex) {
65 e = ex;
66
67
68 }
69 }
70
71 throw e;
72 }
73
74
75 public String getImpl(String name) throws IllegalArgumentException {
76
77 IllegalArgumentException e = null;
78
79 for (Settings delegate : delegates) {
80 try {
81 return delegate.getImpl(name);
82 } catch (IllegalArgumentException ex) {
83 e = ex;
84
85
86 }
87 }
88
89 throw e;
90 }
91
92
93 public boolean isSetImpl(String aName) {
94 for (Settings delegate : delegates) {
95 if (delegate.isSetImpl(aName)) {
96 return true;
97 }
98 }
99
100 return false;
101 }
102
103
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
123 }
124 }
125
126 if (!workedAtAll) {
127 throw (e == null) ? new UnsupportedOperationException() : e;
128 } else {
129 return settingList.iterator();
130 }
131 }
132 }