/** * Returns an unmodifiable List<String> for the given key. Internally the value is stored as a single String, but converted to a list when calling this method. * <br> * The list is unmodifiable to prevent the mistake of trying <pre>getPropertyStringList(..).add("new element");</pre> which will not affect the stored * list. For the property to be updated this{@link #setPropertyStringList(String, List)} must be used. */ public List<String> getPropertyStringList(String key) { String value = getProperty(key); if (value!=null) { return Collections.unmodifiableList(fromListText(value)); } else { return null; } }
/** * Set a value that is known to be a list. The value can be retrieved using this{@link #getPropertyStringList(String)} * <br> * Within the file, the value is stored as a single Comma Separated Value */ public void setPropertyStringList(String key, List<String> value) { setProperty(key, toListText(value)); }
public synchronized void setProperty(String key, String value) { Object oldValue = getInternalPropertyMap().get(key); if (value==null) { deleteProperty(key); } else { getInternalPropertyMap().put(key,value); } if (value==null || !value.equals(oldValue)) { store(); } }
public void restoreDefaults() { propertyMap.clear(); propertyMap.putAll(getDefaultPropertyMap()); store(); }
public void testList() throws Exception { AbstractConfigurable c = DummyConfigurable.getInstance(); c.getInternalPropertyMap().clear(); c.setPropertyStringList("list", new ArrayList<String>()); assertTrue("Should be an instanceof a list",c.getPropertyStringList("list") instanceof List); assertEquals("there should be 0 items",0,c.getPropertyStringList("list").size()); ConfigurationManager.getInstance().populate(c); assertTrue("Should be an instanceof a list",c.getPropertyStringList("list") instanceof List); assertEquals("there should be 0 items",0,c.getPropertyStringList("list").size()); List<String> list = new ArrayList<String>(c.getPropertyStringList("list")); list.add("fred"); c.setPropertyStringList("list", list); assertEquals("there should be 1 item",1,c.getPropertyStringList("list").size()); assertEquals("there should be 1 item",1,c.getPropertyStringList("list").size()); ConfigurationManager.getInstance().populate(c); assertEquals("there should be 1 item",1,c.getPropertyStringList("list").size()); assertEquals("item should be fred","fred",c.getPropertyStringList("list").get(0)); c.getInternalPropertyMap().clear(); c.setProperty("list", "a,b,c"); assertEquals("There should be 3 items in the list",3,c.getPropertyStringList("list").size()); assertEquals("Item 1 should be a","a",c.getPropertyStringList("list").get(0)); assertEquals("Item 1 should be b","b",c.getPropertyStringList("list").get(1)); assertEquals("Item 1 should be c","c",c.getPropertyStringList("list").get(2));
@Test(expected=UnsupportedOperationException.class) public void testUnmodifiable() throws Exception { AbstractConfigurable c = DummyConfigurable.getInstance(); c.getInternalPropertyMap().clear(); c.setPropertyStringList("list", new ArrayList<String>()); c.getPropertyStringList("list").add("fred"); }
@Test public void testListNotThere() throws Exception { AbstractConfigurable c = DummyConfigurable.getInstance(); c.getInternalPropertyMap().clear(); assertNull("the property should be null",c.getProperty("sdflhsdfhsdfjkhsdfkhsdfkhsdfjkh")); assertNull("the list should be null if the property doesn't exist",c.getPropertyStringList("sdflhsdfhsdfjkhsdfkhsdfkhsdfjkh")); }
@Test public void testDeleteDefaultProperty() throws Exception { AbstractConfigurable conf = DummyConfigurable.getInstance(); ConfigurationManager manager = ConfigurationManager.getInstance(); assertEquals("name should equal john","john",conf.getProperty("name")); conf.deleteProperty("name"); manager.store(conf); manager.populate(conf); assertNull("value for name should be null",conf.getProperty("name")); Properties props = new Properties(); props.load(new FileInputStream(configFile)); assertTrue("Key name should be in stored props because its a deleted default value",props.containsKey("name")); assertEquals("name should have the special value to indicate its been deleted",ConfigurationManager.DELETED_VALUE_CODE,props.getProperty("name")); }
public void setRegistryMap(SortedMap<String, Registry> registries) { registryMap.clear(); registryMap.putAll(registries); super.clear(); List<String> keyList = new ArrayList<String>(); for (Entry<String, Registry> entry : registryMap.entrySet()) { final String key = entry.getKey(); keyList.add (key); super.setProperty(key, entry.getValue() .getRegistryBaseString()); } Collections.sort(keyList); String registryNamesConcatenated = StringUtils.join(keyList, ","); super.setProperty(REGISTRY_LIST, registryNamesConcatenated); }
private String[] getRegistryKeys() { String registryNamesConcatenated = super.getProperty(REGISTRY_LIST); if (registryNamesConcatenated == null) { return (String[])getDefaultPropertyMap().keySet().toArray(new String[]{}); } else { return registryNamesConcatenated.split(","); } }
public synchronized String getProperty(String key) { String val = getInternalPropertyMap().get(key); if (val==null) val=getDefaultProperty(key); if (ConfigurationManager.DELETED_VALUE_CODE.equals(val)) val=null; return val; }
public void clear() { getInternalPropertyMap().clear(); }
/** * Just a "proxy" method - {@link AbstractConfigurable#store()} is not * visible to the users of instances of this class otherwise. */ public void store() { super.store(); }
public void testList() throws Exception { AbstractConfigurable c = DummyConfigurable.getInstance(); c.getInternalPropertyMap().clear(); c.setPropertyStringList("list", new ArrayList<String>()); assertTrue("Should be an instanceof a list",c.getPropertyStringList("list") instanceof List); assertEquals("there should be 0 items",0,c.getPropertyStringList("list").size()); ConfigurationManager.getInstance().populate(c); assertTrue("Should be an instanceof a list",c.getPropertyStringList("list") instanceof List); assertEquals("there should be 0 items",0,c.getPropertyStringList("list").size()); List<String> list = new ArrayList<String>(c.getPropertyStringList("list")); list.add("fred"); c.setPropertyStringList("list", list); assertEquals("there should be 1 item",1,c.getPropertyStringList("list").size()); assertEquals("there should be 1 item",1,c.getPropertyStringList("list").size()); ConfigurationManager.getInstance().populate(c); assertEquals("there should be 1 item",1,c.getPropertyStringList("list").size()); assertEquals("item should be fred","fred",c.getPropertyStringList("list").get(0)); c.getInternalPropertyMap().clear(); c.setProperty("list", "a,b,c"); assertEquals("There should be 3 items in the list",3,c.getPropertyStringList("list").size()); assertEquals("Item 1 should be a","a",c.getPropertyStringList("list").get(0)); assertEquals("Item 1 should be b","b",c.getPropertyStringList("list").get(1)); assertEquals("Item 1 should be c","c",c.getPropertyStringList("list").get(2));
@Test(expected=UnsupportedOperationException.class) public void testUnmodifiable() throws Exception { AbstractConfigurable c = DummyConfigurable.getInstance(); c.getInternalPropertyMap().clear(); c.setPropertyStringList("list", new ArrayList<String>()); c.getPropertyStringList("list").add("fred"); }
@Test public void testListNotThere() throws Exception { AbstractConfigurable c = DummyConfigurable.getInstance(); c.getInternalPropertyMap().clear(); assertNull("the property should be null",c.getProperty("sdflhsdfhsdfjkhsdfkhsdfkhsdfjkh")); assertNull("the list should be null if the property doesn't exist",c.getPropertyStringList("sdflhsdfhsdfjkhsdfkhsdfkhsdfjkh")); }
@Test public void testDeleteDefaultProperty() throws Exception { AbstractConfigurable conf = DummyConfigurable.getInstance(); ConfigurationManager manager = ConfigurationManager.getInstance(); assertEquals("name should equal john","john",conf.getProperty("name")); conf.deleteProperty("name"); manager.store(conf); manager.populate(conf); assertNull("value for name should be null",conf.getProperty("name")); Properties props = new Properties(); props.load(new FileInputStream(configFile)); assertTrue("Key name should be in stored props because its a deleted default value",props.containsKey("name")); assertEquals("name should have the special value to indicate its been deleted",ConfigurationManager.DELETED_VALUE_CODE,props.getProperty("name")); }
private void updateRegistryMap() { registryMap.clear(); for (String key : getRegistryKeys()) { String value = super.getProperty(key); try { registryMap.put(key, calculateRegistry(new URL( value))); } catch (MalformedURLException e) { logger.error("bogus url (" + value + ") in configuration file", e); } catch (RegistryException e) { logger.error("failed to construct registry handle for " + value, e); } } }
public Set<String> getKeys() { return getInternalPropertyMap().keySet(); }
@Test public void testListDelimeters() throws Exception { AbstractConfigurable c = DummyConfigurable.getInstance(); c.getInternalPropertyMap().clear(); c.setPropertyStringList("list", new ArrayList<String>()); assertTrue("Should be an instanceof a list",c.getPropertyStringList("list") instanceof List); assertEquals("there should be 0 items",0,((List<String>)c.getPropertyStringList("list")).size()); List<String> list = new ArrayList<String>(c.getPropertyStringList("list")); list.add("a,b,c"); c.setPropertyStringList("list",list); assertEquals("there should be 1 items",1,((List<String>)c.getPropertyStringList("list")).size()); list = new ArrayList<String>(c.getPropertyStringList("list")); list.add("d"); c.setPropertyStringList("list",list); assertEquals("there should be 2 items",2,((List<String>)c.getPropertyStringList("list")).size()); assertEquals("The first item should be a,b,c","a,b,c",c.getPropertyStringList("list").get(0)); assertEquals("The second item should be d","d",c.getPropertyStringList("list").get(1)); assertEquals("there should be 2 items",2,((List<String>)c.getPropertyStringList("list")).size()); assertEquals("The first item should be a,b,c","a,b,c",c.getPropertyStringList("list").get(0)); assertEquals("The second item should be d","d",c.getPropertyStringList("list").get(1)); assertEquals("there should be 2 items",2,((List<String>)c.getPropertyStringList("list")).size()); assertEquals("The first item should be a,b,c","a,b,c",c.getPropertyStringList("list").get(0)); assertEquals("The second item should be d","d",c.getPropertyStringList("list").get(1));