View Javadoc

    1   /*
    2    * $Id: DefaultSettings.java 499294 2007-01-24 07:33:24Z mrdon $
    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.ArrayList;
    24  import java.util.Iterator;
    25  import java.util.StringTokenizer;
    26
    27  import org.apache.commons.logging.Log;
    28  import org.apache.commons.logging.LogFactory;
    29  import org.apache.struts2.StrutsConstants;
    30
    31  import com.opensymphony.xwork2.util.LocalizedTextUtil;
    32
    33
    34  /***
    35   * DefaultSettings implements optional methods of Settings.
    36   * <p>
    37   * This class creates and delegates to other settings by using an internal
    38   * {@link DelegatingSettings} object.
    39   */
    40  public class DefaultSettings extends Settings {
    41
    42      /***
    43       * The logging instance for this class.
    44       */
    45      protected Log log = LogFactory.getLog(this.getClass());
    46
    47      /***
    48       * The Settings object that handles API calls.
    49       */
    50      Settings delegate;
    51
    52      /***
    53       * Constructs an instance by loading the standard property files,
    54       * any custom property files (<code>struts.custom.properties</code>),
    55       * and any custom message resources ().
    56       * <p>
    57       * Since this constructor  combines Settings from multiple resources,
    58       * it utilizes a {@link DelegatingSettings} instance,
    59       * and all API calls are handled by that instance.
    60       */
    61      public DefaultSettings() {
    62
    63          ArrayList<Settings> list = new ArrayList<Settings>();
    64
    65          // stuts.properties, default.properties
    66          try {
    67              list.add(new PropertiesSettings("struts"));
    68          } catch (Exception e) {
    69              log.warn("DefaultSettings: Could not find or error in struts.properties", e);
    70          }
    71
    72          Settings[] settings = new Settings[list.size()];
    73          delegate = new DelegatingSettings(list.toArray(settings));
    74
    75          // struts.custom.properties
    76          try {
    77              StringTokenizer customProperties = new StringTokenizer(delegate.getImpl(StrutsConstants.STRUTS_CUSTOM_PROPERTIES), ",");
    78
    79              while (customProperties.hasMoreTokens()) {
    80                  String name = customProperties.nextToken();
    81
    82                  try {
    83                      list.add(new PropertiesSettings(name));
    84                  } catch (Exception e) {
    85                      log.error("DefaultSettings: Could not find " + name + ".properties. Skipping.");
    86                  }
    87              }
    88
    89              settings = new Settings[list.size()];
    90              delegate = new DelegatingSettings(list.toArray(settings));
    91          } catch (IllegalArgumentException e) {
    92              // Assume it's OK, since IllegalArgumentException is thrown
    93              // when Settings is unable to find a certain setting,
    94              // like the struts.custom.properties, which is commented out
    95          }
    96
    97      }
    98
    99      // See superclass for Javadoc
    100     public void setImpl(String name, String value) throws IllegalArgumentException, UnsupportedOperationException {
    101         delegate.setImpl(name, value);
    102     }
    103
    104     // See superclass for Javadoc
    105     public String getImpl(String aName) throws IllegalArgumentException {
    106         return delegate.getImpl(aName);
    107     }
    108
    109     // See superclass for Javadoc
    110     public boolean isSetImpl(String aName) {
    111         return delegate.isSetImpl(aName);
    112     }
    113
    114     // See superclass for Javadoc
    115     public Iterator listImpl() {
    116         return delegate.listImpl();
    117     }
    118 }