Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-2011. Axon Framework
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.axonframework.eventstore;
 
 
 import java.util.List;
Implementation of the serializer that uses XStream as underlying serialization mechanism. Events are serialized to XML. This Serializer wraps a generic XStreamSerializer. In addition to the org.axonframework.serializer.XStreamSerializer, this implementation allows for an upcasting mechanism (see EventUpcaster.

The downside of this implementation compared to the XStreamSerializer is that it is limited to Events.

Author(s):
Allard Buijze
Since:
0.5
 
 public class XStreamEventSerializer implements Serializer<Event> {
 
     private final XStreamSerializer delegate;
     private final List<EventUpcaster<Document>> upcasters = new ArrayList<EventUpcaster<Document>>();

    
Initialize an EventSerializer that uses XStream to serialize Events. The bytes are returned using UTF-8 encoding.
 
     public XStreamEventSerializer() {
          = new XStreamSerializer();
     }

    
Initialize an EventSerializer that uses XStream to serialize Events. The bytes are returned using UTF-8 encoding.

Parameters:
xStream XStream instance to use
 
     public XStreamEventSerializer(XStream xStream) {
          = new XStreamSerializer(xStream);
     }

    
Initialize an EventSerializer that uses XStream to serialize Events. The bytes are returned using thy character set with the given name. If the character set is not supported by the JVM an UnsupportedCharsetException is thrown.

Parameters:
charsetName The name of the character set to use.
 
     public XStreamEventSerializer(String charsetName) {
          = new XStreamSerializer(Charset.forName(charsetName));
     }

    
Initialize an EventSerializer that uses XStream to serialize Events. The bytes are returned using given character set. If the character set is not supported by the JVM an UnsupportedCharsetException is thrown.

Parameters:
charset The character set to use.
 
     public XStreamEventSerializer(Charset charset) {
          = new XStreamSerializer(charset);
     }

    
Initialize an EventSerializer that uses XStream to serialize Events. The bytes are returned using given character set.

Parameters:
charset The character set to use.
providedXStream XStream instance to use
    public XStreamEventSerializer(Charset charsetXStream providedXStream) {
        if (providedXStream != null) {
             = new XStreamSerializer(charsetprovidedXStream);
        } else {
             = new XStreamSerializer(charset);
        }
    }

    
Initializes an XStreamEventSerializer with the given delegate to perform the actual serialization logic.

Parameters:
delegate The (generic) serializer to perform actual serialization with
    public XStreamEventSerializer(XStreamSerializer delegate) {
        this. = delegate;
    }

    
    @Override
    public byte[] serialize(Event event) {
        return .serialize(event);
    }

    
    @Override
    public void serialize(Event objectOutputStream outputStream) {
        .serialize(objectoutputStream);
    }

    
    @Override
    public Event deserialize(byte[] serializedEvent) {
        try {
            return deserialize(new ByteArrayInputStream(serializedEvent));
        } catch (IOException e) {
            throw new SerializationException(
                    "The impossible has happened: a ByteArrayInputStream throwing an IO Exception"e);
        }
    }

    
    @Override
    public Event deserialize(InputStream inputStreamthrows IOException {
        if (.isEmpty()) {
            return (DomainEvent.deserialize(inputStream);
        } else {
            Document document = readDocument(inputStream);
            for (EventUpcaster<Documentupcaster : ) {
                document = upcaster.upcast(document);
            }
            return (Event.deserialize(new Dom4JReader(document));
        }
    }

    
Reads the given serializedEvent into a Dom4J document. The default implementation uses a StAX reader.

This method can be safely overridden to alter the deserialization mechanism. Make sure to use the correct charset (using getCharset()) when converting characters to bytes and vice versa.

Parameters:
serializedEvent The bytes containing the serialized event
Returns:
a Dom4J Document representation of the event
    protected Document readDocument(byte[] serializedEvent) {
        return readDocument(new ByteArrayInputStream(serializedEvent));
    }


    
Reads the event from the given stream into a Dom4J document. The default implementation uses a StAX reader.

This method can be safely overridden to alter the deserialization mechanism. Make sure to use the correct charset (using getCharset()) when converting characters to bytes and vice versa.

Parameters:
stream The stream containing the serialized event
Returns:
a Dom4J Document representation of the event
    protected Document readDocument(InputStream stream) {
        try {
            STAXEventReader reader = new STAXEventReader();
            return reader.readDocument(new InputStreamReader(stream.getCharset()));
        } catch (XMLStreamException e) {
            throw new SerializationException("Exception while preprocessing events"e);
        }
    }

    
Adds an alias to use instead of the fully qualified class name.

Parameters:
name The alias to use
type The Class to use the alias for
See also:
com.thoughtworks.xstream.XStream.alias(java.lang.String,java.lang.Class)
    public void addAlias(String nameClass type) {
        .addAlias(nametype);
    }

    
Add an alias for a package. This allows long package names to be shortened considerably. Will also use the alias for subpackages of the provided package.

E.g. an alias of "axoncore" for the package "org.axonframework.core" will use "axoncore.repository" for the package "org.axonframework.core.repository".

Parameters:
alias The alias to use.
pkgName The package to use the alias for
See also:
com.thoughtworks.xstream.XStream.aliasPackage(java.lang.String,java.lang.String)
    public void addPackageAlias(String aliasString pkgName) {
        .addPackageAlias(aliaspkgName);
    }

    
Adds an alias to use for a given field in the given class.

Parameters:
alias The alias to use instead of the original field name
definedIn The class that defines the field.
fieldName The name of the field to use the alias for
See also:
com.thoughtworks.xstream.XStream.aliasField(java.lang.String,java.lang.Class,java.lang.String)
    public void addFieldAlias(String aliasClass definedInString fieldName) {
        .addFieldAlias(aliasdefinedInfieldName);
    }

    
Returns a reference to the underlying com.thoughtworks.xstream.XStream instance, that does the actual serialization.

Returns:
the XStream instance that does the actual (de)serialization.
See also:
com.thoughtworks.xstream.XStream
    public XStream getXStream() {
        return .getXStream();
    }

    
Returns the character set used to serialize XML to bytes and vice versa.

Returns:
the character set used to serialize XML to bytes and vice versa
    public Charset getCharset() {
        return .getCharset();
    }

    
Sets the event upcasters the serializer may use. Note that this serializer only supports the dom4j Document representation of upcasters. This means they should all implement EventUpcaster<Document>.

Parameters:
eventUpcasters The upcasters to assign to this serializer
    public void setEventUpcasters(List<EventUpcaster<Document>> eventUpcasters) {
        assertSupportDom4jDocument(eventUpcasters);
        this..clear();
        this..addAll(eventUpcasters);
    }
    private void assertSupportDom4jDocument(List<EventUpcaster<Document>> eventUpcasters) {
        for (EventUpcaster<Documentupcaster : eventUpcasters) {
            if (!upcaster.getSupportedRepresentation().isAssignableFrom(Document.class)) {
                throw new AxonConfigurationException(String.format(
                        "The given upcaster [%s] does not support the dom4j Document representation",
                        upcaster.getClass().getSimpleName()));
            }
        }
    }
New to GrepCode? Check out our FAQ X