1 17 package org.apache.servicemix.components.mps; 18 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import javax.jbi.JBIException; 23 import javax.jbi.messaging.NormalizedMessage; 24 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.servicemix.jbi.util.DOMUtil; 28 import org.w3c.dom.Element ; 29 import org.w3c.dom.NodeList ; 30 31 37 public class PropertyValueResolver { 38 39 private final transient Log logger = LogFactory.getLog(getClass()); 40 41 public static final String XML_ELEMENT_NAME = "property"; 42 43 46 private String name; 47 48 53 private String staticValue = null; 54 55 58 private List propertySetTypes = new ArrayList (); 59 60 69 public PropertyValueResolver(String propertyName, Element self) 70 throws ConfigNotSupportedException { 71 this.name = propertyName; 72 addPropertySetTypes(self); 73 } 74 75 81 public void setProperty(NormalizedMessage in, NormalizedMessage out) 82 throws JBIException { 83 84 if (this.staticValue != null) { 85 out.setProperty(name, staticValue); 86 } else { 87 String value = resolveValue(in); 88 if (value != null) { 89 out.setProperty(name, value); 90 } else { 91 logger.warn("Property " + name 92 + " was not set as the value was unresolved"); 93 } 94 } 95 } 96 97 104 private String resolveValue(NormalizedMessage message) throws JBIException { 105 String propValue = null; 109 logger.debug("propvrsize=" + propertySetTypes.size()); 110 for (int i = 0; i < propertySetTypes.size(); i++) { 111 PropertyValue pv = (PropertyValue) propertySetTypes.get(i); 112 propValue = pv.getPropertyValue(message); 113 logger.debug("value from" + pv.getClass() + " = " + propValue); 114 if (propValue != null && !"".equals(propValue)) { 115 break; 116 } 117 if (logger.isDebugEnabled()) { 118 logger.debug(this.name + ": " + pv.getClass() + " was empty"); 119 } 120 } 121 return propValue; 122 } 123 124 135 private void addPropertySetTypes(Element propertyElement) 136 throws ConfigNotSupportedException { 137 138 NodeList propertyValueNodes = propertyElement.getChildNodes(); 139 for (int i = 0; i < propertyValueNodes.getLength(); i++) { 142 if (propertyValueNodes.item(i).getNodeType() != Element.ELEMENT_NODE) { 143 continue; 144 } 145 Element pvElem = (Element ) propertyValueNodes.item(i); 146 PropertyValue pv; 147 if (pvElem.getNodeName().equals( 148 StaticStringPropertyValue.XML_ELEMENT_NAME)) { 149 if (this.propertySetTypes.size() == 0) { 150 this.staticValue = DOMUtil.getElementText(pvElem); 151 } 152 pv = new StaticStringPropertyValue(DOMUtil 153 .getElementText(pvElem)); 154 } else if (pvElem.getNodeName().equals( 155 XPathContentMessagePropertyValue.XML_ELEMENT_NAME)) { 156 String xpath = DOMUtil.getElementText(pvElem); 157 pv = new XPathContentMessagePropertyValue(xpath); 158 if (logger.isDebugEnabled()) { 159 logger.debug("Created an XPath VR :" + xpath); 160 } 161 } else if (pvElem.getNodeName().equals( 162 ExistingPropertyCopier.XML_ELEMENT_NAME)) { 163 166 String propertyName = this.name; 167 if (pvElem.getAttribute("name") != null 168 && !"".equals(pvElem.getAttribute("name"))) { 169 propertyName = pvElem.getAttribute("name"); 173 } 174 pv = new ExistingPropertyCopier(propertyName); 175 } else { 176 throw new ConfigNotSupportedException("Property value type " 177 + pvElem.getNodeName() 178 + " is not supported for the MessagePropertySetter"); 179 } 180 this.propertySetTypes.add(pv); 181 182 } 183 } 184 } 185 | Popular Tags |