001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: oracle_jdbc_driver_OracleDriver.java 3634 2007-01-08 21:42:24Z gbevin $
007: */
008: package com.uwyn.rife.cmf.dam.contentstores.imagestoredrivers;
009:
010: import com.uwyn.rife.database.*;
011: import java.io.*;
012:
013: import com.uwyn.rife.cmf.dam.ContentDataUser;
014: import com.uwyn.rife.cmf.dam.contentstores.exceptions.StoreContentDataErrorException;
015: import com.uwyn.rife.cmf.dam.contentstores.exceptions.UseContentDataErrorException;
016: import com.uwyn.rife.cmf.dam.exceptions.ContentManagerException;
017: import com.uwyn.rife.config.RifeConfig;
018: import com.uwyn.rife.database.exceptions.DatabaseException;
019: import com.uwyn.rife.database.queries.Insert;
020: import com.uwyn.rife.database.queries.Select;
021: import com.uwyn.rife.tools.InnerClassException;
022: import java.sql.SQLException;
023: import java.sql.Types;
024: import oracle.sql.BLOB;
025:
026: public class oracle_jdbc_driver_OracleDriver extends generic {
027: private Insert mStoreContentDataEmptyBlob = null;
028:
029: public oracle_jdbc_driver_OracleDriver(Datasource datasource) {
030: super (datasource);
031:
032: mStoreContentDataEmptyBlob = new Insert(getDatasource()).into(
033: RifeConfig.Cmf.getTableContentStoreImage())
034: .fieldParameter("contentId").fieldParameter(
035: getContentSizeColumnName()).fieldCustom(
036: "content", "empty_blob()");
037: }
038:
039: protected String getContentSizeColumnName() {
040: return "contentsize";
041: }
042:
043: public <ResultType> ResultType useContentData(final int id,
044: ContentDataUser user) throws ContentManagerException {
045: if (id < 0)
046: throw new IllegalArgumentException("id must be positive");
047: if (null == user)
048: throw new IllegalArgumentException("user can't be null");
049:
050: try {
051: return (ResultType) user.useContentData(executeQuery(
052: mRetrieveContent, new DbPreparedStatementHandler() {
053: public void setParameters(
054: DbPreparedStatement statement) {
055: statement.setInt("contentId", id);
056: }
057:
058: public Object concludeResults(
059: DbResultSet resultset)
060: throws SQLException {
061: if (!resultset.next()) {
062: return null;
063: }
064:
065: ByteArrayOutputStream os = new ByteArrayOutputStream();
066: BLOB blob = (BLOB) resultset
067: .getBlob("content");
068: if (null == blob) {
069: return null;
070: }
071:
072: InputStream is = blob.getBinaryStream();
073: try {
074: byte[] buffer = new byte[blob
075: .getBufferSize()];
076: BufferedInputStream buffered_image_is = new BufferedInputStream(
077: is, buffer.length);
078: int size = 0;
079: while ((size = buffered_image_is
080: .read(buffer)) != -1) {
081: os.write(buffer, 0, size);
082: }
083:
084: try {
085: os.flush();
086: } catch (IOException e) {
087: // don't do anything, the client has probably disconnected
088: }
089: } catch (IOException e) {
090: throw new UseContentDataErrorException(
091: id, e);
092: }
093:
094: return os.toByteArray();
095: }
096: }));
097: } catch (DatabaseException e) {
098: throw new UseContentDataErrorException(id, e);
099: }
100: }
101:
102: protected boolean storeTypedData(Insert storeContent, final int id,
103: final byte[] data) throws ContentManagerException {
104: try {
105: if (null == data) {
106: return executeUpdate(storeContent,
107: new DbPreparedStatementHandler() {
108: public void setParameters(
109: DbPreparedStatement statement) {
110: statement
111: .setInt("contentId", id)
112: .setNull("content", Types.BLOB)
113: .setInt(
114: getContentSizeColumnName(),
115: 0);
116: }
117: }) != -1;
118: } else {
119: Integer result = inTransaction(new DbTransactionUser() {
120: public Object useTransaction()
121: throws InnerClassException {
122: int result = executeUpdate(
123: mStoreContentDataEmptyBlob,
124: new DbPreparedStatementHandler() {
125: public void setParameters(
126: DbPreparedStatement statement) {
127: statement
128: .setInt("contentId", id)
129: .setInt(
130: getContentSizeColumnName(),
131: data.length);
132: }
133: });
134:
135: executeQuery(new Select(getDatasource()).from(
136: mStoreContentDataEmptyBlob.getInto())
137: .field("content").where(
138: "contentId = " + id
139: + " FOR UPDATE"),
140: new DbResultSetHandler() {
141: public Object concludeResults(
142: DbResultSet resultset)
143: throws SQLException {
144: if (!resultset.next()) {
145: return null;
146: }
147:
148: BLOB blob = (BLOB) resultset
149: .getBlob(1);
150: byte[] buffer = new byte[blob
151: .getBufferSize()];
152: InputStream is = new ByteArrayInputStream(
153: data);
154: OutputStream os = blob
155: .getBinaryOutputStream();
156: try {
157: try {
158: int size = 0;
159: while ((size = is
160: .read(buffer)) != -1) {
161: os.write(buffer, 0,
162: size);
163: }
164: } finally {
165: os.close();
166: }
167: } catch (IOException e) {
168: throw new DatabaseException(
169: e);
170: }
171:
172: return null;
173: }
174: });
175:
176: return result;
177: }
178: });
179:
180: return null != result && result != -1;
181: }
182: } catch (DatabaseException e) {
183: throw new StoreContentDataErrorException(id, e);
184: }
185: }
186: }
|