KickJava   Java API By Example, From Geeks To Geeks.

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


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.ByteArrayInputStream JavaDoc;
23 import java.io.ByteArrayOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.ObjectInputStream JavaDoc;
26 import java.io.ObjectOutputStream JavaDoc;
27 import java.io.ObjectStreamClass JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.List JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.TreeMap JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39
40 /**
41  * Default {@link PropertyClass}.
42  *
43  * @author Brett Smith <a HREF="mailto: brett@3sp.com">&lt;brett@3sp.com&gt;</a>
44  */

45 public abstract class AbstractPropertyClass implements PropertyClass {
46     
47     final static Log log = LogFactory.getLog(AbstractPropertyClass.class);
48
49     // Private instance variables
50
private Map JavaDoc<String JavaDoc, PropertyDefinition> definitions;
51     private String JavaDoc name;
52     private List JavaDoc<PropertyDefinitionCategory> categories;
53     private Map JavaDoc<String JavaDoc, PropertyDefinitionCategory> categoryMap;
54     private boolean supportsReplacementVariablesInValues;
55     private boolean autoCommit = true;
56     private Map JavaDoc<AbstractPropertyKey,String JavaDoc> values = new HashMap JavaDoc<AbstractPropertyKey, String JavaDoc>();
57     private ByteArrayOutputStream JavaDoc store;
58
59     /**
60      * Constructor.
61      *
62      * @param name property type name
63      * @param supportsReplacementVariablesInValues
64      * @throws IllegalArgumentException on illegal type name
65      */

66     public AbstractPropertyClass(String JavaDoc name, boolean supportsReplacementVariablesInValues) {
67         if(name.contains(" ")) {
68             throw new IllegalArgumentException JavaDoc("Property type name may not contain spaces.");
69         }
70         this.supportsReplacementVariablesInValues = supportsReplacementVariablesInValues;
71         definitions = new TreeMap JavaDoc<String JavaDoc, PropertyDefinition>();
72         categories = new ArrayList JavaDoc<PropertyDefinitionCategory>();
73         categoryMap = new HashMap JavaDoc<String JavaDoc, PropertyDefinitionCategory>();
74         this.name = name;
75     }
76     
77     public void store() throws IOException JavaDoc {
78         if(store != null) {
79             throw new IllegalStateException JavaDoc("Already storing property class. Either restor or reset first.");
80         }
81         store = new ByteArrayOutputStream JavaDoc();
82         ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(store);
83         try {
84             oos.writeObject(definitions);
85             oos.writeObject(categories);
86             oos.writeObject(categoryMap);
87         }
88         finally {
89             oos.close();
90         }
91     }
92     
93     public void reset() {
94         store = null;
95     }
96     
97     public void restore() throws IOException JavaDoc {
98         log.info("Restoring property class " + getName());
99         if(store == null) {
100             throw new IllegalStateException JavaDoc("Nothing stored for " + getName());
101         }
102         ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream JavaDoc(store.toByteArray())) {
103             protected Class JavaDoc<?> resolveClass(ObjectStreamClass JavaDoc desc) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
104                 try {
105                     return Class.forName(desc.getName(), false, AbstractPropertyClass.this.getClass().getClassLoader());
106                 } catch (ClassNotFoundException JavaDoc ex) {
107                     return super.resolveClass(desc);
108                 }
109             }
110         };
111         try {
112             definitions = (Map JavaDoc<String JavaDoc, PropertyDefinition>) ois.readObject();
113             categories = (List JavaDoc<PropertyDefinitionCategory>) ois.readObject();
114             categoryMap = (Map JavaDoc<String JavaDoc, PropertyDefinitionCategory>) ois.readObject();
115             store = null;
116
117             
118             // PropertyClass member variable is transient so we need to reinitialise
119
for(PropertyDefinition def : definitions.values())
120                 def.init(this);
121             for(PropertyDefinitionCategory cat : categories)
122                 cat.setPropertyClass(this);
123         }
124         catch(ClassNotFoundException JavaDoc cnfe) {
125             throw new IOException JavaDoc("Deserialisation failed. " + cnfe.getMessage());
126         }
127         finally {
128             ois.close();
129         }
130     }
131
132     /*
133      * (non-Javadoc)
134      *
135      * @see com.sslexplorer.properties.PropertyType#addPropertyDefinitionCategory(int,
136      * com.sslexplorer.properties.PropertyDefinitionCategory)
137      */

138     public void addPropertyDefinitionCategory(int parentId, PropertyDefinitionCategory category) {
139         PropertyDefinitionCategory parent = parentId == -1 ? null : categoryMap.get(String.valueOf(parentId));
140         if (parent != null) {
141             category.setParent(parent);
142             if (parent.contains(category)) {
143                 parent.removeCategory(category);
144             }
145             parent.addCategory(category);
146         } else {
147             if(categories.contains(category)) {
148                 categories.remove(category);
149             }
150             categories.add(category);
151         }
152         categoryMap.put(String.valueOf(category.getId()), category);
153
154     }
155
156     /*
157      * (non-Javadoc)
158      *
159      * @see com.sslexplorer.properties.PropertyType#getPropertyDefinitionCategory(int)
160      */

