001: package org.objectweb.celtix.bus.ws.rm.persistence;
002:
003: import java.io.ByteArrayInputStream;
004: import java.io.ByteArrayOutputStream;
005: import java.io.InputStream;
006: import java.io.ObjectInput;
007: import java.io.ObjectInputStream;
008: import java.io.ObjectOutputStream;
009: import java.io.Serializable;
010: import java.util.logging.Level;
011: import java.util.logging.Logger;
012:
013: import javax.xml.bind.JAXBContext;
014: import javax.xml.bind.JAXBElement;
015: import javax.xml.bind.JAXBException;
016: import javax.xml.bind.Marshaller;
017: import javax.xml.bind.Unmarshaller;
018: import javax.xml.soap.MessageFactory;
019: import javax.xml.soap.SOAPConstants;
020: import javax.xml.soap.SOAPException;
021: import javax.xml.soap.SOAPMessage;
022: import javax.xml.ws.handler.MessageContext;
023:
024: import org.objectweb.celtix.common.logging.LogUtils;
025: import org.objectweb.celtix.context.GenericMessageContext;
026: import org.objectweb.celtix.context.ObjectMessageContext;
027: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement;
028:
029: public class PersistenceUtils {
030: private static final Logger LOG = LogUtils
031: .getL7dLogger(PersistenceUtils.class);
032:
033: private static final String SOAP_MSG_KEY = "org.objectweb.celtix.bindings.soap.message";
034: private JAXBContext context;
035: private Unmarshaller unmarshaller;
036: private Marshaller marshaller;
037: private MessageFactory msgFactory;
038:
039: public InputStream getContextAsInputStream(MessageContext ctx) {
040:
041: ByteArrayOutputStream bos = new ByteArrayOutputStream();
042: try {
043: int nKeys = 0;
044: for (String key : ctx.keySet()) {
045: Object value = ctx.get(key);
046: if (isPersistable(key, value)) {
047: nKeys++;
048: } else if (LOG.isLoggable(Level.FINE)) {
049: LOG.fine("Skipping key: " + key
050: + " (value of type "
051: + value.getClass().getName()
052: + " is not serializable)");
053: }
054: }
055:
056: ObjectOutputStream oos = new ObjectOutputStream(bos);
057:
058: oos.writeInt(nKeys);
059:
060: for (String key : ctx.keySet()) {
061: Object value = ctx.get(key);
062: if (isPersistable(key, value)) {
063: oos.writeObject(key);
064: oos.writeObject(value);
065: }
066: }
067:
068: SOAPMessage msg = (SOAPMessage) ctx.get(SOAP_MSG_KEY);
069: msg.writeTo(bos);
070:
071: bos.close();
072:
073: } catch (Exception ex) {
074: throw new RMStoreException(ex);
075: }
076: return new ByteArrayInputStream(bos.toByteArray());
077: }
078:
079: public MessageContext getContext(InputStream is) {
080: MessageContext ctx = new GenericMessageContext();
081: try {
082: ObjectInput oi = new ObjectInputStream(is);
083: int nKeys = oi.readInt();
084:
085: for (int i = 0; i < nKeys; i++) {
086: String key = (String) oi.readObject();
087: Object value = oi.readObject();
088: ctx.put(key, value);
089: }
090:
091: // construct SOAPMessage from input stream
092:
093: SOAPMessage msg = getMessageFactory().createMessage(null,
094: is);
095: ctx.put(SOAP_MSG_KEY, msg);
096:
097: } catch (Exception ex) {
098: throw new RMStoreException(ex);
099: }
100: return ctx;
101: }
102:
103: public InputStream getAcknowledgementAsInputStream(
104: SequenceAcknowledgement ack) {
105:
106: ByteArrayOutputStream bos = new ByteArrayOutputStream();
107: try {
108: getMarshaller().marshal(ack, bos);
109: } catch (JAXBException ex) {
110: throw new RMStoreException(ex);
111: }
112: return new ByteArrayInputStream(bos.toByteArray());
113: }
114:
115: public SequenceAcknowledgement getSequenceAcknowledgment(
116: InputStream is) {
117: Object obj = null;
118: try {
119: obj = getUnmarshaller().unmarshal(is);
120: if (obj instanceof JAXBElement<?>) {
121: JAXBElement<?> el = (JAXBElement<?>) obj;
122: obj = el.getValue();
123: }
124: } catch (JAXBException ex) {
125: throw new RMStoreException(ex);
126: }
127: return (SequenceAcknowledgement) obj;
128: }
129:
130: private JAXBContext getContext() throws JAXBException {
131: if (null == context) {
132: context = JAXBContext.newInstance(
133: SequenceAcknowledgement.class.getPackage()
134: .getName(), getClass().getClassLoader());
135: }
136: return context;
137: }
138:
139: private Unmarshaller getUnmarshaller() throws JAXBException {
140: if (null == unmarshaller) {
141: unmarshaller = getContext().createUnmarshaller();
142: }
143: return unmarshaller;
144: }
145:
146: private Marshaller getMarshaller() throws JAXBException {
147: if (null == marshaller) {
148: marshaller = getContext().createMarshaller();
149: }
150: return marshaller;
151: }
152:
153: private MessageFactory getMessageFactory() throws SOAPException {
154: if (null == msgFactory) {
155: msgFactory = MessageFactory
156: .newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
157: }
158: return msgFactory;
159: }
160:
161: private boolean isPersistable(Object key, Object value) {
162: return value instanceof Serializable
163: && !(ObjectMessageContext.REQUEST_PROXY.equals(key));
164: }
165:
166: }
|