1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.io.*; 27 import java.lang.reflect.*; 28 import java.sql.Connection ; 29 import java.sql.PreparedStatement ; 30 import java.sql.CallableStatement ; 31 import java.sql.ResultSet ; 32 import java.sql.SQLException ; 33 34 import org.apache.slide.common.Uri; 35 import org.apache.slide.common.Service; 36 import org.apache.slide.common.ServiceAccessException; 37 import org.apache.slide.content.NodeRevisionDescriptor; 38 import org.apache.slide.content.NodeRevisionContent; 39 import org.apache.slide.util.logger.Logger; 40 41 42 47 public class OracleRDBMSAdapter extends CommonRDBMSAdapter implements SequenceAdapter { 48 49 51 public OracleRDBMSAdapter(Service service, Logger logger) 52 throws ClassNotFoundException , NoSuchMethodException 53 { 54 super(service, logger); 55 56 Class bCls = Class.forName("oracle.sql.BLOB"); 57 blobGetOutStreamMethod = bCls.getMethod("getBinaryOutputStream", new Class [] {}); 58 } 59 60 61 63 protected void storeContent( 64 Connection connection, Uri uri, 65 NodeRevisionDescriptor revisionDescriptor, 66 NodeRevisionContent revisionContent) throws IOException, SQLException 67 { 68 assureVersionInfo(connection, uri, revisionDescriptor); 69 70 InputStream is = revisionContent.streamContent(); 71 if (is != null) { 72 long blobLength = 0; 73 File tempFile = null; 74 75 if (bcompress) { 76 getLogger().log("Compressing the data", LOG_CHANNEL, 77 Logger.DEBUG); 78 StoreContentZip ziputil = new StoreContentZip(); 79 ziputil.Zip(is); 80 is = ziputil.getInputStream(); 81 82 if (revisionDescriptor.getContentLength() == -1) { 84 revisionDescriptor.setContentLength(ziputil.getInitialContentLength()); 85 } 86 87 blobLength = ziputil.getContentLength(); 88 } else { 89 if (revisionDescriptor.getContentLength() == -1) { 91 try { 92 tempFile = File.createTempFile("content", null); 93 FileOutputStream fos = new FileOutputStream(tempFile); 94 try { 95 byte buffer[] = new byte[4096]; 96 do { 97 int nChar = is.read(buffer); 98 if (nChar == -1) { 99 break; 100 } 101 fos.write(buffer, 0, nChar); 102 } while (true); 103 } finally { 104 fos.close(); 105 } 106 is.close(); 107 is = new FileInputStream(tempFile); 108 109 revisionDescriptor.setContentLength(tempFile.length()); 110 111 } catch (IOException ex) { 112 getLogger().log(ex.toString() + " during the calculation of the content length.", 113 LOG_CHANNEL, Logger.ERROR); 114 throw ex; 115 } 116 } 117 118 blobLength = revisionDescriptor.getContentLength(); 119 } 120 121 CallableStatement statement = null; 122 try { 123 long versionID = getVersionID(connection, uri.toString(), revisionDescriptor); 124 statement = connection.prepareCall( 125 "BEGIN " 126 + "insert into VERSION_CONTENT (VERSION_ID, CONTENT) " 127 + "values (?, empty_blob()) RETURN CONTENT into ?;" 128 + "END; " 129 ); 130 statement.setLong(1, versionID); 131 statement.registerOutParameter(2, java.sql.Types.BLOB); 132 133 statement.executeUpdate(); 134 135 139 Object bObj = statement.getBlob(2); 140 OutputStream os = null; 141 try { 142 os = (OutputStream) blobGetOutStreamMethod.invoke(bObj, new Object [] {}); 143 } catch (Exception ex) { 144 ex.printStackTrace(); 145 throw new IOException("Reflection error"); 146 } 147 148 try { 149 byte buffer[] = new byte[4096]; 150 do { 151 int nChar = is.read(buffer); 152 if (nChar == -1) { 153 break; 154 } 155 os.write(buffer, 0, nChar); 156 } while (true); 157 } finally { 158 os.flush(); 159 os.close(); 160 } 161 162 } finally { 163 if (tempFile != null) { 164 try { 165 is.close(); 166 } catch (Exception ex) { 167 } 169 is = null; 170 if (!tempFile.delete()) { 171 logger.log("Could not delete file \"" + tempFile.getAbsolutePath() + 172 "\""); 173 } 174 } 175 176 try { 177 close(statement); 178 } finally { 179 if (is != null) { 180 try { 185 is.close(); 186 } catch (IOException ioe) { 187 logger.log("Could not close stream", ioe, LOG_CHANNEL, Logger.DEBUG); 188 } 189 } 190 } 191 } 192 } 193 } 194 195 protected static String normalizeSequenceName(String sequenceName) { 196 return sequenceName.replace('-', '_').toUpperCase() + "_SEQ"; 197 } 198 199 public boolean isSequenceSupported(Connection conn) { 200 return true; 201 } 202 203 public boolean createSequence(Connection conn, String sequenceName) throws ServiceAccessException { 204 205 String query = "CREATE SEQUENCE \"" + normalizeSequenceName(sequenceName) + "\""; 206 207 PreparedStatement statement = null; 208 209 try { 210 statement = conn.prepareStatement(query); 211 statement.executeUpdate(); 212 return true; 213 } catch (SQLException e) { 214 throw new ServiceAccessException(service, e); 215 } finally { 216 close(statement); 217 } 218 219 } 220 221 public long nextSequenceValue(Connection conn, String sequenceName) throws ServiceAccessException { 222 String selectQuery = "SELECT \"" + normalizeSequenceName(sequenceName)+"\".nextval FROM DUAL"; 223 224 PreparedStatement selectStatement = null; 225 ResultSet res = null; 226 227 try { 228 selectStatement = conn.prepareStatement(selectQuery); 229 res = selectStatement.executeQuery(); 230 if (!res.next()) { 231 throw new ServiceAccessException(service, "Could not increment sequence " + sequenceName); 232 } 233 long value = res.getLong(1); 234 return value; 235 } catch (SQLException e) { 236 throw new ServiceAccessException(service, e); 237 } finally { 238 close(selectStatement, res); 239 } 240 } 241 242 public boolean sequenceExists(Connection conn, String sequenceName) throws ServiceAccessException { 243 244 PreparedStatement selectStatement = null; 245 ResultSet res = null; 246 247 try { 248 selectStatement = 249 conn.prepareStatement("ALTER SEQUENCE \"" + normalizeSequenceName(sequenceName) + "\" INCREMENT BY 1"); 250 res = selectStatement.executeQuery(); 251 return true; 252 } catch (SQLException e) { 253 return false; 254 } finally { 255 close(selectStatement, res); 256 } 257 } 258 259 260 262 protected Method blobGetOutStreamMethod; 263 } 264 265 266 | Popular Tags |