1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.config;
19
20 import java.util.HashSet;
21 import java.util.Iterator;
22 import java.util.Set;
23
24
25 /***
26 * A Settings implementation which stores an internal list of settings objects. Each time
27 * a config method is called (get, set, list, etc..) this class will go through the list of settingss
28 * and call the method until successful.
29 *
30 */
31 public class DelegatingSettings extends Settings {
32
33 Settings[] configList;
34
35
36 /***
37 * Creates a new DelegatingSettings object given a list of {@link Settings} implementations.
38 *
39 * @param aConfigList a list of Settings implementations.
40 */
41 public DelegatingSettings(Settings[] aConfigList) {
42 configList = aConfigList;
43 }
44
45
46 /***
47 * Sets the given property - calls setImpl(String, Object) method on config objects in the config
48 * list until successful.
49 *
50 * @see #set(String, String)
51 */
52 public void setImpl(String name, String value) throws IllegalArgumentException, UnsupportedOperationException {
53
54
55 IllegalArgumentException e = null;
56
57 for (int i = 0; i < configList.length; i++) {
58 try {
59 configList[i].getImpl(name);
60
61
62 configList[i].setImpl(name, value);
63
64
65 return;
66 } catch (IllegalArgumentException ex) {
67 e = ex;
68
69
70 }
71 }
72
73 throw e;
74 }
75
76 /***
77 * Gets the specified property - calls getImpl(String) method on config objects in config list
78 * until successful.
79 *
80 * @see #get(String)
81 */
82 public String getImpl(String name) throws IllegalArgumentException {
83
84 IllegalArgumentException e = null;
85
86 for (int i = 0; i < configList.length; i++) {
87 try {
88 return configList[i].getImpl(name);
89 } catch (IllegalArgumentException ex) {
90 e = ex;
91
92
93 }
94 }
95
96 throw e;
97 }
98
99 /***
100 * Determines if a paramter has been set - calls the isSetImpl(String) method on each config object
101 * in config list. Returns <tt>true</tt> when one of the config implementations returns true. Returns
102 * <tt>false</tt> otherwise.
103 *
104 * @see #isSet(String)
105 */
106 public boolean isSetImpl(String aName) {
107 for (int i = 0; i < configList.length; i++) {
108 if (configList[i].isSetImpl(aName)) {
109 return true;
110 }
111 }
112
113 return false;
114 }
115
116 /***
117 * Returns a list of all property names - returns a list of all property names in all config
118 * objects in config list.
119 *
120 * @see #list()
121 */
122 public Iterator listImpl() {
123 boolean workedAtAll = false;
124
125 Set<Object> settingList = new HashSet<Object>();
126 UnsupportedOperationException e = null;
127
128 for (int i = 0; i < configList.length; i++) {
129 try {
130 Iterator list = configList[i].listImpl();
131
132 while (list.hasNext()) {
133 settingList.add(list.next());
134 }
135
136 workedAtAll = true;
137 } catch (UnsupportedOperationException ex) {
138 e = ex;
139
140
141 }
142 }
143
144 if (!workedAtAll) {
145 throw (e == null) ? new UnsupportedOperationException() : e;
146 } else {
147 return settingList.iterator();
148 }
149 }
150 }