KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > store > jdbc > JdbcStore


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.store.jdbc;
18
19 import java.io.IOException JavaDoc;
20 import java.io.ObjectInputStream JavaDoc;
21 import java.io.ObjectOutputStream JavaDoc;
22 import java.sql.Connection JavaDoc;
23
24 import org.apache.activeio.util.ByteArrayInputStream;
25 import org.apache.activeio.util.ByteArrayOutputStream;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.servicemix.store.Store;
29
30 public class JdbcStore implements Store {
31
32     private static final Log log = LogFactory.getLog(JdbcStore.class);
33
34     private JdbcStoreFactory factory;
35     private String JavaDoc name;
36     
37     public JdbcStore(JdbcStoreFactory factory, String JavaDoc name) {
38         this.factory = factory;
39         this.name = name;
40     }
41
42     public boolean hasFeature(String JavaDoc name) {
43         return PERSISTENT.equals(name) ||
44                (CLUSTERED.equals(name) && factory.isClustered()) ||
45                (TRANSACTIONAL.equals(name) && factory.isTransactional());
46     }
47
48     public void store(String JavaDoc id, Object JavaDoc data) throws IOException JavaDoc {
49         log.debug("Storing object with id: " + id);
50         Connection JavaDoc connection = null;
51         try {
52             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
53             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(buffer);
54             out.writeObject(data);
55             out.close();
56             connection = factory.getDataSource().getConnection();
57             factory.getAdapter().doStoreData(connection, name + ":" + id, buffer.toByteArray());
58         } catch (Exception JavaDoc e) {
59             throw (IOException JavaDoc) new IOException JavaDoc("Error storing object").initCause(e);
60         } finally {
61             if (connection != null) {
62                 try {
63                     connection.close();
64                 } catch (Exception JavaDoc e) {
65                     throw (IOException JavaDoc) new IOException JavaDoc("Error closing connection").initCause(e);
66                 }
67             }
68         }
69     }
70
71     public String JavaDoc store(Object JavaDoc data) throws IOException JavaDoc {
72         String JavaDoc id = factory.getIdGenerator().generateId();
73         store(id, data);
74         return id;
75     }
76
77     public Object JavaDoc load(String JavaDoc id) throws IOException JavaDoc {
78         log.debug("Loading object with id: " + id);
79         Connection JavaDoc connection = null;
80         try {
81             connection = factory.getDataSource().getConnection();
82             byte[] data = factory.getAdapter().doLoadData(connection, name + ":" + id);
83             Object JavaDoc result = null;
84             if (data != null) {
85                 ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(new ByteArrayInputStream(data));
86                 result = ois.readObject();
87                 factory.getAdapter().doRemoveData(connection, name + ":" + id);
88             }
89             return result;
90         } catch (Exception JavaDoc e) {
91             throw (IOException JavaDoc) new IOException JavaDoc("Error storing object").initCause(e);
92         } finally {
93             if (connection != null) {
94                 try {
95                     connection.close();
96                 } catch (Exception JavaDoc e) {
97                     throw (IOException JavaDoc) new IOException JavaDoc("Error closing connection").initCause(e);
98                 }
99             }
100         }
101     }
102
103 }
104
Popular Tags