View Javadoc

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