/** * Update the map-elements in the datasource that are below <i>parent</i>. * * @param parent Parent element to add / update * @param configuration configuration that holds the properties * @param mapName the name of the map-property in the configuration that holds the elements * @param propertyElementName the name of the element to write below <i>parent</i> */ private static void updateMap( Element parent, Configuration configuration, String mapName, String propertyElementName) { PropertyMap map = configuration.getMap(mapName); // Wrap in ArrayList to avoid ConcurrentModificationException when adding or removing children // while iterating. List<Element> mapElements = new ArrayList<Element>(parent.getChildren(propertyElementName)); if ((map == null) || map.getMap().isEmpty()) { if (!mapElements.isEmpty()) { parent.removeChildren(propertyElementName); } return; } Map<String, Element> elements = new HashMap<String, Element>(); for (Element el : mapElements) { elements.put(el.getAttributeValue("name"), el); if (map.get(el.getAttributeValue("name")) == null) { parent.removeContent(el); } } for (Property prop : map.getMap().values()) { Element element = elements.get(prop.getName()); if (element == null) { element = new Element(propertyElementName); element.setAttribute("name", prop.getName()); parent.addContent(element); } element.setText(((PropertySimple) prop).getStringValue()); } }
public void print(PropertyMap p, int depth) { out.println(indent(depth) + p.getName() + " [" + p.getMap().size() + "] {"); for (String key : p.getMap().keySet()) { Property entry = p.getMap().get(key); if (entry instanceof PropertySimple) { print((PropertySimple) entry, depth + 1); } else if (entry instanceof PropertyMap) { print((PropertyMap) entry, depth + 1); } } out.println(indent(depth) + "}"); }
/** * Look for elements with the name <i>elementName</i> below <i>parent</i> and put them in to * <i>config</i> as a new map with the name <i>mapName</i>. * * @param parent The parent element to search for children * @param config The configuration to put the elements found * @param mapName The name of the map as a key to put into the config * @param elementName The elements to look for */ private static void bindMap( Element parent, Configuration config, String mapName, String elementName) { PropertyMap map = new PropertyMap(mapName); for (Object child : parent.getChildren(elementName)) { Element childElement = (Element) child; String name = childElement.getAttributeValue("name"); map.put(new PropertySimple(name, childElement.getText())); } config.put(map); }
static Map<String, String> getConnectionPropertiesAsMap( PropertyList listPropertyWrapper, String keyName) { if (listPropertyWrapper == null) { return Collections.emptyMap(); } List<Property> propertyList = listPropertyWrapper.getList(); if (propertyList.size() == 0) { return Collections.emptyMap(); } Map<String, String> result = new HashMap<String, String>(propertyList.size()); for (Property p : propertyList) { PropertyMap map = (PropertyMap) p; String key = map.getSimpleValue(keyName, null); String value = map.getSimpleValue("value", null); if (key == null || value == null) { continue; } result.put(key, value); } return result; }
public void loadResourceConfiguration() throws Exception { // Just bail because this test wont work yet if (true) { return; } Configuration configuration; try { configuration = component.loadResourceConfiguration(pluginConfiguration); } catch (UnsatisfiedLinkError ule) { // Skip tests if augeas not available return; } assert configuration != null : "Null configuration returned from load call"; Collection<Property> allProperties = configuration.getProperties(); assert allProperties.size() == 1 : "Incorrect number of properties found. Expected: 1, Found: " + allProperties.size(); PropertyList entryList = (PropertyList) allProperties.iterator().next(); for (Property property : entryList.getList()) { PropertyMap entry = (PropertyMap) property; Property typeProperty = entry.get("type"); Property uriProperty = entry.get("uri"); Property distributionProperty = entry.get("distribution"); assert typeProperty != null : "Type was null in entry"; assert uriProperty != null : "URI was null in entry"; assert distributionProperty != null : "Distribution was null in entry"; log.info("Type: " + ((PropertySimple) typeProperty).getStringValue()); log.info("URI: " + ((PropertySimple) uriProperty).getStringValue()); log.info("Distribution: " + ((PropertySimple) distributionProperty).getStringValue()); } }
public void configurationMembersSwappedAndKeptPositions() { Configuration testConfig = getFreshConfig(); // keep track of the order of the properies in the original map PropertyMap originalMap = (PropertyMap) testConfig.getList("topLevelListOfMaps").getList().get(0); List<Property> originalMapPropertiesInOrder = new ArrayList<Property>(originalMap.getMap().values()); PasswordObfuscationUtility.obfuscatePasswords(DEFINITION, testConfig); PropertyList list = testConfig.getList("topLevelListOfMaps"); PropertyMap map = (PropertyMap) list.getList().get(0); Assert.assertTrue( map.get("password") instanceof ObfuscatedPropertySimple, "The password in the map failed to be swapped to obfuscated form"); // now check that the positions in the map actually didn't change List<Property> mapPropertiesInOrder = new ArrayList<Property>(map.getMap().values()); Assert.assertEquals( mapPropertiesInOrder.size(), originalMapPropertiesInOrder.size(), "Different number of properties in the map after obfuscation"); for (int i = 0; i < mapPropertiesInOrder.size(); ++i) { Property originalProp = originalMapPropertiesInOrder.get(i); Property prop = mapPropertiesInOrder.get(i); Assert.assertEquals( prop.getName(), originalProp.getName(), "Properties seem to be mixed up after obfuscation"); } }
@Override public CreateResourceReport createResource(CreateResourceReport report) { report.setStatus(CreateResourceStatus.INVALID_CONFIGURATION); Address createAddress = new Address(this.address); String path = report.getPluginConfiguration().getSimpleValue("path", ""); String resourceName; if (!path.contains("=")) { // this is not a singleton subsystem // resources like example=test1 and example=test2 can be created resourceName = report.getUserSpecifiedResourceName(); } else { // this is a request to create a true singleton subsystem // both the path and the name are set at resource level configuration resourceName = path.substring(path.indexOf('=') + 1); path = path.substring(0, path.indexOf('=')); } createAddress.add(path, resourceName); Operation op = new Operation("add", createAddress); for (Property prop : report.getResourceConfiguration().getProperties()) { SimpleEntry<String, ?> entry = null; boolean isEntryEligible = true; if (prop instanceof PropertySimple) { PropertySimple propertySimple = (PropertySimple) prop; PropertyDefinitionSimple propertyDefinition = this.configurationDefinition.getPropertyDefinitionSimple(propertySimple.getName()); if (propertyDefinition == null || (!propertyDefinition.isRequired() && propertySimple.getStringValue() == null)) { isEntryEligible = false; } else { entry = preparePropertySimple(propertySimple, propertyDefinition); } } else if (prop instanceof PropertyList) { PropertyList propertyList = (PropertyList) prop; PropertyDefinitionList propertyDefinition = this.configurationDefinition.getPropertyDefinitionList(propertyList.getName()); if (!propertyDefinition.isRequired() && propertyList.getList().size() == 0) { isEntryEligible = false; } else { entry = preparePropertyList(propertyList, propertyDefinition); } } else if (prop instanceof PropertyMap) { PropertyMap propertyMap = (PropertyMap) prop; PropertyDefinitionMap propertyDefinition = this.configurationDefinition.getPropertyDefinitionMap(propertyMap.getName()); if (!propertyDefinition.isRequired() && propertyMap.getMap().size() == 0) { isEntryEligible = false; } else { entry = preparePropertyMap(propertyMap, propertyDefinition); } } if (isEntryEligible) { op.addAdditionalProperty(entry.getKey(), entry.getValue()); } } Result result = this.connection.execute(op); if (result.isSuccess()) { report.setStatus(CreateResourceStatus.SUCCESS); report.setResourceKey(createAddress.getPath()); report.setResourceName(report.getUserSpecifiedResourceName()); } else { report.setStatus(CreateResourceStatus.FAILURE); report.setErrorMessage(result.getFailureDescription()); } return report; }