comparison artifact-database/src/main/java/de/intevation/artifactdatabase/Backend.java @ 14:0d16d1bb2df0

Initial checkin of artigact persistents back by database. artifacts/trunk@29 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 07 Sep 2009 08:41:05 +0000
parents 0d6badf6af42
children 9ad6ec2d09c3
comparison
equal deleted inserted replaced
13:0d6badf6af42 14:0d16d1bb2df0
1 package de.intevation.artifactdatabase; 1 package de.intevation.artifactdatabase;
2
3 import org.w3c.dom.Document;
4
5 import java.util.UUID;
6
7 import java.sql.Connection;
8 import java.sql.SQLException;
9 import java.sql.PreparedStatement;
10 import java.sql.Types;
11 import java.sql.ResultSet;
12
13 import javax.sql.DataSource;
14
15 import java.io.IOException;
16 import java.io.ByteArrayOutputStream;
17 import java.io.ObjectOutputStream;
18
19 import java.util.zip.GZIPOutputStream;
2 20
3 import de.intevation.artifacts.ArtifactFactory; 21 import de.intevation.artifacts.ArtifactFactory;
4 import de.intevation.artifacts.Artifact; 22 import de.intevation.artifacts.Artifact;
5 23
6 /** 24 /**
7 * @author Sascha L. Teichmann 25 * @author Sascha L. Teichmann
8 */ 26 */
9 public class Backend 27 public class Backend
10 { 28 {
29 public static final String SQL_NEXT_ID =
30 SQL.get("artifacts.id.nextval");
31
32 public static final String SQL_INSERT =
33 SQL.get("artifacts.insert");
34
35 public static final String SQL_UPDATE =
36 SQL.get("artifacts.update");
37
38 public static final String SQL_TOUCH =
39 SQL.get("artifacts.touch");
40
41 /**
42 * Used to wrap the calls to invole database actions.
43 */
44 public class ArtifactProxy
45 implements Artifact
46 {
47 protected Artifact original;
48 protected int id;
49 protected boolean unwritten;
50
51 public ArtifactProxy() {
52 }
53
54 public ArtifactProxy(Artifact original, int id, boolean unwritten) {
55 this.original = original;
56 this.id = id;
57 this.unwritten = unwritten;
58 }
59
60 public Artifact getOriginal() {
61 return original;
62 }
63
64 public int getId() {
65 return id;
66 }
67
68 public boolean isUnwritten() {
69 return unwritten;
70 }
71
72 public String identifier() {
73 return original.identifier();
74 }
75
76 public String hash() {
77 return original.hash();
78 }
79
80 public Document describe(Object context) {
81 try {
82 return original.describe(context);
83 }
84 finally {
85 touch(this);
86 }
87 }
88
89 public Document advance(Document target, Object context) {
90 try {
91 return original.advance(target, context);
92 }
93 finally {
94 store(this);
95 }
96 }
97
98 public Document feed(Document data, Object context) {
99 try {
100 return original.feed(data, context);
101 }
102 finally {
103 store(this);
104 }
105 }
106
107 public byte [] out(Document format, Object context) {
108 try {
109 return original.out(format, context);
110 }
111 finally {
112 touch(this);
113 }
114 }
115
116 public void setup(String identifier, Object context) {
117 original.setup(identifier, context);
118 }
119
120 public void endOfLife(Object context) {
121 original.endOfLife(context);
122 }
123
124 public byte [] toBytes() {
125 try {
126 ByteArrayOutputStream bos = new ByteArrayOutputStream();
127 GZIPOutputStream gos = new GZIPOutputStream(bos);
128 ObjectOutputStream oos = new ObjectOutputStream(gos);
129
130 oos.writeObject(original);
131 oos.flush();
132
133 return bos.toByteArray();
134 }
135 catch (IOException ioe) {
136 throw new RuntimeException(ioe);
137 }
138 }
139 } // class ArtifactProxy
140
11 public Backend() { 141 public Backend() {
12 } 142 }
13 143
14 public Artifact getArtifact(String idenitfier) { 144 public Artifact getArtifact(String idenitfier) {
15 return null; 145 return null;
16 } 146 }
17 147
18 public Artifact createArtifactWithFactory( 148 public Artifact createArtifactWithFactory(
19 ArtifactFactory factory, Object context 149 ArtifactFactory factory, Object context
20 ) { 150 ) {
21 return null; 151 UUID uuid = UUID.randomUUID();
22 } 152 Artifact artifact = factory.createArtifact(
153 uuid.toString(), context);
154
155 Long ttl = factory.timeToLiveUntouched(
156 artifact, context);
157
158 int id = insertDatabase(uuid, ttl);
159
160 return new ArtifactProxy(artifact, id, true);
161 }
162
163 protected int insertDatabase(UUID uuid, Long ttl) {
164 Connection connection = null;
165 PreparedStatement stmnt_next_id = null;
166 PreparedStatement stmnt_insert = null;
167 ResultSet res_id = null;
168
169 DataSource dataSource = DBConnection.getDataSource();
170 try {
171 connection = dataSource.getConnection();
172 try {
173 connection.setAutoCommit(false);
174
175 stmnt_next_id = connection.prepareStatement(SQL_NEXT_ID);
176 stmnt_insert = connection.prepareStatement(SQL_INSERT);
177
178 res_id = stmnt_next_id.executeQuery();
179
180 if (!res_id.next()) {
181 throw new RuntimeException("No id generated");
182 }
183
184 int id = res_id.getInt(1);
185
186 stmnt_insert.setInt(1, id);
187 stmnt_insert.setString(2, uuid.toString());
188 if (ttl == null) {
189 stmnt_insert.setNull(3, Types.BIGINT);
190 }
191 else {
192 stmnt_insert.setLong(3, ttl.longValue());
193 }
194
195 stmnt_insert.execute();
196
197 connection.commit();
198
199 return id;
200 }
201 catch (SQLException sqle) {
202 connection.rollback();
203 throw sqle;
204 }
205 }
206 catch (SQLException sqle) {
207 sqle.printStackTrace(System.err);
208 }
209 finally {
210 if (res_id != null) {
211 try { res_id.close(); }
212 catch (SQLException sqle) {}
213 }
214 if (stmnt_insert != null) {
215 try { stmnt_insert.close(); }
216 catch (SQLException sqle) {}
217 }
218 if (stmnt_next_id != null) {
219 try { stmnt_next_id.close(); }
220 catch (SQLException sqle) {}
221 }
222 if (connection != null) {
223 try { connection.close(); }
224 catch (SQLException sqle) {}
225 }
226 }
227 throw new RuntimeException("failed insert artifact into database");
228 }
229
230 public void touch(ArtifactProxy proxy) {
231 System.err.println("touch: " + proxy);
232 if (proxy.isUnwritten()) {
233 store(proxy);
234 return;
235 }
236 Connection connection = null;
237 PreparedStatement stmnt_touch = null;
238 DataSource dataSource = DBConnection.getDataSource();
239 try {
240 connection = dataSource.getConnection();
241 try {
242 connection.setAutoCommit(false);
243 stmnt_touch = connection.prepareStatement(SQL_UPDATE);
244 stmnt_touch.setInt(1, proxy.getId());
245 stmnt_touch.execute();
246 connection.commit();
247 }
248 catch (SQLException sqle) {
249 connection.rollback();
250 }
251 }
252 catch (SQLException sqle) {
253 sqle.printStackTrace(System.err);
254 }
255 finally {
256 if (stmnt_touch != null) {
257 try { stmnt_touch.close(); }
258 catch (SQLException sqle) {}
259 }
260 if (connection != null) {
261 try { connection.close(); }
262 catch (SQLException sqle) {}
263 }
264 }
265 }
266
267 public void store(ArtifactProxy proxy) {
268 System.err.println("store: " + proxy);
269 Connection connection = null;
270 PreparedStatement stmnt_update = null;
271 DataSource dataSource = DBConnection.getDataSource();
272 try {
273 connection = dataSource.getConnection();
274 try {
275 connection.setAutoCommit(false);
276 stmnt_update = connection.prepareStatement(SQL_UPDATE);
277 stmnt_update.setInt(1, proxy.getId());
278 stmnt_update.setBytes(2, proxy.toBytes());
279 stmnt_update.execute();
280 connection.commit();
281 }
282 catch (SQLException sqle) {
283 connection.rollback();
284 }
285 }
286 catch (SQLException sqle) {
287 sqle.printStackTrace(System.err);
288 }
289 finally {
290 if (stmnt_update != null) {
291 try { stmnt_update.close(); }
292 catch (SQLException sqle) {}
293 }
294 if (connection != null) {
295 try { connection.close(); }
296 catch (SQLException sqle) {}
297 }
298 }
299 }
300
23 } 301 }
24 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8: 302 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org