001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.config;
006:
007: import com.opensymphony.xwork.util.LocalizedTextUtil;
008: import com.opensymphony.webwork.WebWorkConstants;
009: import org.apache.commons.logging.Log;
010: import org.apache.commons.logging.LogFactory;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014: import java.util.StringTokenizer;
015:
016: /**
017: * Default implementation of Configuration - creates and delegates to other configurations by using an internal
018: * {@link DelegatingConfiguration}.
019: *
020: * @author Rickard �berg
021: * @author Jason Carreira
022: * @author Bill Lynch (docs)
023: */
024: public class DefaultConfiguration extends Configuration {
025:
026: protected Log log = LogFactory.getLog(this .getClass());
027: Configuration config;
028:
029: /**
030: * Creates a new DefaultConfiguration object by loading all property files
031: * and creating an internal {@link DelegatingConfiguration} object. All calls to get and set
032: * in this class will call that configuration object.
033: */
034: public DefaultConfiguration() {
035: // Create default implementations
036: // Use default properties and webwork.properties
037: ArrayList list = new ArrayList();
038:
039: try {
040: list.add(new PropertiesConfiguration("webwork"));
041: } catch (Exception e) {
042: log.warn("Could not find webwork.properties");
043: }
044:
045: try {
046: list.add(new PropertiesConfiguration(
047: "com/opensymphony/webwork/default"));
048: } catch (Exception e) {
049: log
050: .error(
051: "Could not find com/opensymphony/webwork/default.properties",
052: e);
053: }
054:
055: Configuration[] configList = new Configuration[list.size()];
056: config = new DelegatingConfiguration((Configuration[]) list
057: .toArray(configList));
058:
059: // Add list of additional properties configurations
060: try {
061: StringTokenizer configFiles = new StringTokenizer(
062: (String) config
063: .getImpl(WebWorkConstants.WEBWORK_CUSTOM_PROPERTIES),
064: ",");
065:
066: while (configFiles.hasMoreTokens()) {
067: String name = configFiles.nextToken();
068:
069: try {
070: list.add(new PropertiesConfiguration(name));
071: } catch (Exception e) {
072: log.error("Could not find " + name
073: + ".properties. Skipping");
074: }
075: }
076:
077: configList = new Configuration[list.size()];
078: config = new DelegatingConfiguration((Configuration[]) list
079: .toArray(configList));
080: } catch (IllegalArgumentException e) {
081: }
082:
083: // Add addtional list of i18n global resource bundles
084: try {
085:
086: LocalizedTextUtil
087: .addDefaultResourceBundle("com/opensymphony/webwork/webwork-messages");
088: StringTokenizer bundleFiles = new StringTokenizer(
089: (String) config
090: .getImpl(WebWorkConstants.WEBWORK_CUSTOM_I18N_RESOURCES),
091: ", ");
092:
093: while (bundleFiles.hasMoreTokens()) {
094: String name = bundleFiles.nextToken();
095:
096: try {
097: log.info("Loading global messages from " + name);
098: LocalizedTextUtil.addDefaultResourceBundle(name);
099: } catch (Exception e) {
100: log.error("Could not find " + name
101: + ".properties. Skipping");
102: }
103: }
104: } catch (IllegalArgumentException e) {
105: // webwork.custom.i18n.resources wasn't provided
106: }
107: }
108:
109: /**
110: * Sets the given property - delegates to the internal config implementation.
111: *
112: * @see #set(String, Object)
113: */
114: public void setImpl(String aName, Object aValue)
115: throws IllegalArgumentException,
116: UnsupportedOperationException {
117: config.setImpl(aName, aValue);
118: }
119:
120: /**
121: * Gets the specified property - delegates to the internal config implementation.
122: *
123: * @see #get(String)
124: */
125: public Object getImpl(String aName) throws IllegalArgumentException {
126: // Delegate
127: return config.getImpl(aName);
128: }
129:
130: /**
131: * Determines whether or not a value has been set - delegates to the internal config implementation.
132: *
133: * @see #isSet(String)
134: */
135: public boolean isSetImpl(String aName) {
136: return config.isSetImpl(aName);
137: }
138:
139: /**
140: * Returns a list of all property names - delegates to the internal config implementation.
141: *
142: * @see #list()
143: */
144: public Iterator listImpl() {
145: return config.listImpl();
146: }
147: }
|