161     public PropertyDefinitionCategory getPropertyDefinitionCategory(int id) {
162         return categoryMap.get(String.valueOf(id));
163     }
164
165     /*
166      * (non-Javadoc)
167      *
168      * @see com.sslexplorer.properties.PropertyType#removePropertyDefinitionCategory(int,
169      * com.sslexplorer.properties.PropertyDefinitionCategory)
170      */

171     public void removePropertyDefinitionCategory(int parentId, PropertyDefinitionCategory category) {
172         PropertyDefinitionCategory parent = parentId == -1 ? null : categoryMap.get(String.valueOf(parentId));
173         if (parent != null) {
174             parent.removeCategory(category);
175         }
176
177     }
178
179     /* (non-Javadoc)
180      * @see com.sslexplorer.properties.PropertyType#deregisterPropertyDefinition(java.lang.String)
181      */

182     public void deregisterPropertyDefinition(String JavaDoc propertyDefinitionName) {
183         definitions.remove(propertyDefinitionName);
184     }
185
186     /*
187      * (non-Javadoc)
188      *
189      * @see com.sslexplorer.properties.PropertyType#getName()
190      */

191     public String JavaDoc getName() {
192         return name;
193     }
194
195     /*
196      * (non-Javadoc)
197      *
198      * @see com.sslexplorer.properties.PropertyType#getPropertyDefinition(java.lang.String)
199      */

200     public PropertyDefinition getDefinition(String JavaDoc name) {
201         return definitions.get(name);
202     }
203
204     /*
205      * (non-Javadoc)
206      *
207      * @see com.sslexplorer.properties.PropertyType#getPropertyDefinitions()
208      */

209     public Collection JavaDoc<PropertyDefinition> getDefinitions() {
210         List JavaDoc<PropertyDefinition> l = new ArrayList JavaDoc<PropertyDefinition>(definitions.values());
211         Collections.sort(l);
212         return l;
213     }
214
215     /* (non-Javadoc)
216      * @see com.sslexplorer.boot.PropertyClass#isDefinitionExists(java.lang.String)
217      */

218     public boolean isDefinitionExists(String JavaDoc name) {
219         return definitions.containsKey(name);
220     }
221
222     /* (non-Javadoc)
223      * @see com.sslexplorer.boot.PropertyClass#retrieveProperty(com.sslexplorer.boot.AbstractPropertyKey)
224      */

225     public String JavaDoc retrieveProperty(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
226         synchronized(values) {
227             if(values.containsKey(key)) {
228                 return (String JavaDoc)values.get(key);
229             }
230         }
231         return retrievePropertyImpl(key);
232     }
233
234     /**
235      * Retrieve the property from the underlying store.
236      *
237      * @param key key
238      * @return value
239      * @throws IllegalArgumentException if key is incorrect
240      */

241     protected abstract String JavaDoc retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc;
242
243     /* (non-Javadoc)
244      * @see com.sslexplorer.boot.PropertyClass#storeProperty(com.sslexplorer.boot.AbstractPropertyKey, java.lang.String)
245      */

246     public synchronized String JavaDoc storeProperty(AbstractPropertyKey key, String JavaDoc value) throws IllegalArgumentException JavaDoc {
247         if(!autoCommit) {
248             if(values.containsKey(key)) {
249                 return values.put(key, value);
250             }
251             else {
252                 String JavaDoc val = retrieveProperty(key);
253                 values.put(key, value);
254                 return val;
255             }
256         }
257         else {
258             return storePropertyImpl(key, value);
259         }
260     }
261     
262     /* (non-Javadoc)
263      * @see com.sslexplorer.boot.PropertyClass#storeProperty(com.sslexplorer.boot.AbstractPropertyKey, java.lang.String)
264      */

265     protected abstract String JavaDoc storePropertyImpl(AbstractPropertyKey key, String JavaDoc value) throws IllegalArgumentException JavaDoc;
266
267     /*
268      * (non-Javadoc)
269      *
270      * @see com.sslexplorer.properties.PropertyType#registerPropertyDefinition(com.sslexplorer.boot.PropertyDefinition)
271      */

272     public void registerPropertyDefinition(PropertyDefinition propertyDefinition) {
273         propertyDefinition.init(this);
274         definitions.put(propertyDefinition.getName(), propertyDefinition);
275     }
276
277     /* (non-Javadoc)
278      * @see com.sslexplorer.boot.PropertyClass#commit()
279      */

