1 17 package org.apache.servicemix.components.mps; 18 19 import java.io.IOException ; 20 import java.util.HashMap ; 21 import java.util.Map ; 22 23 import javax.jbi.JBIException; 24 import javax.jbi.component.ComponentContext; 25 import javax.jbi.messaging.MessageExchange; 26 import javax.jbi.messaging.MessagingException; 27 import javax.jbi.messaging.NormalizedMessage; 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 import javax.xml.transform.TransformerException ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.servicemix.components.util.TransformComponentSupport; 36 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 37 import org.apache.xpath.CachedXPathAPI; 38 import org.apache.xpath.objects.XObject; 39 import org.springframework.core.io.Resource; 40 import org.springframework.util.Assert; 41 import org.w3c.dom.Document ; 42 import org.w3c.dom.Element ; 43 import org.w3c.dom.Node ; 44 import org.xml.sax.SAXException ; 45 46 47 48 128 public class MessagePropertySetterXML extends TransformComponentSupport { 129 130 133 private final transient Log logger = LogFactory.getLog(getClass()); 134 135 139 public static final String MPS_PROP_NAME_PROPERTYSET = "org.apache.servicemix.components.mps.propertyset"; 140 141 144 public static final String XML_ELEMENT_NAME = "property-set"; 145 146 149 private Resource xmlConfiguration = null; 150 151 154 private Document xmlMPSdom = null; 155 156 163 private Map propertSets = new HashMap (); 164 165 174 private String propertySet = null; 175 176 177 183 private String xpathForPropertySet = null; 184 185 191 protected boolean transform(MessageExchange arg0, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 192 try { 193 copyPropertiesAndAttachments(arg0,in,out); 194 out.setContent(in.getContent()); 195 196 String propertySetName = ""; 197 if (xpathForPropertySet != null) { 198 try { 199 CachedXPathAPI xpathApi = new org.apache.xpath.CachedXPathAPI(); 200 Document doc = new SourceTransformer().toDOMDocument(in); 201 XObject propSetXO = xpathApi.eval(doc.getDocumentElement(),xpathForPropertySet); 202 propertySetName = propSetXO.str(); 203 } catch (Exception e) { 204 throw new MessagingException("Problem getting the propertySet using XPath", e); 205 } 206 } else if (this.propertySet != null) { 207 propertySetName = this.propertySet; 208 } else if (in.getProperty(MPS_PROP_NAME_PROPERTYSET) != null) { 209 propertySetName = in.getProperty(MPS_PROP_NAME_PROPERTYSET).toString(); 210 } else { 211 return false; 212 } 213 logger.info("Applying properties from property-set [" + propertySetName + "]"); 214 getPropertySetByName(propertySetName).applyProperties(in,out); 215 return true; 216 } catch (JBIException e) { 217 throw new MessagingException("Problem setting properties",e); 218 } catch (PropertySetNotFoundException e) { 219 logger.warn(e.getLocalizedMessage()); 220 return false; 221 } 222 } 223 224 229 private void initConfig() throws JBIException { 230 Assert.notNull(this.xmlConfiguration); 231 DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); 232 domFactory.setIgnoringElementContentWhitespace(true); 233 domFactory.setIgnoringComments(true); 234 domFactory.setCoalescing(true); DocumentBuilder domBuilder; 236 try { 237 logger.info("Intialising MessagePropertySetterXML, loading settings from " + this.xmlConfiguration.getFile().getAbsolutePath()); 238 domBuilder = domFactory.newDocumentBuilder(); 239 xmlMPSdom = domBuilder.parse(this.xmlConfiguration.getInputStream()); 240 } catch (ParserConfigurationException e) { 241 throw new JBIException("Problem parsing the XML config file for MPS",e); 242 } catch (SAXException e) { 243 throw new JBIException("Problem loading the XML config file for MPS",e); 244 } catch (IOException e) { 245 throw new JBIException("Problem loading the XML config file for MPS",e); 246 } 247 248 } 249 250 253 public void init(ComponentContext context) throws JBIException { 254 super.init(context); 255 initConfig(); 256 } 257 258 266 private PropertySet createPropertySet(String propertySetName) throws JBIException , PropertySetNotFoundException{ 267 PropertySet ps; 268 CachedXPathAPI xpath = new CachedXPathAPI(); 269 StringBuffer xpathSB = new StringBuffer ("//") 270 .append(XML_ELEMENT_NAME) 271 .append("[@name='") 272 .append(propertySetName) 273 .append("']"); 274 try { 275 Node propertySetNode = xpath.selectSingleNode(xmlMPSdom,xpathSB.toString()); 276 if (propertySetNode == null) { 277 throw new PropertySetNotFoundException("Could not find a property-set for [" + propertySetName + "] in " + xmlConfiguration.getFilename()); 278 } 279 ps = new PropertySet(propertySetName,(Element ) propertySetNode); 280 this.propertSets.put(propertySetName, ps); 281 } catch (TransformerException e) { 282 throw new JBIException("Could not load the PropertySet for " + propertySet,e); 283 } catch (ConfigNotSupportedException e) { 284 throw new JBIException("Could not load the PropertySet for. XMLConfig is not good for " + propertySet,e); 285 } 286 return ps; 287 288 } 289 290 296 private PropertySet getPropertySetByName(String name) throws JBIException, PropertySetNotFoundException { 297 if (this.propertSets.containsKey(name)) { 299 return (PropertySet)this.propertSets.get(name); 300 } else { 301 return this.createPropertySet(name); 302 } 303 304 } 305 306 309 public void setPropertySet(String propertySet) { 310 this.propertySet = propertySet; 311 } 312 313 316 public void setXmlConfiguration(Resource xmlConfiguration) { 317 this.xmlConfiguration = xmlConfiguration; 318 } 319 320 323 public void setXpathForPropertySet(String xpathForPropertySet) { 324 this.xpathForPropertySet = xpathForPropertySet; 325 } 326 327 } 328 | Popular Tags |