private Object convertValue(String value, String type) { if (type == null) { // If no type is set we simply return the string value return value; } try { XMLConfigConstants.DATA_TYPES dataType = XMLConfigConstants.DATA_TYPES.valueOf(type); switch (dataType) { case BOOLEAN : return JavaUtils.isTrueExplicitly(value); case DOUBLE : return Double.parseDouble(value); case FLOAT : return Float.parseFloat(value); case INTEGER : return Integer.parseInt(value); case LONG : return Long.parseLong(value); case OM : return buildOMElement(value); case SHORT : return Short.parseShort(value); default : return value; } } catch (IllegalArgumentException e) { String msg = "Unknown type : " + type + " for the property mediator or the " + "property value cannot be converted into the specified type."; log.error(msg, e); throw new SynapseException(msg, e); } }
public void testTypeAwarePropertyHandling() throws Exception { PropertyMediator propMediatorOne = new PropertyMediator(); propMediatorOne.setName("nameOne"); propMediatorOne.setValue("valueOne", XMLConfigConstants.DATA_TYPES.STRING.name()); propMediatorTwo.setValue("25000", XMLConfigConstants.DATA_TYPES.INTEGER.name()); propMediatorTwo.setScope(XMLConfigConstants.SCOPE_AXIS2); propMediatorThree.setValue("123.456", XMLConfigConstants.DATA_TYPES.DOUBLE.name()); propMediatorThree.setScope(XMLConfigConstants.SCOPE_TRANSPORT); propMediatorFour.setValue("true", XMLConfigConstants.DATA_TYPES.BOOLEAN.name()); propMediatorFive.setValue("123456", XMLConfigConstants.DATA_TYPES.LONG.name()); propMediatorFive.setScope(XMLConfigConstants.SCOPE_AXIS2); propMediatorSix.setValue("12345", XMLConfigConstants.DATA_TYPES.SHORT.name()); propMediatorSix.setScope(XMLConfigConstants.SCOPE_TRANSPORT); propMediatorSeven.setValue("123.456", XMLConfigConstants.DATA_TYPES.FLOAT.name());
public void testXMLPropertyHandling() throws Exception { PropertyMediator propMediatorOne = new PropertyMediator(); propMediatorOne.setName("nameOne"); String xml = "<Project><name>Synapse</name></Project>"; OMElement valueOne = TestUtils.createOMElement(xml); propMediatorOne.setValueElement(valueOne); // Test setting XML properties MessageContext synCtx = TestUtils.getTestContext("<getQuote><symbol>IBM</symbol></getQuote>"); propMediatorOne.mediate(synCtx); Object prop = synCtx.getProperty("nameOne"); assertEquals(valueOne.toString(), prop.toString()); // Objects are not equal, so need to compare the content // Test XML property retreival String exprValue = new SynapseXPath("synapse:get-property('nameOne')").stringValueOf(synCtx); assertEquals(xml, exprValue); // Test property removal propMediatorOne.setAction(PropertyMediator.ACTION_REMOVE); propMediatorOne.mediate(synCtx); assertNull(synCtx.getProperty("nameOne")); // Setting XML properties using expressions synCtx.setProperty("nameOne", xml); PropertyMediator propertyMediatorTwo = new PropertyMediator(); propertyMediatorTwo.setName("nameTwo"); propertyMediatorTwo.setExpression(new SynapseXPath("synapse:get-property('nameOne')"), XMLConfigConstants.DATA_TYPES.OM.name()); propertyMediatorTwo.mediate(synCtx); Object exprProp = synCtx.getProperty("nameTwo"); assertTrue(exprProp != null && exprProp instanceof OMElement); }
private Object convertValue(String value, String type) { if (type == null) { // If no type is set we simply return the string value return value; } try { XMLConfigConstants.DATA_TYPES dataType = XMLConfigConstants.DATA_TYPES.valueOf(type); switch (dataType) { case BOOLEAN : return JavaUtils.isTrueExplicitly(value); case DOUBLE : return Double.parseDouble(value); case FLOAT : return Float.parseFloat(value); case INTEGER : return Integer.parseInt(value); case LONG : return Long.parseLong(value); case OM : return SynapseConfigUtils.stringToOM(value); case SHORT : return Short.parseShort(value); default : return value; } } catch (IllegalArgumentException e) { String msg = "Unknown type : " + type + " for the property mediator or the " + "property value cannot be converted into the specified type."; log.error(msg, e); throw new SynapseException(msg, e); } }