ingo@100: /* ingo@100: * Copyright (c) 2010 by Intevation GmbH ingo@100: * ingo@100: * This program is free software under the LGPL (>=v2.1) ingo@100: * Read the file LGPL.txt coming with the software for details ingo@100: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@100: */ ingo@100: sascha@41: package de.intevation.artifactdatabase; sascha@41: sascha@41: import de.intevation.artifacts.Artifact; sascha@41: import de.intevation.artifacts.ArtifactSerializer; sascha@41: sascha@93: import java.io.ByteArrayInputStream; sascha@93: import java.io.ByteArrayOutputStream; sascha@93: import java.io.IOException; sascha@41: import java.io.InputStream; sascha@41: import java.io.ObjectInputStream; sascha@41: import java.io.ObjectOutputStream; sascha@93: import java.io.OutputStream; sascha@41: sascha@93: import java.util.zip.GZIPInputStream; sascha@41: import java.util.zip.GZIPOutputStream; sascha@41: sascha@41: import org.apache.log4j.Logger; sascha@41: sascha@41: /** sascha@87: * Default implementation of the ArtifactSerializer interface. sascha@87: * It uses serialized Java objects which are gzipped and sascha@87: * turned into bytes. sascha@87: * sascha@77: * @author Sascha L. Teichmann sascha@41: */ sascha@41: public class DefaultArtifactSerializer sascha@41: implements ArtifactSerializer sascha@41: { sascha@41: private static Logger logger = sascha@41: Logger.getLogger(DefaultArtifactSerializer.class); sascha@41: sascha@87: /** sascha@87: * Static instance to avoid repeated creation of Serializers. sascha@87: */ sascha@41: public static final ArtifactSerializer INSTANCE = sascha@41: new DefaultArtifactSerializer(); sascha@41: sascha@87: /** sascha@87: * Default constructor. sascha@87: */ sascha@41: public DefaultArtifactSerializer() { sascha@41: } sascha@41: sascha@41: public Artifact fromBytes(byte [] bytes) { sascha@41: sascha@41: if (bytes == null) { sascha@41: return null; sascha@41: } sascha@41: sascha@41: ObjectInputStream ois = null; sascha@41: sascha@41: try { sascha@41: ByteArrayInputStream bis = new ByteArrayInputStream(bytes); sascha@41: GZIPInputStream gis = new GZIPInputStream(bis); sascha@41: ois = getObjectInputStream(gis); sascha@41: sascha@41: return (Artifact)ois.readObject(); sascha@41: } sascha@41: catch (IOException ioe) { sascha@41: logger.error(ioe.getLocalizedMessage(), ioe); sascha@41: } sascha@41: catch (ClassNotFoundException cnfe) { sascha@41: logger.error(cnfe.getLocalizedMessage(), cnfe); sascha@41: } sascha@41: catch (ClassCastException cce) { sascha@41: logger.error(cce.getLocalizedMessage(), cce); sascha@41: } sascha@41: finally { sascha@41: if (ois != null) { sascha@41: try { ois.close(); } sascha@41: catch (IOException ioe) { } sascha@41: } sascha@41: } sascha@41: sascha@41: return null; sascha@41: } sascha@41: sascha@41: public byte [] toBytes(Artifact artifact) { sascha@41: try { sascha@41: ByteArrayOutputStream bos = new ByteArrayOutputStream(); sascha@41: GZIPOutputStream gos = new GZIPOutputStream(bos); sascha@41: ObjectOutputStream oos = getObjectOutputStream(gos); sascha@41: sascha@41: oos.writeObject(artifact); sascha@41: oos.flush(); sascha@41: oos.close(); sascha@41: sascha@41: return bos.toByteArray(); sascha@41: } sascha@41: catch (IOException ioe) { sascha@41: logger.error(ioe.getLocalizedMessage(), ioe); sascha@41: throw new RuntimeException(ioe); sascha@41: } sascha@41: } sascha@41: sascha@87: /** sascha@87: * Wraps an input stream into an object input stream. You may sascha@87: * overwrite this to get a more specialized deserializer. sascha@87: * @param is The raw input stream sascha@87: * @return An instance of a subclass of ObjectInputStream. sascha@87: * @throws IOException Thrown if something went wrong during sascha@87: * creation of the object input stream. sascha@87: */ sascha@41: protected ObjectInputStream getObjectInputStream(InputStream is) sascha@41: throws IOException sascha@41: { sascha@41: return new ObjectInputStream(is); sascha@41: } sascha@41: sascha@87: /** sascha@87: * Wraps an output stream into an object output stream. You may sascha@87: * overwrite this to get a more specialized serializer. sascha@87: * @param os the raw output stream. sascha@87: * @return An instance of a subclass of ObjectOutputStream. sascha@87: * @throws IOException Thrown if something went wrong during sascha@87: * creation of the object output stream. sascha@87: */ sascha@41: protected ObjectOutputStream getObjectOutputStream(OutputStream os) sascha@41: throws IOException sascha@41: { sascha@41: return new ObjectOutputStream(os); sascha@41: } sascha@41: } sascha@87: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :