KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > config > DefaultConfiguration


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.webwork.config;
6
7 import com.opensymphony.xwork.util.LocalizedTextUtil;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10
11 import java.util.ArrayList JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.StringTokenizer JavaDoc;
14
15
16 /**
17  * Default implementation of Configuration - creates and delegates to other configurations by using an internal
18  * {@link DelegatingConfiguration}.
19  *
20  * @author Rickard Öberg
21  * @author Jason Carreira
22  * @author Bill Lynch (docs)
23  */

24 public class DefaultConfiguration extends Configuration {
25     //~ Instance fields ////////////////////////////////////////////////////////
26

27     protected Log log = LogFactory.getLog(this.getClass());
28     Configuration config;
29
30     //~ Constructors ///////////////////////////////////////////////////////////
31

32     /**
33      * Creates a new DefaultConfiguration object by loading all property files
34      * and creating an internal {@link DelegatingConfiguration} object. All calls to get and set
35      * in this class will call that configuration object.
36      */

37     public DefaultConfiguration() {
38         // Create default implementations
39
// Use default properties and webwork.properties
40
ArrayList JavaDoc list = new ArrayList JavaDoc();
41
42         try {
43             list.add(new PropertiesConfiguration("webwork"));
44         } catch (Exception JavaDoc e) {
45             log.warn("Could not find webwork.properties");
46         }
47
48         try {
49             list.add(new PropertiesConfiguration("com/opensymphony/webwork/default"));
50         } catch (Exception JavaDoc e) {
51             log.error("Could not find com/opensymphony/webwork/default.properties", e);
52         }
53
54         Configuration[] configList = new Configuration[list.size()];
55         config = new DelegatingConfiguration((Configuration[]) list.toArray(configList));
56
57         // Add list of additional properties configurations
58
try {
59             StringTokenizer JavaDoc configFiles = new StringTokenizer JavaDoc((String JavaDoc) config.getImpl("webwork.custom.properties"), ",");
60
61             while (configFiles.hasMoreTokens()) {
62                 String JavaDoc name = configFiles.nextToken();
63
64                 try {
65                     list.add(new PropertiesConfiguration(name));
66                 } catch (Exception JavaDoc e) {
67                     log.error("Could not find " + name + ".properties. Skipping");
68                 }
69             }
70
71             configList = new Configuration[list.size()];
72             config = new DelegatingConfiguration((Configuration[]) list.toArray(configList));
73         } catch (IllegalArgumentException JavaDoc e) {
74         }
75
76         // Add addtional list of i18n global resource bundles
77
try {
78             StringTokenizer JavaDoc bundleFiles = new StringTokenizer JavaDoc((String JavaDoc) config.getImpl("webwork.custom.i18n.resources"), ", ");
79
80             while (bundleFiles.hasMoreTokens()) {
81                 String JavaDoc name = bundleFiles.nextToken();
82
83                 try {
84                     log.info("Loading global messages from " + name);
85                     LocalizedTextUtil.addDefaultResourceBundle(name);
86                 } catch (Exception JavaDoc e) {
87                     log.error("Could not find " + name + ".properties. Skipping");
88                 }
89             }
90         } catch (IllegalArgumentException JavaDoc e) {
91             // webwork.custom.i18n.resources wasn't provided
92
}
93     }
94
95     //~ Methods ////////////////////////////////////////////////////////////////
96

97     /**
98      * Sets the given property - delegates to the internal config implementation.
99      *
100      * @see #set(String, Object)
101      */

102     public void setImpl(String JavaDoc aName, Object JavaDoc aValue) throws IllegalArgumentException JavaDoc, UnsupportedOperationException JavaDoc {
103         config.setImpl(aName, aValue);
104     }
105
106     /**
107      * Gets the specified property - delegates to the internal config implementation.
108      *
109      * @see #get(String)
110      */

111     public Object JavaDoc getImpl(String JavaDoc aName) throws IllegalArgumentException JavaDoc {
112         // Delegate
113
return config.getImpl(aName);
114     }
115
116     /**
117      * Determines whether or not a value has been set - delegates to the internal config implementation.
118      *
119      * @see #isSet(String)
120      */

121     public boolean isSetImpl(String JavaDoc aName) {
122         return config.isSetImpl(aName);
123     }
124
125     /**
126      * Returns a list of all property names - delegates to the internal config implementation.
127      *
128      * @see #list()
129      */

130     public Iterator JavaDoc listImpl() {
131         return config.listImpl();
132     }
133 }
134
Popular Tags