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