KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > values > VariablesJob


1 package org.oddjob.values;
2
3 import org.apache.commons.beanutils.ConversionException;
4 import org.apache.commons.beanutils.DynaBean;
5 import org.apache.commons.beanutils.DynaClass;
6 import org.oddjob.arooa.ArooaConstants;
7 import org.oddjob.arooa.ArooaContext;
8 import org.oddjob.arooa.ArooaHandler;
9 import org.oddjob.arooa.handlers.MappedPropertyHandler;
10 import org.oddjob.framework.SimpleJob;
11 import org.oddjob.values.types.PropertyType;
12
13
14 /**
15  * @oddjob.description This job provides a 'variables'
16  * like declaration within
17  * Oddjob. Its child elements are types with a name and a
18  * value. The values are available as properties of this job.
19  *
20  * @oddjob.example
21  *
22  * <pre>
23  * &lt;oddjob&gt;
24  * &lt;sequential&gt;
25  * &lt;variables id="vars" name="Define a variable"&gt;
26  * &lt;value name="greeting" value="Hello World"/&gt;
27  * &lt;/variables&gt;
28  * &lt;echo name="Issue a greeting." text="${vars.greeting}" /&gt;
29  * &lt;/sequential&gt;
30  * &lt;/oddjob&gt;
31  * </pre>
32  */

33
34 public class VariablesJob extends SimpleJob
35         implements DynaBean {
36     
37     /** Implementation for a dyna map. */
38     private final PropertyType lazy = new PropertyType();
39     
40     /**
41      * Constructor.
42      *
43      */

44     public VariablesJob() {
45         // set the logger so it isn't hidden.
46
lazy.set("logger", getLogger());
47     }
48     
49     /**
50      * Add a name value pair.
51      *
52      * @oddjob.element <i>Any type</i>
53      * @oddjob.description Any type. In addition to the properties
54      * of the type (documented with each
55      * type) a name is required which is the name the
56      * variable will have.
57      * @oddjob.required No.
58      *
59      * @param name The name of the variable.
60      * @param value The runtime configurable for the value.
61      */

62     public void setValue(String JavaDoc name, Object JavaDoc value) {
63         logger().debug("Setting [" + name + "] = [" + value + "]");
64         lazy.add(name, value);
65     }
66     
67     /*
68      * (non-Javadoc)
69      * @see org.oddjob.jobs.AbstractJob#execute()
70      */

71     protected int execute() throws Exception JavaDoc {
72         
73         return 0;
74     }
75
76     /**
77      * Does the specified mapped property contain a value for the specified
78      * key value?
79      *
80      * @param name Name of the property to check
81      * @param key Name of the key to check
82      *
83      * @exception IllegalArgumentException if there is no property
84      * of the specified name
85      */

86     public boolean contains(String JavaDoc name, String JavaDoc key) {
87         return lazy.contains(name, key);
88     }
89
90     /**
91      * Return the value of a simple property with the specified name.
92      *
93      * @param name Name of the property whose value is to be retrieved
94      *
95      * @exception IllegalArgumentException if there is no property
96      * of the specified name
97      */

98     public Object JavaDoc get(String JavaDoc name) {
99         return lazy.get(name);
100     }
101
102     /**
103      * Return the value of an indexed property with the specified name.
104      *
105      * @param name Name of the property whose value is to be retrieved
106      * @param index Index of the value to be retrieved
107      *
108      * @exception IllegalArgumentException if there is no property
109      * of the specified name
110      * @exception IllegalArgumentException if the specified property
111      * exists, but is not indexed
112      * @exception IndexOutOfBoundsException if the specified index
113      * is outside the range of the underlying property
114      * @exception NullPointerException if no array or List has been
115      * initialized for this property
116      */

117     public Object JavaDoc get(String JavaDoc name, int index) {
118         return lazy.get(name, index);
119     }
120
121
122     /**
123      * Return the value of a mapped property with the specified name,
124      * or <code>null</code> if there is no value for the specified key.
125      *
126      * @param name Name of the property whose value is to be retrieved
127      * @param key Key of the value to be retrieved
128      *
129      * @exception IllegalArgumentException if there is no property
130      * of the specified name
131      * @exception IllegalArgumentException if the specified property
132      * exists, but is not mapped
133      */

134     public Object JavaDoc get(String JavaDoc name, String JavaDoc key) {
135         return lazy.get(name, key);
136     }
137
138     /**
139      * Return the <code>DynaClass</code> instance that describes the set of
140      * properties available for this DynaBean.
141      */

142     public DynaClass getDynaClass() {
143         return lazy.getDynaClass();
144     }
145
146
147     /**
148      * Remove any existing value for the specified key on the
149      * specified mapped property.
150      *
151      * @param name Name of the property for which a value is to
152      * be removed
153      * @param key Key of the value to be removed
154      *
155      * @exception IllegalArgumentException if there is no property
156      * of the specified name
157      */

158     public void remove(String JavaDoc name, String JavaDoc key) {
159         lazy.remove(name, key);
160     }
161     
162     /**
163      * Set the value of a simple property with the specified name.
164      *
165      * @param name Name of the property whose value is to be set
166      * @param value Value to which this property is to be set
167      *
168      * @exception ConversionException if the specified value cannot be
169      * converted to the type required for this property
170      * @exception IllegalArgumentException if there is no property
171      * of the specified name
172      * @exception NullPointerException if an attempt is made to set a
173      * primitive property to null
174      */

175     public void set(String JavaDoc name, Object JavaDoc value) {
176         logger().debug("Setting [" + name + "] = [" + value + "]");
177         lazy.set(name, value);
178     }
179
180
181     /**
182      * Set the value of an indexed property with the specified name.
183      *
184      * @param name Name of the property whose value is to be set
185      * @param index Index of the property to be set
186      * @param value Value to which this property is to be set
187      *
188      * @exception ConversionException if the specified value cannot be
189      * converted to the type required for this property
190      * @exception IllegalArgumentException if there is no property
191      * of the specified name
192      * @exception IllegalArgumentException if the specified property
193      * exists, but is not indexed
194      * @exception IndexOutOfBoundsException if the specified index
195      * is outside the range of the underlying property
196      */

197     public void set(String JavaDoc name, int index, Object JavaDoc value) {
198         logger().debug("Setting [" + name + "[" + index + "]] = [" + value + "]");
199         lazy.set(name, index, value);
200     }
201
202     /**
203      * Set the value of a mapped property with the specified name.
204      *
205      * @param name Name of the property whose value is to be set
206      * @param key Key of the property to be set
207      * @param value Value to which this property is to be set
208      *
209      * @exception ConversionException if the specified value cannot be
210      * converted to the type required for this property
211      * @exception IllegalArgumentException if there is no property
212      * of the specified name
213      * @exception IllegalArgumentException if the specified property
214      * exists, but is not mapped
215      */

216     public void set(String JavaDoc name, String JavaDoc key, Object JavaDoc value) {
217         logger().debug("Setting [" + name + "(" + key + ")] = [" + value + "]");
218         setValue(key, value);
219     }
220         
221     
222     public ArooaHandler handlerFor(ArooaContext context) {
223         context.set(ArooaConstants.RTC_LEAVE_PROXY, new Boolean JavaDoc(true));
224         context.set(ArooaConstants.ELEMENT_NAME, "value");
225         return new MappedPropertyHandler();
226     }
227
228 }
229
230
Popular Tags