280     public synchronized void commit() {
281         for(AbstractPropertyKey key : values.keySet()) {
282             storePropertyImpl(key, values.get(key));
283         }
284         values.clear();
285     }
286
287     /* (non-Javadoc)
288      * @see com.sslexplorer.boot.PropertyClass#setAutoCommit(boolean)
289      */

290     public synchronized void setAutoCommit(boolean autoCommit) {
291         if(!autoCommit && this.autoCommit && values.size() > 0) {
292             throw new RuntimeException JavaDoc("Cannot unset auto commit when there are values to be commited.");
293         }
294         this.autoCommit = autoCommit;
295     }
296
297     /* (non-Javadoc)
298      * @see com.sslexplorer.boot.PropertyClass#getCategories()
299      */

300     public Collection JavaDoc<PropertyDefinitionCategory> getCategories() {
301         return categories;
302     }
303
304     /* (non-Javadoc)
305      * @see com.sslexplorer.boot.PropertyClass#isSupportsReplacementVariablesInValues()
306      */

307     public boolean isSupportsReplacementVariablesInValues() {
308         return supportsReplacementVariablesInValues;
309     }
310     
311     /* (non-Javadoc)
312      * @see java.lang.Comparable#compareTo(java.lang.Object)
313      */

314     public int compareTo(PropertyClass o) {
315         return getName().compareTo(o.getName());
316     }
317
318     /**
319      * Retrieve a property as an integer. The value will be retrieved as a string
320      * then converted to a primitive int.
321      * <p>
322      * If the property has never been set then the default value provided in the
323      * {@link PropertyDefinition} will be returned.
324      *
325      * @param key property key
326      * @return value
327      * @throws IllegalArgumentException if property doesn't exist
328      */

329     public int retrievePropertyInt(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
330         return Integer.parseInt(retrieveProperty(key));
331     }
332
333     /**
334      * Retrieve a property as a long. The value will be retrieved as a string then
335      * converted to a primitive long.
336      * <p>
337      * If the property has never been set then the default value provided in the
338      * {@link PropertyDefinition} will be returned.
339      *
340      * @param key property key
341      * @return value
342      * @throws IllegalArgumentException if property doesn't exist
343      */

344     public long retrievePropertyLong(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
345         return Long.parseLong(retrieveProperty(key));
346     }
347
348     /**
349      * Retrieve a property as a boolen. The value will be retrieved as a string then
350      * converted to a primitive boolean, <code>true</code> if the string value
351      * is <i>true</i> otherwise <code>false</code>.
352      * <p>
353      * If the property has never been set then the default value provided in the
354      * {@link PropertyDefinition} will be returned.
355      * <p>
356      *
357      * @param key property key
358      * @return value
359      * @throws IllegalArgumentException if property doesn't exist
360      */

361     public boolean retrievePropertyBoolean(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
362         return Boolean.parseBoolean(retrieveProperty(key));
363     }
364
365     /**
366      * Retrieve a property as a list. The value will be retrieved as a string then
367      * converted to a {@link PropertyList}.
368      * <p>
369      * If the property has never been set then the default value provided in the
370      * {@link PropertyDefinition} will be returned.
371      *
372      * @param key property key
373      * @return value
374      * @throws IllegalArgumentException if property doesn't exist
375      */

376     public PropertyList retrievePropertyList(AbstractPropertyKey key) throws IllegalArgumentException JavaDoc {
377         return new PropertyList(retrieveProperty(key));
378     }
379
380     /* (non-Javadoc)
381      * @see com.sslexplorer.boot.PropertyClass#addPropertyDefinitionCategories(java.util.Collection)
382      */

383     public void addPropertyDefinitionCategories(Collection JavaDoc<PropertyDefinitionCategory> propertyDefinitionCategories) {
384         for(PropertyDefinitionCategory cat : propertyDefinitionCategories) {
385             addPropertyDefinitionCategory(cat.getParent() == null ? -1 : cat.getParent().getId(), cat);
386         }
387     }
388
389     /* (non-Javadoc)
390      * @see com.sslexplorer.boot.PropertyClass#addPropertyDefinitions(java.util.Collection)
391      */

392     public void addPropertyDefinitions(Collection JavaDoc<PropertyDefinition> propertyDefinitions) {
393         for(PropertyDefinition def : propertyDefinitions) {
394             registerPropertyDefinition(def);
395         }
396     }
397
398     /* (non-Javadoc)
399      * @see com.sslexplorer.boot.PropertyClass#clearPropertyDefinitionCategories()
400      */

401     public void clearPropertyDefinitionCategories() {
402         categories.clear();
403         categoryMap.clear();
404     }
405
406     /* (non-Javadoc)
407      * @see com.sslexplorer.boot.PropertyClass#clearPropertyDefinitions()
408      */

409     public void clearPropertyDefinitions() {
410         definitions.clear();
411     }
412 }
413
Popular Tags