1 17 package org.apache.servicemix.components.groovy; 18 19 import groovy.xml.DOMBuilder; 20 21 import org.apache.servicemix.components.script.ScriptComponent; 22 23 import javax.jbi.messaging.MessageExchange; 24 import javax.jbi.messaging.MessagingException; 25 import javax.jbi.messaging.NormalizedMessage; 26 import javax.script.Namespace; 27 import javax.xml.namespace.QName ; 28 import javax.xml.parsers.DocumentBuilder ; 29 import javax.xml.parsers.DocumentBuilderFactory ; 30 import javax.xml.parsers.ParserConfigurationException ; 31 32 38 public class GroovyComponent extends ScriptComponent { 39 40 private DocumentBuilderFactory documentBuilderFactory; 41 private DocumentBuilder documentBuilder; 42 43 public GroovyComponent() { 44 setScriptEngineName("groovy"); 45 } 46 47 public GroovyComponent(QName service, String endpoint) { 48 super(service, endpoint); 49 setScriptEngineName("groovy"); 50 } 51 52 public DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { 53 if (documentBuilder == null) { 54 documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); 55 } 56 return documentBuilder; 57 } 58 59 public void setDocumentBuilder(DocumentBuilder documentBuilder) { 60 this.documentBuilder = documentBuilder; 61 } 62 63 public DocumentBuilderFactory getDocumentBuilderFactory() { 64 if (documentBuilderFactory == null) { 65 documentBuilderFactory = DocumentBuilderFactory.newInstance(); 66 } 67 return documentBuilderFactory; 68 } 69 70 public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory) { 71 this.documentBuilderFactory = documentBuilderFactory; 72 } 73 74 protected void populateNamespace(Namespace namespace, MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException { 75 try { 76 super.populateNamespace(namespace, exchange, in, out); 77 78 DocumentBuilder documentBuilder = getDocumentBuilder(); 80 namespace.put("builder", new DOMBuilder(documentBuilder)); 81 } 82 catch (ParserConfigurationException e) { 83 throw new MessagingException("Failed to create DOM DocumentBuilder: " + e, e); 84 } 85 } 86 87 } 88 | Popular Tags |