KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > bus > ws > rm > persistence > PersistenceUtils


1 package org.objectweb.celtix.bus.ws.rm.persistence;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.ObjectInput JavaDoc;
7 import java.io.ObjectInputStream JavaDoc;
8 import java.io.ObjectOutputStream JavaDoc;
9 import java.io.Serializable JavaDoc;
10 import java.util.logging.Level JavaDoc;
11 import java.util.logging.Logger JavaDoc;
12
13 import javax.xml.bind.JAXBContext;
14 import javax.xml.bind.JAXBElement;
15 import javax.xml.bind.JAXBException;
16 import javax.xml.bind.Marshaller;
17 import javax.xml.bind.Unmarshaller;
18 import javax.xml.soap.MessageFactory JavaDoc;
19 import javax.xml.soap.SOAPConstants JavaDoc;
20 import javax.xml.soap.SOAPException JavaDoc;
21 import javax.xml.soap.SOAPMessage JavaDoc;
22 import javax.xml.ws.handler.MessageContext;
23
24 import org.objectweb.celtix.common.logging.LogUtils;
25 import org.objectweb.celtix.context.GenericMessageContext;
26 import org.objectweb.celtix.context.ObjectMessageContext;
27 import org.objectweb.celtix.ws.rm.SequenceAcknowledgement;
28
29 public class PersistenceUtils {
30     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(PersistenceUtils.class);
31     
32     private static final String JavaDoc SOAP_MSG_KEY = "org.objectweb.celtix.bindings.soap.message";
33     private JAXBContext context;
34     private Unmarshaller unmarshaller;
35     private Marshaller marshaller;
36     private MessageFactory JavaDoc msgFactory;
37     
38     
39
40     public InputStream JavaDoc getContextAsInputStream(MessageContext ctx) {
41
42         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
43         try {
44             int nKeys = 0;
45             for (String JavaDoc key : ctx.keySet()) {
46                 Object JavaDoc value = ctx.get(key);
47                 if (isPersistable(key, value)) {
48                     nKeys++;
49                 } else if (LOG.isLoggable(Level.FINE)) {
50                     LOG.fine("Skipping key: " + key + " (value of type "
51                         + value.getClass().getName() + " is not serializable)");
52                 }
53             }
54             
55             ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(bos);
56             
57             oos.writeInt(nKeys);
58             
59             for (String JavaDoc key : ctx.keySet()) {
60                 Object JavaDoc value = ctx.get(key);
61                 if (isPersistable(key, value)) {
62                     oos.writeObject(key);
63                     oos.writeObject(value);
64                 }
65             }
66
67             SOAPMessage JavaDoc msg = (SOAPMessage JavaDoc)ctx.get(SOAP_MSG_KEY);
68             msg.writeTo(bos);
69             
70             bos.close();
71
72         } catch (Exception JavaDoc ex) {
73             throw new RMStoreException(ex);
74         }
75         return new ByteArrayInputStream JavaDoc(bos.toByteArray());
76     }
77     
78     public MessageContext getContext(InputStream JavaDoc is) {
79         MessageContext ctx = new GenericMessageContext();
80         try {
81             ObjectInput JavaDoc oi = new ObjectInputStream JavaDoc(is);
82             int nKeys = oi.readInt();
83             
84             for (int i = 0; i < nKeys; i++) {
85                 String JavaDoc key = (String JavaDoc)oi.readObject();
86                 Object JavaDoc value = oi.readObject();
87                 ctx.put(key, value);
88             }
89             
90             // construct SOAPMessage from input stream
91

92             SOAPMessage JavaDoc msg = getMessageFactory().createMessage(null, is);
93             ctx.put(SOAP_MSG_KEY, msg);
94             
95         } catch (Exception JavaDoc ex) {
96             throw new RMStoreException(ex);
97         }
98         return ctx;
99     }
100
101     public InputStream JavaDoc getAcknowledgementAsInputStream(SequenceAcknowledgement ack) {
102         
103         ByteArrayOutputStream JavaDoc bos = new ByteArrayOutputStream JavaDoc();
104         try {
105             getMarshaller().marshal(ack, bos);
106         } catch (JAXBException ex) {
107             throw new RMStoreException(ex);
108         }
109         return new ByteArrayInputStream JavaDoc(bos.toByteArray());
110     }
111     
112     public SequenceAcknowledgement getSequenceAcknowledgment(InputStream JavaDoc is) {
113         Object JavaDoc obj = null;
114         try {
115             obj = getUnmarshaller().unmarshal(is);
116             if (obj instanceof JAXBElement<?>) {
117                 JAXBElement<?> el = (JAXBElement<?>)obj;
118                 obj = el.getValue();
119             }
120         } catch (JAXBException ex) {
121             throw new RMStoreException(ex);
122         }
123         return (SequenceAcknowledgement)obj;
124     }
125     
126     private JAXBContext getContext() throws JAXBException {
127         if (null == context) {
128             context = JAXBContext.newInstance(SequenceAcknowledgement.class.getPackage().getName(),
129                                               getClass().getClassLoader());
130         }
131         return context;
132     }
133     
134     
135     private Unmarshaller getUnmarshaller() throws JAXBException {
136         if (null == unmarshaller) {
137             unmarshaller = getContext().createUnmarshaller();
138         }
139         return unmarshaller;
140     }
141     
142     private Marshaller getMarshaller() throws JAXBException {
143         if (null == marshaller) {
144             marshaller = getContext().createMarshaller();
145         }
146         return marshaller;
147     }
148     
149     private MessageFactory JavaDoc getMessageFactory() throws SOAPException JavaDoc {
150         if (null == msgFactory) {
151             msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
152         }
153         return msgFactory;
154     }
155     
156     private boolean isPersistable(Object JavaDoc key, Object JavaDoc value) {
157         return value instanceof Serializable JavaDoc
158             && !(ObjectMessageContext.REQUEST_PROXY.equals(key));
159     }
160    
161 }
162
Popular Tags