KickJava   Java API By Example, From Geeks To Geeks.

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


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

5 package com.opensymphony.webwork.config;
6
7 import java.util.HashSet JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Set JavaDoc;
10
11
12 /**
13  * A Configuration implementation which stores an internal list of configuration objects. Each time
14  * a config method is called (get, set, list, etc..) this class will go through the list of configurations
15  * and call the method until successful.
16  *
17  * @author Rickard Öberg
18  * @author Jason Carreira
19  * @author Bill Lynch (docs)
20  */

21 public class DelegatingConfiguration extends Configuration {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     Configuration[] configList;
25
26     //~ Constructors ///////////////////////////////////////////////////////////
27

28     /**
29      * Creates a new DelegatingConfiguration object given a list of {@link Configuration} implementations.
30      *
31      * @param aConfigList a list of Configuration implementations.
32      */

33     public DelegatingConfiguration(Configuration[] aConfigList) {
34         configList = aConfigList;
35     }
36
37     //~ Methods ////////////////////////////////////////////////////////////////
38

39     /**
40      * Sets the given property - calls setImpl(String, Object) method on config objects in the config
41      * list until successful.
42      *
43      * @see #set(String, Object)
44      */

45     public void setImpl(String JavaDoc name, Object JavaDoc value) throws IllegalArgumentException JavaDoc, UnsupportedOperationException JavaDoc {
46         // Determine which config to use by using get
47
// Delegate to the other configurations
48
IllegalArgumentException JavaDoc e = null;
49
50         for (int i = 0; i < configList.length; i++) {
51             try {
52                 configList[i].getImpl(name);
53
54                 // Found it, now try setting
55
configList[i].setImpl(name, value);
56
57                 // Worked, now return
58
return;
59             } catch (IllegalArgumentException JavaDoc ex) {
60                 e = ex;
61
62                 // Try next config
63
}
64         }
65
66         throw e;
67     }
68
69     /**
70      * Gets the specified property - calls getImpl(String) method on config objects in config list
71      * until successful.
72      *
73      * @see #get(String)
74      */

75     public Object JavaDoc getImpl(String JavaDoc name) throws IllegalArgumentException JavaDoc {
76         // Delegate to the other configurations
77
IllegalArgumentException JavaDoc e = null;
78
79         for (int i = 0; i < configList.length; i++) {
80             try {
81                 return configList[i].getImpl(name);
82             } catch (IllegalArgumentException JavaDoc ex) {
83                 e = ex;
84
85                 // Try next config
86
}
87         }
88
89         throw e;
90     }
91
92     /**
93      * Determines if a paramter has been set - calls the isSetImpl(String) method on each config object
94      * in config list. Returns <tt>true</tt> when one of the config implementations returns true. Returns
95      * <tt>false</tt> otherwise.
96      *
97      * @see #isSet(String)
98      */

99     public boolean isSetImpl(String JavaDoc aName) {
100         for (int i = 0; i < configList.length; i++) {
101             if (configList[i].isSetImpl(aName)) {
102                 return true;
103             }
104         }
105
106         return false;
107     }
108
109     /**
110      * Returns a list of all property names - returns a list of all property names in all config
111      * objects in config list.
112      *
113      * @see #list()
114      */

115     public Iterator JavaDoc listImpl() {
116         boolean workedAtAll = false;
117
118         Set JavaDoc settingList = new HashSet JavaDoc();
119         UnsupportedOperationException JavaDoc e = null;
120
121         for (int i = 0; i < configList.length; i++) {
122             try {
123                 Iterator JavaDoc list = configList[i].listImpl();
124
125                 while (list.hasNext()) {
126                     settingList.add(list.next());
127                 }
128
129                 workedAtAll = true;
130             } catch (UnsupportedOperationException JavaDoc ex) {
131                 e = ex;
132
133                 // Try next config
134
}
135         }
136
137         if (!workedAtAll) {
138             throw (e == null) ? new UnsupportedOperationException JavaDoc() : e;
139         } else {
140             return settingList.iterator();
141         }
142     }
143 }
144
Popular Tags