KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > messaging > DefaultMarshaler


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.jbi.messaging;
18
19 import org.apache.servicemix.jbi.jaxp.StringSource;
20 import org.w3c.dom.Node JavaDoc;
21
22 import javax.jbi.messaging.MessageExchange;
23 import javax.jbi.messaging.MessagingException;
24 import javax.jbi.messaging.NormalizedMessage;
25 import javax.xml.transform.Source JavaDoc;
26 import javax.xml.transform.dom.DOMSource JavaDoc;
27
28 /**
29  * Default implementation of {@link PojoMarshaler} which will pass through String
30  * objects as XML content, DOM objects or Stream objects, otherwise the payload
31  * is stored in a message property.
32  *
33  * @version $Revision: 426415 $
34  */

35 public class DefaultMarshaler implements PojoMarshaler {
36
37     private PojoMarshaler parent;
38
39     public DefaultMarshaler() {
40     }
41
42     public DefaultMarshaler(PojoMarshaler parent) {
43         this.parent = parent;
44     }
45
46     public PojoMarshaler getParent() {
47         return parent;
48     }
49
50     public void marshal(MessageExchange exchange, NormalizedMessage message, Object JavaDoc body) throws MessagingException {
51         if (body instanceof Source) {
52             message.setContent((Source) body);
53         }
54         else {
55             message.setProperty(BODY, body);
56             Source content = asContent(message, body);
57             message.setContent(content);
58         }
59     }
60
61     public Object JavaDoc unmarshal(MessageExchange exchange, NormalizedMessage message) throws MessagingException {
62         Object JavaDoc answer = message.getProperty(BODY);
63         if (answer == null) {
64             if (parent != null) {
65                 answer = parent.unmarshal(exchange, message);
66             }
67             if (answer == null) {
68                 answer = defaultUnmarshal(exchange, message);
69             }
70         }
71         return answer;
72     }
73
74     protected Object JavaDoc defaultUnmarshal(MessageExchange exchange, NormalizedMessage message) {
75         Source content = message.getContent();
76         if (content instanceof DOMSource JavaDoc) {
77             DOMSource JavaDoc source = (DOMSource JavaDoc) content;
78             return source.getNode();
79         }
80         return content;
81     }
82
83     protected Source asContent(NormalizedMessage message, Object JavaDoc body) {
84         if (body instanceof Source) {
85             return (Source) body;
86         }
87         else if (body instanceof String JavaDoc) {
88             // lets assume String is the XML to send
89
return new StringSource((String JavaDoc) body);
90         }
91         else if (body instanceof Node JavaDoc) {
92             return new DOMSource JavaDoc((Node JavaDoc) body);
93         }
94         return null;
95     }
96 }
97
Popular Tags