annotate artifact-database/src/main/java/de/intevation/artifactdatabase/DefaultArtifactSerializer.java @ 89:d348fe1fd822

More javadoc (fixes small glitches, too). artifacts/trunk@845 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 26 Mar 2010 16:16:32 +0000
parents 0f48188a6e02
children e27cf9c84eb8
rev   line source
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.artifactdatabase;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import de.intevation.artifacts.Artifact;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4 import de.intevation.artifacts.ArtifactSerializer;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import java.io.InputStream;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7 import java.io.IOException;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 import java.io.OutputStream;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import java.io.ObjectInputStream;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10 import java.io.ObjectOutputStream;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
11 import java.io.ByteArrayOutputStream;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
12 import java.io.ByteArrayInputStream;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import java.util.zip.GZIPOutputStream;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 import java.util.zip.GZIPInputStream;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import org.apache.log4j.Logger;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 /**
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
20 * Default implementation of the ArtifactSerializer interface.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
21 * It uses serialized Java objects which are gzipped and
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
22 * turned into bytes.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
23 *
77
48d1a9a082c2 Bring @author javadoc tags in form '@author <a href="john.doe@example.com">John Doe</a>'
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 41
diff changeset
24 * @author <a href="mailto:sascha.teichmann@intevation.de">Sascha L. Teichmann</a>
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 */
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26 public class DefaultArtifactSerializer
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 implements ArtifactSerializer
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
28 {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 private static Logger logger =
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
30 Logger.getLogger(DefaultArtifactSerializer.class);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
31
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
32 /**
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
33 * Static instance to avoid repeated creation of Serializers.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
34 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35 public static final ArtifactSerializer INSTANCE =
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
36 new DefaultArtifactSerializer();
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
37
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
38 /**
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
39 * Default constructor.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
40 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
41 public DefaultArtifactSerializer() {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
42 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
43
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
44 public Artifact fromBytes(byte [] bytes) {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
45
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 if (bytes == null) {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47 return null;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
48 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50 ObjectInputStream ois = null;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 try {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54 GZIPInputStream gis = new GZIPInputStream(bis);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 ois = getObjectInputStream(gis);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 return (Artifact)ois.readObject();
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 catch (IOException ioe) {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 logger.error(ioe.getLocalizedMessage(), ioe);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 catch (ClassNotFoundException cnfe) {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 logger.error(cnfe.getLocalizedMessage(), cnfe);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 catch (ClassCastException cce) {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 logger.error(cce.getLocalizedMessage(), cce);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 finally {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69 if (ois != null) {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 try { ois.close(); }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 catch (IOException ioe) { }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 return null;
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 public byte [] toBytes(Artifact artifact) {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 try {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 ByteArrayOutputStream bos = new ByteArrayOutputStream();
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81 GZIPOutputStream gos = new GZIPOutputStream(bos);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 ObjectOutputStream oos = getObjectOutputStream(gos);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 oos.writeObject(artifact);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 oos.flush();
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 oos.close();
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
87
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
88 return bos.toByteArray();
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
89 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
90 catch (IOException ioe) {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
91 logger.error(ioe.getLocalizedMessage(), ioe);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
92 throw new RuntimeException(ioe);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
93 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
94 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
95
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
96 /**
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
97 * Wraps an input stream into an object input stream. You may
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
98 * overwrite this to get a more specialized deserializer.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
99 * @param is The raw input stream
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
100 * @return An instance of a subclass of ObjectInputStream.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
101 * @throws IOException Thrown if something went wrong during
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
102 * creation of the object input stream.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
103 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
104 protected ObjectInputStream getObjectInputStream(InputStream is)
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
105 throws IOException
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
106 {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
107 return new ObjectInputStream(is);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
108 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
109
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
110 /**
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
111 * Wraps an output stream into an object output stream. You may
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
112 * overwrite this to get a more specialized serializer.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
113 * @param os the raw output stream.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
114 * @return An instance of a subclass of ObjectOutputStream.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
115 * @throws IOException Thrown if something went wrong during
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
116 * creation of the object output stream.
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
117 */
41
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
118 protected ObjectOutputStream getObjectOutputStream(OutputStream os)
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
119 throws IOException
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
120 {
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
121 return new ObjectOutputStream(os);
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
122 }
5e4bc24ea438 Made serilization more flexible. DB update required!!!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
123 }
87
0f48188a6e02 Added some javadoc to the artifactdatabase module. Not done yet.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 77
diff changeset
124 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org