View Javadoc

1   /*
2    * $Id: DelegatingSettings.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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      // See superclass for Javadoc
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); // Throws exception if not found
62                  delegate.setImpl(name, value); // Found it
63                  return; // Done
64              } catch (IllegalArgumentException ex) {
65                  e = ex;
66  
67                  // Try next delegate
68              }
69          }
70  
71          throw e;
72      }
73  
74      // See superclass for Javadoc
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);  // Throws exception if not found
82              } catch (IllegalArgumentException ex) {
83                  e = ex;
84  
85                  // Try next delegate
86              }
87          }
88  
89          throw e;
90      }
91  
92      // See superclass for Javadoc
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     // 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 }