KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > wsn > client > JAXBMarshaler


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.wsn.client;
18
19 import java.io.StringWriter JavaDoc;
20
21 import javax.jbi.messaging.MessageExchange;
22 import javax.jbi.messaging.MessagingException;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.xml.bind.JAXBContext;
25 import javax.xml.transform.Source JavaDoc;
26
27 import org.apache.servicemix.jbi.jaxp.StringSource;
28 import org.apache.servicemix.jbi.messaging.DefaultMarshaler;
29
30 public class JAXBMarshaler extends DefaultMarshaler {
31
32     private JAXBContext context;
33     
34     public JAXBMarshaler(JAXBContext context) {
35         this.context = context;
36     }
37
38     public JAXBContext getContext() {
39         return context;
40     }
41
42     public void setContext(JAXBContext context) {
43         this.context = context;
44     }
45     
46     protected Object JavaDoc defaultUnmarshal(MessageExchange exchange, NormalizedMessage message) {
47         try {
48             Source JavaDoc content = message.getContent();
49             return context.createUnmarshaller().unmarshal(content);
50         } catch (Exception JavaDoc e) {
51             throw new RuntimeException JavaDoc(e);
52         }
53     }
54
55     protected Source JavaDoc asContent(NormalizedMessage message, Object JavaDoc body) {
56         try {
57             StringWriter JavaDoc writer = new StringWriter JavaDoc();
58             context.createMarshaller().marshal(body, writer);
59             return new StringSource(writer.toString());
60         } catch (Exception JavaDoc e) {
61             throw new RuntimeException JavaDoc(e);
62         }
63     }
64
65     @Override JavaDoc
66     public void marshal(MessageExchange exchange, NormalizedMessage message, Object JavaDoc body) throws MessagingException {
67         if (body instanceof Source JavaDoc) {
68             message.setContent((Source JavaDoc) body);
69         }
70         else {
71             Source JavaDoc content = asContent(message, body);
72             message.setContent(content);
73         }
74     }
75 }
76
Popular Tags