View Javadoc

1   /*
2    * $Id: DefaultSettings.java 439747 2006-09-03 09:22:46Z 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.ArrayList;
21  import java.util.Iterator;
22  import java.util.StringTokenizer;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.struts2.StrutsConstants;
27  
28  import com.opensymphony.xwork2.util.LocalizedTextUtil;
29  
30  
31  /***
32   * Default implementation of Settings - creates and delegates to other settingss by using an internal
33   * {@link DelegatingSettings}.
34   */
35  public class DefaultSettings extends Settings {
36  
37      protected Log log = LogFactory.getLog(this.getClass());
38      Settings config;
39  
40  
41      /***
42       * Creates a new DefaultSettings object by loading all property files
43       * and creating an internal {@link DelegatingSettings} object. All calls to get and set
44       * in this class will call that settings object.
45       */
46      public DefaultSettings() {
47          // Create default implementations 
48          // Use default properties and struts.properties
49          ArrayList<Settings> list = new ArrayList<Settings>();
50  
51          try {
52              list.add(new PropertiesSettings("struts"));
53          } catch (Exception e) {
54              log.warn("Could not find or error in struts.properties", e);
55          }
56  
57          try {
58              list.add(new PropertiesSettings("org/apache/struts2/default"));
59          } catch (Exception e) {
60              log.error("Could not find org/apache/struts2/default.properties", e);
61          }
62  
63          Settings[] configList = new Settings[list.size()];
64          config = new DelegatingSettings((Settings[]) list.toArray(configList));
65  
66          // Add list of additional properties settingss
67          try {
68              StringTokenizer configFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_PROPERTIES), ",");
69  
70              while (configFiles.hasMoreTokens()) {
71                  String name = configFiles.nextToken();
72  
73                  try {
74                      list.add(new PropertiesSettings(name));
75                  } catch (Exception e) {
76                      log.error("Could not find " + name + ".properties. Skipping");
77                  }
78              }
79  
80              configList = new Settings[list.size()];
81              config = new DelegatingSettings((Settings[]) list.toArray(configList));
82          } catch (IllegalArgumentException e) {
83              // thrown when Settings is unable to find a certain property
84              // eg. struts.custom.properties in default.properties which is commented
85              // out
86          }
87  
88          // Add additional list of i18n global resource bundles
89          try {
90  
91              LocalizedTextUtil.addDefaultResourceBundle("org/apache/struts2/struts-messages");
92              StringTokenizer bundleFiles = new StringTokenizer((String) config.getImpl(StrutsConstants.STRUTS_CUSTOM_I18N_RESOURCES), ", ");
93  
94              while (bundleFiles.hasMoreTokens()) {
95                  String name = bundleFiles.nextToken();
96                  try {
97                      log.info("Loading global messages from " + name);
98                      LocalizedTextUtil.addDefaultResourceBundle(name);
99                  } catch (Exception e) {
100                     log.error("Could not find " + name + ".properties. Skipping");
101                 }
102             }
103         } catch (IllegalArgumentException e) {
104             // struts.custom.i18n.resources wasn't provided
105         }
106     }
107 
108 
109     /***
110      * Sets the given property - delegates to the internal config implementation.
111      *
112      * @see #set(String, String)
113      */
114     public void setImpl(String aName, String aValue) throws IllegalArgumentException, UnsupportedOperationException {
115         config.setImpl(aName, aValue);
116     }
117 
118     /***
119      * Gets the specified property - delegates to the internal config implementation.
120      *
121      * @see #get(String)
122      */
123     public String getImpl(String aName) throws IllegalArgumentException {
124         // Delegate
125         return config.getImpl(aName);
126     }
127 
128     /***
129      * Determines whether or not a value has been set - delegates to the internal config implementation.
130      *
131      * @see #isSet(String)
132      */
133     public boolean isSetImpl(String aName) {
134         return config.isSetImpl(aName);
135     }
136 
137     /***
138      * Returns a list of all property names - delegates to the internal config implementation.
139      *
140      * @see #list()
141      */
142     public Iterator listImpl() {
143         return config.listImpl();
144     }
145 }