KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > invicta > handler > InvictaBasicHandler


1 package net.sf.invicta.handler;
2
3 import java.util.List JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import net.sf.invicta.InvictaException;
7 import net.sf.invicta.Logger;
8 import net.sf.invicta.api.DefinedProperty;
9 import net.sf.invicta.api.InvictaComponent;
10 import net.sf.invicta.api.InvictaProject;
11
12 /**
13  *
14  */

15 public abstract class InvictaBasicHandler implements InvictaHandler {
16     protected Map JavaDoc paramsMap;
17     protected List JavaDoc params;
18     protected InvictaComponent component;
19
20     /**
21      *
22      * @return InvictaProject
23      */

24     protected InvictaProject getProject() {
25         return this.component.getProject();
26     }
27     
28     /**
29      *
30      * @return InvictaComponent
31      */

32     protected InvictaComponent getComponent() {
33         return this.component;
34     }
35
36     /**
37      *
38      * @return int
39      */

40     protected int getParametersNumber() {
41         if (this.params != null) {
42             return this.params.size();
43         } else {
44             return this.paramsMap.size();
45         }
46     }
47
48     /**
49      *
50      * @param index
51      * @return String
52      */

53     protected String JavaDoc getParameter(int index) {
54         if ((this.params == null) || (this.params.size() <= index))
55             return null;
56                     
57         return (String JavaDoc)this.params.get(index);
58     }
59
60     /**
61      *
62      * @param key
63      * @return String
64      */

65     protected String JavaDoc getParameter(Object JavaDoc key) {
66         if (this.paramsMap == null)
67             return null;
68                     
69         return (String JavaDoc)this.paramsMap.get(key);
70     }
71     
72     /**
73      *
74      * @param index
75      * @param defaultValue
76      * @return String
77      */

78     protected String JavaDoc getParameter(int index, String JavaDoc defaultValue) {
79         String JavaDoc value = getParameter(index);
80         if (value == null)
81             return defaultValue;
82                     
83         return value;
84     }
85
86     /**
87      *
88      * @param key
89      * @param defaultValue
90      * @return String
91      */

92     protected String JavaDoc getParameter(Object JavaDoc key, String JavaDoc defaultValue) {
93         String JavaDoc value = getParameter(key);
94         if (value == null)
95             return defaultValue;
96         
97         return value;
98     }
99
100     /**
101      *
102      * @param index
103      * @param defaultValue
104      * @return boolean
105      */

106     protected boolean getBooleanParameter(int index, boolean defaultValue) {
107         String JavaDoc value = getParameter(index);
108         if (value == null)
109             return defaultValue;
110                     
111         return Boolean.valueOf(value).booleanValue();
112     }
113
114     /**
115      *
116      * @param key
117      * @param defaultValue
118      * @return boolean
119      */

120     protected boolean getBooleanParameter(Object JavaDoc key, boolean defaultValue) {
121         String JavaDoc value = getParameter(key);
122         if (value == null)
123             return defaultValue;
124         
125         return Boolean.valueOf(value).booleanValue();
126     }
127
128     /**
129      *
130      * @param index
131      * @return String
132      * @throws InvictaHandlerException
133      */

134     protected String JavaDoc getRequiredParameter(int index) throws InvictaHandlerException {
135         String JavaDoc value = getParameter(index);
136         if (value == null)
137             throw new InvictaHandlerException(this, "Missing required parameter at index " + index);
138                     
139         return value;
140     }
141
142     /**
143      *
144      * @param key
145      * @return String
146      * @throws InvictaHandlerException
147      */

148     protected String JavaDoc getRequiredParameter(Object JavaDoc key) throws InvictaHandlerException {
149         String JavaDoc value = getParameter(key);
150                     
151         if (value == null)
152             throw new InvictaHandlerException(this, "Missing required parameter: '" + key + "'");
153                     
154         return value;
155     }
156     
157     /**
158      *
159      * @param key
160      * @return String
161      * @throws InvictaHandlerException
162      */

163     protected String JavaDoc getParameterPropertyValue(Object JavaDoc key) throws InvictaHandlerException {
164         String JavaDoc propertyName = getParameter(key);
165         if (propertyName == null)
166             return null;
167         return getRequiredPropertyValue(propertyName);
168     }
169     
170     /**
171      *
172      * @param index
173      * @return String
174      * @throws InvictaHandlerException
175      */

176     protected String JavaDoc getParameterPropertyValue(int index) throws InvictaHandlerException {
177         String JavaDoc propertyName = getParameter(index);
178         if (propertyName == null)
179             return null;
180         return getRequiredPropertyValue(propertyName);
181     }
182
183     /**
184      *
185      * @param key
186      * @param defaultValue
187      * @return boolean
188      * @throws InvictaHandlerException
189      */

190     protected boolean getParameterPropertyBooleanValue(Object JavaDoc key, boolean defaultValue) throws InvictaHandlerException {
191         String JavaDoc propertyName = getParameter(key);
192         if (propertyName == null)
193             return defaultValue;
194         
195         String JavaDoc value = getRequiredPropertyValue(propertyName);
196         return Boolean.valueOf(value).booleanValue();
197     }
198     
199     /**
200      *
201      * @param index
202      * @param defaultValue
203      * @return boolean
204      * @throws InvictaHandlerException
205      */

206     protected boolean getParameterPropertyBooleanValue(int index, boolean defaultValue) throws InvictaHandlerException {
207         String JavaDoc propertyName = getParameter(index);
208         if (propertyName == null)
209             return defaultValue;
210         String JavaDoc value = getRequiredPropertyValue(propertyName);
211         return Boolean.valueOf(value).booleanValue();
212     }
213
214     /**
215      *
216      * @param propertyName
217      * @return String
218      * @throws InvictaHandlerException
219      */

220     protected String JavaDoc getRequiredPropertyValue(String JavaDoc propertyName) throws InvictaHandlerException {
221         String JavaDoc value = getComponent().getPropertyValue(propertyName);
222         
223         if (value == null)
224             throw new InvictaHandlerException(this,
225                 "Property '" + propertyName
226                 + "' not defined for component '"
227                 + getComponent().getName());
228         
229         return value;
230     }
231     
232     /**
233      *
234      * @param propertyName
235      * @return String
236      * @throws InvictaHandlerException
237      */

238     protected String JavaDoc getRequiredPropertyReference(String JavaDoc propertyName) throws InvictaHandlerException {
239         DefinedProperty property = getComponent().getDefinedProperty(propertyName);
240         
241         if (property == null)
242             throw new InvictaHandlerException(this,
243                 "Property '" + propertyName
244                 + "' not defined for component '"
245                 + getComponent().getName());
246         
247         return property.getReferenceValue();
248     }
249     
250     /**
251      *
252      * @param index
253      * @return boolean
254      */

255     protected boolean containsParameter(int index) {
256         return getParameter(index) != null;
257     }
258
259     /**
260      *
261      * @param key
262      * @return boolean
263      */

264     protected boolean containsParameter(Object JavaDoc key) {
265         return getParameter(key) != null;
266     }
267
268     /**
269      *
270      * @param context
271      * @param params
272      * @return Object
273      * @throws InvictaException
274      */

275     public Object JavaDoc handle(Object JavaDoc context, List JavaDoc params) throws InvictaException {
276     
277         if (!(context instanceof InvictaComponent))
278             throw new InvictaHandlerException(this, "Template problem: Wrong component paramteter (Must be InvictaComponent)");
279                 
280         this.component = (InvictaComponent)context;
281                  
282         this.params = params;
283         return handle(params);
284     }
285
286     /**
287      *
288      * @param context
289      * @param paramsMap
290      * @return Object
291      * @throws InvictaException
292      */

293     public Object JavaDoc handle(Object JavaDoc context, Map JavaDoc paramsMap) throws InvictaException {
294     
295         if (!(context instanceof InvictaComponent))
296             throw new InvictaHandlerException(this, "Template problem: Wrong component paramteter (Must be InvictaComponent)");
297                 
298         this.component = (InvictaComponent)context;
299         this.paramsMap = paramsMap;
300         return handle(paramsMap);
301     }
302     
303     /**
304      *
305      * @param params
306      * @return String
307      * @throws InvictaException
308      */

309     public String JavaDoc handle(List JavaDoc params) throws InvictaException {
310         throw new InvictaHandlerException(this, "Cannot receive list of parameters.");
311     }
312     
313     /**
314      *
315      * @param paramsMap
316      * @return String
317      * @throws InvictaException
318      */

319     public String JavaDoc handle(Map JavaDoc paramsMap) throws InvictaException {
320         throw new InvictaHandlerException(this, "Cannot receive map of parameters.");
321     }
322
323     /**
324      *
325      * @param message
326      */

327     protected void log(String JavaDoc message) {
328         Logger.info(getName() + ": " + message);
329     }
330     
331     /**
332      *
333      * @param message
334      */

335     protected void debug(String JavaDoc message) {
336         Logger.info(getName() + ": " + message);
337     }
338     
339 }
340
Popular Tags