dsavazzi 2004/11/13 07:10:07
Modified: src/stores/org/apache/slide/store/impl/rdbms
OracleRDBMSAdapter.java
Log:
Added patch by Richard Emberson.
Revision Changes Path
1.12 +156 -10
jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/OracleRDBMSAdapter.java
Index: OracleRDBMSAdapter.java
===================================================================
RCS file:
/home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/OracleRDBMSAdapter.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- OracleRDBMSAdapter.java 30 Oct 2004 12:48:42 -0000 1.11
+++ OracleRDBMSAdapter.java 13 Nov 2004 15:10:07 -0000 1.12
@@ -23,36 +23,178 @@
package org.apache.slide.store.impl.rdbms;
+import java.io.*;
+import java.lang.reflect.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
+import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
+import org.apache.slide.common.Uri;
import org.apache.slide.common.Service;
import org.apache.slide.common.ServiceAccessException;
+import org.apache.slide.content.NodeRevisionDescriptor;
+import org.apache.slide.content.NodeRevisionContent;
import org.apache.slide.util.logger.Logger;
/**
- * Adapter for Oracle 10.
+ * Adapter for Oracle 9 and 10.
*
* @version $Revision$
*/
public class OracleRDBMSAdapter extends CommonRDBMSAdapter implements
SequenceAdapter {
- protected static String normalizeSequenceName(String sequenceName) {
- return sequenceName.replace('-', '_').toUpperCase() + "_SEQ";
- }
-
// Constructor
- public OracleRDBMSAdapter(Service service, Logger logger) {
+ public OracleRDBMSAdapter(Service service, Logger logger) throws
ClassNotFoundException, NoSuchMethodException
+ {
super(service, logger);
+
+ Class bCls = Class.forName("oracle.sql.BLOB");
+ blobGetOutStreamMethod = bCls.getMethod("getBinaryOutputStream", new
Class[] {});
}
// Public Methods
+ protected void storeContent(
+ Connection connection, Uri uri,
+ NodeRevisionDescriptor revisionDescriptor,
+ NodeRevisionContent revisionContent) throws IOException,
SQLException
+ {
+ assureVersionInfo(connection, uri, revisionDescriptor);
+
+ InputStream is = revisionContent.streamContent();
+ if (is != null) {
+ long blobLength = 0;
+ File tempFile = null;
+
+ if (bcompress) {
+ getLogger().log("Compressing the data", LOG_CHANNEL,
+ Logger.DEBUG);
+ StoreContentZip ziputil = new StoreContentZip();
+ ziputil.Zip(is);
+ is = ziputil.getInputStream();
+
+ // fix RevisionDescriptor Content Length
+ if (revisionDescriptor.getContentLength() == -1) {
+
revisionDescriptor.setContentLength(ziputil.getInitialContentLength());
+ }
+
+ blobLength = ziputil.getContentLength();
+ } else {
+ // fix RevisionDescriptor Content Length
+ if (revisionDescriptor.getContentLength() == -1) {
+ try {
+ tempFile = File.createTempFile("content", null);
+ FileOutputStream fos = new
FileOutputStream(tempFile);
+ try {
+ byte buffer[] = new byte[4096];
+ do {
+ int nChar = is.read(buffer);
+ if (nChar == -1) {
+ break;
+ }
+ fos.write(buffer, 0, nChar);
+ } while (true);
+ } finally {
+ fos.close();
+ }
+ is.close();
+ is = new FileInputStream(tempFile);
+
+
revisionDescriptor.setContentLength(tempFile.length());
+
+ } catch (IOException ex) {
+ getLogger().log(ex.toString() + " during the
calculation of the content length.",
+ LOG_CHANNEL, Logger.ERROR);
+ throw ex;
+ }
+ }
+
+ blobLength = revisionDescriptor.getContentLength();
+ }
+
+ CallableStatement statement = null;
+ try {
+ long versionID = getVersionID(connection, uri.toString(),
revisionDescriptor);
+ statement = connection.prepareCall(
+ "BEGIN "
+ + "insert into VERSION_CONTENT (VERSION_ID, CONTENT) "
+ + "values (?, empty_blob()) RETURN CONTENT into ?;"
+ + "END; "
+ );
+ statement.setLong(1, versionID);
+ statement.registerOutParameter(2, java.sql.Types.BLOB);
+
+ statement.executeUpdate();
+
+ // do these two lines using reflection
+ // BLOB blob = (BLOB) statement.getBlob(2);
+ // OutputStream os = blob.getBinaryOutputStream();
+
+ Object bObj = statement.getBlob(2);
+ OutputStream os = null;
+ try {
+ os = (OutputStream) blobGetOutStreamMethod.invoke(bObj,
new Object[] {});
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ throw new IOException("Reflection error");
+ }
+
+ try {
+ byte buffer[] = new byte[4096];
+ do {
+ int nChar = is.read(buffer);
+ if (nChar == -1) {
+ break;
+ }
+ os.write(buffer, 0, nChar);
+ } while (true);
+ } finally {
+ os.flush();
+ os.close();
+ }
+
+ } finally {
+ if (tempFile != null) {
+ try {
+ is.close();
+ } catch (Exception ex) {
+ // ignore
+ }
+ is = null;
+ if (!tempFile.delete()) {
+ logger.log("Could not delete file \"" +
tempFile.getAbsolutePath() +
+ "\"");
+ }
+ }
+
+ try {
+ close(statement);
+ } finally {
+ if (is != null) {
+ // XXX some JDBC drivers seem to close the stream
upon
+ // closing of
+ // the statement; if so this will raise an
IOException
+ // silently ignore it...
+ try {
+ is.close();
+ } catch (IOException ioe) {
+ logger.log("Could not close stream", ioe,
LOG_CHANNEL, Logger.DEBUG);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ protected static String normalizeSequenceName(String sequenceName) {
+ return sequenceName.replace('-', '_').toUpperCase() + "_SEQ";
+ }
+
public boolean isSequenceSupported(Connection conn) {
return true;
}
@@ -113,6 +255,10 @@
}
}
+
+ // Attributes
+
+ protected Method blobGetOutStreamMethod;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]