View Javadoc

1   /*
2    * $Id: DelegatingSettings.java 425615 2006-07-26 04:33:53Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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          // Determine which config to use by using get
54          // Delegate to the other settingss
55          IllegalArgumentException e = null;
56  
57          for (int i = 0; i < configList.length; i++) {
58              try {
59                  configList[i].getImpl(name);
60  
61                  // Found it, now try setting
62                  configList[i].setImpl(name, value);
63  
64                  // Worked, now return
65                  return;
66              } catch (IllegalArgumentException ex) {
67                  e = ex;
68  
69                  // Try next config
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          // Delegate to the other settings
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                  // Try next config
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                 // Try next config
141             }
142         }
143 
144         if (!workedAtAll) {
145             throw (e == null) ? new UnsupportedOperationException() : e;
146         } else {
147             return settingList.iterator();
148         }
149     }
150 }