KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > boot > DefaultPropertyDefinition


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.boot;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.List JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 /**
30  * Default implementation of a {@link com.sslexplorer.boot.PropertyDefinition}.
31  * <p>
32  * As well as providing default attributes, this class understands a
33  * <i>Validation Pattern</i>. This is used to provide a
34  * {@link PropertyValidator} implementation used when the
35  * {@link #validate(String)} method is invoked.
36  *
37  * <p>
38  * If a <code>null</code> pattern is supplied, a default validator based on
39  * property type will be created or
40  * <p>
41  * If not <code>null</code> the pattern should be in the format
42  * <code>[className]([property1=value1,..)</code>. The bracketed property
43  * list is optional. For example, to use the <code>IntegerValidator</code> to
44  * validate integer values between 10 and 20 you would use
45  * <code>com.sslexplorer.input.validators.IntegerValidator(minValue=10,maxValue=20)</code>.
46  *
47  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
48  */

49 public class DefaultPropertyDefinition implements PropertyDefinition, Comparable JavaDoc<PropertyDefinition>, Serializable JavaDoc {
50
51
52     
53     // Private instance variables
54

55     protected int type = TYPE_STRING, category;
56     protected String JavaDoc name, typeMeta;
57     protected String JavaDoc defaultValue;
58     protected int sortOrder;
59     protected Object JavaDoc typeMetaObject;
60     protected String JavaDoc messageResourcesKey;
61     protected boolean hidden;
62     protected String JavaDoc validationString;
63     protected transient PropertyClass propertyClass;
64     protected boolean restartRequired;
65
66     //
67
static HashMap JavaDoc<String JavaDoc, PropertyValidator> validators = new HashMap JavaDoc<String JavaDoc, PropertyValidator>();
68
69     /**
70      * Constructor.
71      */

72     public DefaultPropertyDefinition() {
73         this(TYPE_UNDEFINED, null, null, 0, null, 0, false);
74     }
75
76     /**
77      * Constructor.
78      *
79      * @param type type
80      * @param name name
81      * @param typeMeta type meta-data
82      * @param category category ID
83      * @param defaultValue default value
84      * @param sortOrder sort order
85      * @param hidden hidden
86      */

87     public DefaultPropertyDefinition(int type, String JavaDoc name, String JavaDoc typeMeta, int category, String JavaDoc defaultValue,
88                                      int sortOrder, boolean hidden) {
89         this(type, name, typeMeta, category, defaultValue, sortOrder, hidden, null);
90     }
91
92     /**
93      * Constructor.
94      *
95      * @param type type
96      * @param name name
97      * @param typeMeta type meta-data
98      * @param category category ID
99      * @param defaultValue default value
100      * @param sortOrder sort order
101      * @param hidden hidden
102      * @param validationString validation pattern. See class documentation.
103      */

104     public DefaultPropertyDefinition(int type, String JavaDoc name, String JavaDoc typeMeta, int category, String JavaDoc defaultValue,
105                                      int sortOrder, boolean hidden, String JavaDoc validationString) {
106         this(type, name, typeMeta, category, defaultValue, sortOrder, "properties", hidden, validationString);
107     }
108
109     /**
110      * Constructor.
111      *
112      * @param type type
113      * @param name name
114      * @param typeMeta type meta-data
115      * @param category category ID
116      * @param defaultValue default value
117      * @param sortOrder sort order
118      * @param messageResourcesKey message resource bundle bundle
119      * @param hidden hidden
120      */

121     public DefaultPropertyDefinition(int type, String JavaDoc name, String JavaDoc typeMeta, int category, String JavaDoc defaultValue,
122                                      int sortOrder, String JavaDoc messageResourcesKey, boolean hidden) {
123         this(type, name, typeMeta, category, defaultValue, sortOrder, messageResourcesKey, hidden, null);
124     }
125
126     /**
127      * Constructor.
128      *
129      * @param type type
130      * @param name name
131      * @param typeMeta type meta-data
132      * @param category category ID
133      * @param defaultValue default value
134      * @param sortOrder sort order
135      * @param messageResourcesKey message resource bundle bundle
136      * @param hidden hidden
137      * @param validationString validation string. See class documentation.
138      */

139     public DefaultPropertyDefinition(int type, String JavaDoc name, String JavaDoc typeMeta, int category, String JavaDoc defaultValue,
140                                      int sortOrder, String JavaDoc messageResourcesKey, boolean hidden, String JavaDoc validationString) {
141         this.type = type;
142         this.name = name;
143         this.typeMeta = Util.trimmedOrBlank(typeMeta);
144         this.category = category;
145         this.defaultValue = defaultValue;
146         this.sortOrder = sortOrder;
147         this.hidden = hidden;
148         this.messageResourcesKey = messageResourcesKey == null ? "properties" : messageResourcesKey;
149         this.validationString = validationString;
150     }
151
152     protected void check() {
153
154         switch (type) {
155             case TYPE_TIME_IN_MS:
156             case TYPE_INTEGER:
157                 if (this.validationString == null || this.validationString.equals("")) {
158                     this.validationString = "com.sslexplorer.input.validators.IntegerValidator(replacementVariables=" + getPropertyClass().isSupportsReplacementVariablesInValues() + ")";
159                 }
160                 break;
161             case TYPE_BOOLEAN: {
162                 List JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
163                 StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(typeMeta == null || typeMeta.equals("") ? "true,false" : typeMeta, ",");
164                 while (t.hasMoreTokens()) {
165                     l.add(t.nextToken());
166                 }
167                 typeMetaObject = l;
168                 break;
169             }
170             case TYPE_LIST: {
171                 List JavaDoc<TypeMetaListItem> l = new ArrayList JavaDoc<TypeMetaListItem>();
172                 StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(typeMeta, ",");
173                 while (t.hasMoreTokens()) {
174                     l.add(new TypeMetaListItem(t.nextToken(), this.messageResourcesKey));
175                 }
176                 typeMetaObject = l;
177                 break;
178             }
179         }
180     }
181
182     /*
183      * (non-Javadoc)
184      *
185      * @see com.sslexplorer.boot.PropertyDefinition#getMessageResourcesKey()
186      */

187     public String JavaDoc getMessageResourcesKey() {
188         return messageResourcesKey;
189     }
190
191     /*
192      * (non-Javadoc)
193      *
194      * @see com.sslexplorer.boot.PropertyDefinition#getCategory()
195      */

196     public int getCategory() {
197         return category;
198     }
199
200     /**
201      * @param category
202      */

203     public void setCategory(int category) {
204         this.category = category;
205     }
206
207     /*
208      * (non-Javadoc)
209      *
210      * @see com.sslexplorer.boot.PropertyDefinition#getName()
211      */

212     public String JavaDoc getName() {
213         return name;
214     }
215
216     /**
217      * Set the name. Cannot be set once define.
218      *
219      * @param name name
220      * @throws IllegalStateException if already set
221      */

222     public void setName(String JavaDoc name) {
223         if(this.name != null) {
224             throw new IllegalStateException JavaDoc("Already set.");
225         }
226         this.name = name;
227     }
228
229     /*
230      * (non-Javadoc)
231      *
232      * @see com.sslexplorer.boot.PropertyDefinition#getType()
233      */

234     public int getType() {
235         return type;
236     }
237
238     /**
239      * Set the type. Cannot be set once defined.
240      *
241      * @param type type
242      * @throws IllegalStateException if type already set
243      */

244     public void setType(int type) {
245         if(this.type != TYPE_UNDEFINED) {
246             throw new IllegalStateException JavaDoc("Type is already set.");
247         }
248         this.type = type;
249     }
250
251     /*
252      * (non-Javadoc)
253      *
254      * @see com.sslexplorer.boot.PropertyDefinition#getTypeMeta()
255      */

256     public String JavaDoc getTypeMeta() {
257         return typeMeta;
258     }
259
260     /*
261      * (non-Javadoc)
262      *
263      * @see com.sslexplorer.boot.PropertyDefinition#setTypeMeta(java.lang.String)
264      */

265     public void setTypeMeta(String JavaDoc typeMeta) {
266         this.typeMeta = Util.trimmedOrBlank(typeMeta);
267     }
268
269     /*
270      * (non-Javadoc)
271      *
272      * @see com.sslexplorer.boot.PropertyDefinition#getDefaultValue()
273      */

274     public String JavaDoc getDefaultValue() {
275         return defaultValue==null ? "" : defaultValue;
276     }
277
278     /*
279      * (non-Javadoc)
280      *
281      * @see com.sslexplorer.boot.PropertyDefinition#setDefaultValue(java.lang.String)
282      */

283     public void setDefaultValue(String JavaDoc defaultValue) {
284         this.defaultValue = defaultValue==null ? "" : defaultValue;
285     }
286
287     /*
288      * (non-Javadoc)
289      *
290      * @see com.sslexplorer.boot.PropertyDefinition#getSortOrder()
291      */

292     public int getSortOrder() {
293         return sortOrder;
294     }
295
296     /**
297      * Set the sort order
298      *
299      * @param sortOrder sort order
300      */

301     public void setSortOrder(int sortOrder) {
302         this.sortOrder = sortOrder;
303     }
304
305     /*
306      * (non-Javadoc)
307      *
308      * @see com.sslexplorer.boot.PropertyDefinition#getTypeMetaObject()
309      */

310     public Object JavaDoc getTypeMetaObject() {
311         return typeMetaObject;
312     }
313
314     /*
315      * (non-Javadoc)
316      *
317      * @see com.sslexplorer.boot.PropertyDefinition#isHidden()
318      */

319     public boolean isHidden() {
320         return hidden;
321     }
322
323     /* (non-Javadoc)
324      * @see com.sslexplorer.boot.PropertyDefinition#validate(java.lang.String, java.lang.ClassLoader)
325      */

326     public void validate(String JavaDoc value, ClassLoader JavaDoc classLoader) throws CodedException {
327         if (validationString == null || validationString.equals("")) {
328             return;
329         }
330         int idx = validationString.indexOf('(');
331         try {
332             PropertyValidator v = getValidator(classLoader, idx == -1 ? validationString : validationString.substring(0, idx));
333             Properties JavaDoc p = null;
334             if (idx != -1) {
335                 if (!validationString.endsWith(")")) {
336                     throw new Exception JavaDoc("Validation string in incorrect format, missing ).");
337                 }
338                 PropertyList pl = new PropertyList(validationString.substring(idx + 1, validationString.length() - 1), ',');
339                 p = pl.getAsNameValuePairs();
340             }
341             v.validate(this, value, p);
342         } catch (CodedException ce) {
343             throw ce;
344         } catch (Exception JavaDoc e) {
345             throw new Error JavaDoc("Failed to create validator using '" + validationString + "'. ", e);
346         }
347     }
348
349     /*
350      * (non-Javadoc)
351      *
352      * @see java.lang.Comparable#compareTo(java.lang.Object)
353      */

354     public int compareTo(PropertyDefinition obj) {
355         int i = new Integer JavaDoc(getCategory()).compareTo(new Integer JavaDoc(obj.getCategory()));
356         return i == 0 ? new Integer JavaDoc(getSortOrder()).compareTo(new Integer JavaDoc(obj.getSortOrder())) : i;
357     }
358
359     /* (non-Javadoc)
360      * @see com.sslexplorer.boot.PropertyDefinition#isRestartRequired()
361      */

362     public boolean isRestartRequired() {
363         return restartRequired;
364     }
365     
366     /**
367      * Set whether restart is required when this property changes
368      *
369      * @param restartRequired restart required
370      */

371     public void setRestartRequired(boolean restartRequired) {
372         this.restartRequired = restartRequired;
373     }
374
375     /**
376      * Get the validation string
377      *
378      * @return validation string
379      */

380     public String JavaDoc getValidationString() {
381         return validationString;
382     }
383     
384     /* (non-Javadoc)
385      * @see com.sslexplorer.boot.PropertyDefinition#setValidationString(java.lang.String)
386      */

387     public void setValidationString(String JavaDoc validationString) {
388         this.validationString = validationString;
389     }
390
391     /* (non-Javadoc)
392      * @see com.sslexplorer.boot.PropertyDefinition#getPropertyClass()
393      */

394     public PropertyClass getPropertyClass() {
395         return propertyClass;
396     }
397
398     /* (non-Javadoc)
399      * @see com.sslexplorer.boot.PropertyDefinition#init(com.sslexplorer.boot.PropertyClass)
400      */

401     public void init(PropertyClass propertyClass) {
402         this.propertyClass = propertyClass;
403         check();
404     }
405
406     /* (non-Javadoc)
407      * @see java.lang.Object#toString()
408      */

409     public String JavaDoc toString() {
410         return "[PropertyDefinition name='" + name
411             + "' type="
412             + type
413             + " category="
414             + category
415             + " defaultValue='"
416             + defaultValue
417             + "' typeMeta='"
418             + typeMeta
419             + "' sortOrder="
420             + sortOrder
421             + " messageResourcesKey='"
422             + messageResourcesKey
423             + "' validationString='"
424             + validationString
425             + "'";
426     }
427
428     static PropertyValidator getValidator(ClassLoader JavaDoc classLoader, String JavaDoc className) throws InstantiationException JavaDoc, IllegalAccessException JavaDoc,
429                     ClassNotFoundException JavaDoc {
430         synchronized (validators) {
431             PropertyValidator pv = validators.get(className);
432             if (pv == null) {
433                 pv = (PropertyValidator) Class.forName(className, true, classLoader).newInstance();
434                 validators.put(className, pv);
435             }
436             return pv;
437         }
438     }
439 }
Popular Tags