001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.servicemix.store.jdbc;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.ByteArrayOutputStream;
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024: import java.sql.Connection;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.servicemix.store.Store;
029:
030: public class JdbcStore implements Store {
031:
032: private static final Log LOG = LogFactory.getLog(JdbcStore.class);
033:
034: private JdbcStoreFactory factory;
035: private String name;
036:
037: public JdbcStore(JdbcStoreFactory factory, String name) {
038: this .factory = factory;
039: this .name = name;
040: }
041:
042: public boolean hasFeature(String feature) {
043: return PERSISTENT.equals(feature)
044: || (CLUSTERED.equals(feature) && factory.isClustered())
045: || (TRANSACTIONAL.equals(feature) && factory
046: .isTransactional());
047: }
048:
049: public void store(String id, Object data) throws IOException {
050: LOG.debug("Storing object with id: " + id);
051: Connection connection = null;
052: try {
053: ByteArrayOutputStream buffer = new ByteArrayOutputStream();
054: ObjectOutputStream out = new ObjectOutputStream(buffer);
055: out.writeObject(data);
056: out.close();
057: connection = factory.getDataSource().getConnection();
058: factory.getAdapter().doStoreData(connection,
059: name + ":" + id, buffer.toByteArray());
060: } catch (Exception e) {
061: throw (IOException) new IOException("Error storing object")
062: .initCause(e);
063: } finally {
064: close(connection);
065: }
066: }
067:
068: public String store(Object data) throws IOException {
069: String id = factory.getIdGenerator().generateId();
070: store(id, data);
071: return id;
072: }
073:
074: public Object load(String id) throws IOException {
075: LOG.debug("Loading object with id: " + id);
076: Connection connection = null;
077: try {
078: connection = factory.getDataSource().getConnection();
079: byte[] data = factory.getAdapter().doLoadData(connection,
080: name + ":" + id);
081: Object result = null;
082: if (data != null) {
083: ObjectInputStream ois = new ObjectInputStream(
084: new ByteArrayInputStream(data));
085: result = ois.readObject();
086: factory.getAdapter().doRemoveData(connection,
087: name + ":" + id);
088: }
089: return result;
090: } catch (Exception e) {
091: throw (IOException) new IOException("Error storing object")
092: .initCause(e);
093: } finally {
094: close(connection);
095: }
096: }
097:
098: protected void close(Connection connection) throws IOException {
099: if (connection != null) {
100: try {
101: connection.close();
102: } catch (Exception e) {
103: throw (IOException) new IOException(
104: "Error closing connection").initCause(e);
105: }
106: }
107: }
108:
109: }
|