KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > groovy > GroovyComponent


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
28 import javax.xml.parsers.DocumentBuilder JavaDoc;
29 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
30 import javax.xml.parsers.ParserConfigurationException JavaDoc;
31
32 /**
33  * A component which is capable of invoking a <a HREF="http://groovy.codehaus.org/">Groovy</a> script to process
34  * or transform a message.
35  *
36  * @version $Revision: 426415 $
37  */

38 public class GroovyComponent extends ScriptComponent {
39
40     private DocumentBuilderFactory JavaDoc documentBuilderFactory;
41     private DocumentBuilder JavaDoc documentBuilder;
42
43     public GroovyComponent() {
44         setScriptEngineName("groovy");
45     }
46
47     public GroovyComponent(QName JavaDoc service, String JavaDoc endpoint) {
48         super(service, endpoint);
49         setScriptEngineName("groovy");
50     }
51
52     public DocumentBuilder JavaDoc getDocumentBuilder() throws ParserConfigurationException JavaDoc {
53         if (documentBuilder == null) {
54             documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
55         }
56         return documentBuilder;
57     }
58
59     public void setDocumentBuilder(DocumentBuilder JavaDoc documentBuilder) {
60         this.documentBuilder = documentBuilder;
61     }
62
63     public DocumentBuilderFactory JavaDoc getDocumentBuilderFactory() {
64         if (documentBuilderFactory == null) {
65             documentBuilderFactory = DocumentBuilderFactory.newInstance();
66         }
67         return documentBuilderFactory;
68     }
69
70     public void setDocumentBuilderFactory(DocumentBuilderFactory JavaDoc 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             // lets output a builder
79
DocumentBuilder JavaDoc documentBuilder = getDocumentBuilder();
80             namespace.put("builder", new DOMBuilder(documentBuilder));
81         }
82         catch (ParserConfigurationException JavaDoc e) {
83             throw new MessagingException("Failed to create DOM DocumentBuilder: " + e, e);
84         }
85     }
86
87 }
88
Popular Tags