001: /*
002: * $Id: DefaultSettings.java 499294 2007-01-24 07:33:24Z mrdon $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.config;
022:
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.StringTokenizer;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.apache.struts2.StrutsConstants;
030:
031: import com.opensymphony.xwork2.util.LocalizedTextUtil;
032:
033: /**
034: * DefaultSettings implements optional methods of Settings.
035: * <p>
036: * This class creates and delegates to other settings by using an internal
037: * {@link DelegatingSettings} object.
038: */
039: public class DefaultSettings extends Settings {
040:
041: /**
042: * The logging instance for this class.
043: */
044: protected Log log = LogFactory.getLog(this .getClass());
045:
046: /**
047: * The Settings object that handles API calls.
048: */
049: Settings delegate;
050:
051: /**
052: * Constructs an instance by loading the standard property files,
053: * any custom property files (<code>struts.custom.properties</code>),
054: * and any custom message resources ().
055: * <p>
056: * Since this constructor combines Settings from multiple resources,
057: * it utilizes a {@link DelegatingSettings} instance,
058: * and all API calls are handled by that instance.
059: */
060: public DefaultSettings() {
061:
062: ArrayList<Settings> list = new ArrayList<Settings>();
063:
064: // stuts.properties, default.properties
065: try {
066: list.add(new PropertiesSettings("struts"));
067: } catch (Exception e) {
068: log
069: .warn(
070: "DefaultSettings: Could not find or error in struts.properties",
071: e);
072: }
073:
074: Settings[] settings = new Settings[list.size()];
075: delegate = new DelegatingSettings(list.toArray(settings));
076:
077: // struts.custom.properties
078: try {
079: StringTokenizer customProperties = new StringTokenizer(
080: delegate
081: .getImpl(StrutsConstants.STRUTS_CUSTOM_PROPERTIES),
082: ",");
083:
084: while (customProperties.hasMoreTokens()) {
085: String name = customProperties.nextToken();
086:
087: try {
088: list.add(new PropertiesSettings(name));
089: } catch (Exception e) {
090: log.error("DefaultSettings: Could not find " + name
091: + ".properties. Skipping.");
092: }
093: }
094:
095: settings = new Settings[list.size()];
096: delegate = new DelegatingSettings(list.toArray(settings));
097: } catch (IllegalArgumentException e) {
098: // Assume it's OK, since IllegalArgumentException is thrown
099: // when Settings is unable to find a certain setting,
100: // like the struts.custom.properties, which is commented out
101: }
102:
103: }
104:
105: // See superclass for Javadoc
106: public void setImpl(String name, String value)
107: throws IllegalArgumentException,
108: UnsupportedOperationException {
109: delegate.setImpl(name, value);
110: }
111:
112: // See superclass for Javadoc
113: public String getImpl(String aName) throws IllegalArgumentException {
114: return delegate.getImpl(aName);
115: }
116:
117: // See superclass for Javadoc
118: public boolean isSetImpl(String aName) {
119: return delegate.isSetImpl(aName);
120: }
121:
122: // See superclass for Javadoc
123: public Iterator listImpl() {
124: return delegate.listImpl();
125: }
126: }
|