1 19 20 package com.sslexplorer.boot; 21 22 import java.io.ByteArrayInputStream ; 23 import java.io.ByteArrayOutputStream ; 24 import java.io.IOException ; 25 import java.io.ObjectInputStream ; 26 import java.io.ObjectOutputStream ; 27 import java.io.ObjectStreamClass ; 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Collections ; 31 import java.util.HashMap ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.TreeMap ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 40 45 public abstract class AbstractPropertyClass implements PropertyClass { 46 47 final static Log log = LogFactory.getLog(AbstractPropertyClass.class); 48 49 private Map <String , PropertyDefinition> definitions; 51 private String name; 52 private List <PropertyDefinitionCategory> categories; 53 private Map <String , PropertyDefinitionCategory> categoryMap; 54 private boolean supportsReplacementVariablesInValues; 55 private boolean autoCommit = true; 56 private Map <AbstractPropertyKey,String > values = new HashMap <AbstractPropertyKey, String >(); 57 private ByteArrayOutputStream store; 58 59 66 public AbstractPropertyClass(String name, boolean supportsReplacementVariablesInValues) { 67 if(name.contains(" ")) { 68 throw new IllegalArgumentException ("Property type name may not contain spaces."); 69 } 70 this.supportsReplacementVariablesInValues = supportsReplacementVariablesInValues; 71 definitions = new TreeMap <String , PropertyDefinition>(); 72 categories = new ArrayList <PropertyDefinitionCategory>(); 73 categoryMap = new HashMap <String , PropertyDefinitionCategory>(); 74 this.name = name; 75 } 76 77 public void store() throws IOException { 78 if(store != null) { 79 throw new IllegalStateException ("Already storing property class. Either restor or reset first."); 80 } 81 store = new ByteArrayOutputStream (); 82 ObjectOutputStream oos = new ObjectOutputStream (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 { 98 log.info("Restoring property class " + getName()); 99 if(store == null) { 100 throw new IllegalStateException ("Nothing stored for " + getName()); 101 } 102 ObjectInputStream ois = new ObjectInputStream (new ByteArrayInputStream (store.toByteArray())) { 103 protected Class <?> resolveClass(ObjectStreamClass desc) throws IOException , ClassNotFoundException { 104 try { 105 return Class.forName(desc.getName(), false, AbstractPropertyClass.this.getClass().getClassLoader()); 106 } catch (ClassNotFoundException ex) { 107 return super.resolveClass(desc); 108 } 109 } 110 }; 111 try { 112 definitions = (Map <String , PropertyDefinition>) ois.readObject(); 113 categories = (List <PropertyDefinitionCategory>) ois.readObject(); 114 categoryMap = (Map <String , PropertyDefinitionCategory>) ois.readObject(); 115 store = null; 116 117 118 for(PropertyDefinition def : definitions.values()) 120 def.init(this); 121 for(PropertyDefinitionCategory cat : categories) 122 cat.setPropertyClass(this); 123 } 124 catch(ClassNotFoundException cnfe) { 125 throw new IOException ("Deserialisation failed. " + cnfe.getMessage()); 126 } 127 finally { 128 ois.close(); 129 } 130 } 131 132 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 161 public PropertyDefinitionCategory getPropertyDefinitionCategory(int id) { 162 return categoryMap.get(String.valueOf(id)); 163 } 164 165 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 182 public void deregisterPropertyDefinition(String propertyDefinitionName) { 183 definitions.remove(propertyDefinitionName); 184 } 185 186 191 public String getName() { 192 return name; 193 } 194 195 200 public PropertyDefinition getDefinition(String name) { 201 return definitions.get(name); 202 } 203 204 209 public Collection <PropertyDefinition> getDefinitions() { 210 List <PropertyDefinition> l = new ArrayList <PropertyDefinition>(definitions.values()); 211 Collections.sort(l); 212 return l; 213 } 214 215 218 public boolean isDefinitionExists(String name) { 219 return definitions.containsKey(name); 220 } 221 222 225 public String retrieveProperty(AbstractPropertyKey key) throws IllegalArgumentException { 226 synchronized(values) { 227 if(values.containsKey(key)) { 228 return (String )values.get(key); 229 } 230 } 231 return retrievePropertyImpl(key); 232 } 233 234 241 protected abstract String retrievePropertyImpl(AbstractPropertyKey key) throws IllegalArgumentException ; 242 243 246 public synchronized String storeProperty(AbstractPropertyKey key, String value) throws IllegalArgumentException { 247 if(!autoCommit) { 248 if(values.containsKey(key)) { 249 return values.put(key, value); 250 } 251 else { 252 String val = retrieveProperty(key); 253 values.put(key, value); 254 return val; 255 } 256 } 257 else { 258 return storePropertyImpl(key, value); 259 } 260 } 261 262 265 protected abstract String storePropertyImpl(AbstractPropertyKey key, String value) throws IllegalArgumentException ; 266 267 272 public void registerPropertyDefinition(PropertyDefinition propertyDefinition) { 273 propertyDefinition.init(this); 274 definitions.put(propertyDefinition.getName(), propertyDefinition); 275 } 276 277 280 public synchronized void commit() { 281 for(AbstractPropertyKey key : values.keySet()) { 282 storePropertyImpl(key, values.get(key)); 283 } 284 values.clear(); 285 } 286 287 290 public synchronized void setAutoCommit(boolean autoCommit) { 291 if(!autoCommit && this.autoCommit && values.size() > 0) { 292 throw new RuntimeException ("Cannot unset auto commit when there are values to be commited."); 293 } 294 this.autoCommit = autoCommit; 295 } 296 297 300 public Collection <PropertyDefinitionCategory> getCategories() { 301 return categories; 302 } 303 304 307 public boolean isSupportsReplacementVariablesInValues() { 308 return supportsReplacementVariablesInValues; 309 } 310 311 314 public int compareTo(PropertyClass o) { 315 return getName().compareTo(o.getName()); 316 } 317 318 329 public int retrievePropertyInt(AbstractPropertyKey key) throws IllegalArgumentException { 330 return Integer.parseInt(retrieveProperty(key)); 331 } 332 333 344 public long retrievePropertyLong(AbstractPropertyKey key) throws IllegalArgumentException { 345 return Long.parseLong(retrieveProperty(key)); 346 } 347 348 361 public boolean retrievePropertyBoolean(AbstractPropertyKey key) throws IllegalArgumentException { 362 return Boolean.parseBoolean(retrieveProperty(key)); 363 } 364 365 376 public PropertyList retrievePropertyList(AbstractPropertyKey key) throws IllegalArgumentException { 377 return new PropertyList(retrieveProperty(key)); 378 } 379 380 383 public void addPropertyDefinitionCategories(Collection <PropertyDefinitionCategory> propertyDefinitionCategories) { 384 for(PropertyDefinitionCategory cat : propertyDefinitionCategories) { 385 addPropertyDefinitionCategory(cat.getParent() == null ? -1 : cat.getParent().getId(), cat); 386 } 387 } 388 389 392 public void addPropertyDefinitions(Collection <PropertyDefinition> propertyDefinitions) { 393 for(PropertyDefinition def : propertyDefinitions) { 394 registerPropertyDefinition(def); 395 } 396 } 397 398 401 public void clearPropertyDefinitionCategories() { 402 categories.clear(); 403 categoryMap.clear(); 404 } 405 406 409 public void clearPropertyDefinitions() { 410 definitions.clear(); 411 } 412 } 413 | Popular Tags |