001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.ws.rm.persistence;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023:
024: import javax.xml.bind.JAXBContext;
025: import javax.xml.bind.JAXBElement;
026: import javax.xml.bind.JAXBException;
027: import javax.xml.bind.Marshaller;
028: import javax.xml.bind.Unmarshaller;
029:
030: import org.apache.cxf.common.util.PackageUtils;
031: import org.apache.cxf.ws.rm.SequenceAcknowledgement;
032:
033: /**
034: *
035: */
036: public final class PersistenceUtils {
037:
038: private static PersistenceUtils instance;
039: private JAXBContext context;
040: private Unmarshaller unmarshaller;
041: private Marshaller marshaller;
042:
043: /**
044: * Prevents instantiation.
045: */
046: private PersistenceUtils() {
047: }
048:
049: public static PersistenceUtils getInstance() {
050: if (null == instance) {
051: instance = new PersistenceUtils();
052: }
053: return instance;
054: }
055:
056: public SequenceAcknowledgement deserialiseAcknowledgment(
057: InputStream is) {
058: Object obj = null;
059: try {
060: obj = getUnmarshaller().unmarshal(is);
061: if (obj instanceof JAXBElement<?>) {
062: JAXBElement<?> el = (JAXBElement<?>) obj;
063: obj = el.getValue();
064: }
065: } catch (JAXBException ex) {
066: throw new RMStoreException(ex);
067: }
068: return (SequenceAcknowledgement) obj;
069: }
070:
071: public InputStream serialiseAcknowledgment(
072: SequenceAcknowledgement ack) {
073: ByteArrayOutputStream bos = new ByteArrayOutputStream();
074: try {
075: getMarshaller().marshal(ack, bos);
076: } catch (JAXBException ex) {
077: throw new RMStoreException(ex);
078: }
079: return new ByteArrayInputStream(bos.toByteArray());
080: }
081:
082: private JAXBContext getContext() throws JAXBException {
083: if (null == context) {
084: context = JAXBContext.newInstance(PackageUtils
085: .getPackageName(SequenceAcknowledgement.class),
086: getClass().getClassLoader());
087: }
088: return context;
089: }
090:
091: private Unmarshaller getUnmarshaller() throws JAXBException {
092: if (null == unmarshaller) {
093: unmarshaller = getContext().createUnmarshaller();
094: }
095: return unmarshaller;
096: }
097:
098: private Marshaller getMarshaller() throws JAXBException {
099: if (null == marshaller) {
100: marshaller = getContext().createMarshaller();
101: }
102: return marshaller;
103: }
104: }
|