View Javadoc

    1   /*
    2    * $Id: DelegatingSettings.java 478167 2006-11-22 14:19:53Z husted $
    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  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      // See superclass for Javadoc
    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); // Throws exception if not found
    61                  delegate.setImpl(name, value); // Found it
    62                  return; // Done
    63              } catch (IllegalArgumentException ex) {
    64                  e = ex;
    65
    66                  // Try next delegate
    67              }
    68          }
    69
    70          throw e;
    71      }
    72
    73      // See superclass for Javadoc
    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);  // Throws exception if not found
    81              } catch (IllegalArgumentException ex) {
    82                  e = ex;
    83
    84                  // Try next delegate
    85              }
    86          }
    87
    88          throw e;
    89      }
    90
    91      // See superclass for Javadoc
    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     // See superclass for Javadoc
    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                 // Try next delegate
    122             }
    123         }
    124
    125         if (!workedAtAll) {
    126             throw (e == null) ? new UnsupportedOperationException() : e;
    127         } else {
    128             return settingList.iterator();
    129         }
    130     }
    131 }