001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software, please see the 018 * company website: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.db.oracle; 029 030import org.opencms.db.CmsDbContext; 031import org.opencms.db.CmsDbEntryNotFoundException; 032import org.opencms.db.CmsDbIoException; 033import org.opencms.db.CmsDbSqlException; 034import org.opencms.db.generic.CmsSqlManager; 035import org.opencms.db.generic.Messages; 036 037import java.io.IOException; 038import java.io.OutputStream; 039import java.sql.Connection; 040import java.sql.PreparedStatement; 041import java.sql.ResultSet; 042import java.sql.SQLException; 043 044/** 045 * Oracle database backend for the OpenCms deduplicating media storage engine (DMSE).<p> 046 * 047 * @see org.opencms.db.generic.CmsDbStorage 048 * @see org.opencms.db.storage.CmsStorageManager 049 */ 050public class CmsDbStorage extends org.opencms.db.generic.CmsDbStorage { 051 052 /** 053 * Public constructor.<p> 054 * 055 * @param sqlManager the SQL manager to use 056 */ 057 public CmsDbStorage(CmsSqlManager sqlManager) { 058 059 super(sqlManager); 060 } 061 062 /** 063 * @see org.opencms.db.storage.I_CmsStorage#storeContent(org.opencms.db.CmsDbContext, java.lang.String, byte[]) 064 */ 065 @Override 066 public void storeContent(CmsDbContext dbc, String hash, byte[] content) throws Exception { 067 068 Connection conn = null; 069 PreparedStatement stmt = null; 070 PreparedStatement commit = null; 071 ResultSet res = null; 072 boolean wasInTransaction = false; 073 try { 074 conn = m_sqlManager.getConnection(dbc); 075 stmt = m_sqlManager.getPreparedStatement(conn, "C_STORAGE_EXISTS"); 076 stmt.setString(1, hash); 077 res = stmt.executeQuery(); 078 if (res.next()) { 079 m_sqlManager.closeAll(dbc, conn, stmt, res); 080 conn = null; 081 stmt = null; 082 res = null; 083 return; 084 } 085 m_sqlManager.closeAll(dbc, null, stmt, res); 086 stmt = null; 087 res = null; 088 089 stmt = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_STORAGE_WRITE"); 090 stmt.setString(1, hash); 091 stmt.executeUpdate(); 092 m_sqlManager.closeAll(dbc, null, stmt, null); 093 stmt = null; 094 095 stmt = m_sqlManager.getPreparedStatement(conn, "C_ORACLE_STORAGE_UPDATECONTENT"); 096 097 wasInTransaction = !conn.getAutoCommit(); 098 if (!wasInTransaction) { 099 conn.setAutoCommit(false); 100 } 101 102 stmt.setString(1, hash); 103 res = stmt.executeQuery(); 104 if (!res.next()) { 105 throw new CmsDbEntryNotFoundException(Messages.get().container(Messages.ERR_GENERIC_SQL_1)); 106 } 107 108 OutputStream output = CmsUserDriver.getOutputStreamFromBlob(res, "FILE_CONTENT"); 109 output.write(content, 0, content.length); 110 output.close(); 111 112 if (!wasInTransaction) { 113 commit = m_sqlManager.getPreparedStatement(conn, "C_COMMIT"); 114 commit.execute(); 115 m_sqlManager.closeAll(dbc, null, commit, null); 116 } 117 118 m_sqlManager.closeAll(dbc, null, stmt, res); 119 commit = null; 120 stmt = null; 121 res = null; 122 123 if (!wasInTransaction) { 124 conn.setAutoCommit(true); 125 } 126 } catch (IOException e) { 127 throw new CmsDbIoException(Messages.get().container(Messages.ERR_WRITING_TO_OUTPUT_STREAM_1, hash), e); 128 } catch (SQLException e) { 129 throw new CmsDbSqlException( 130 Messages.get().container(Messages.ERR_GENERIC_SQL_1, CmsDbSqlException.getErrorQuery(stmt)), 131 e); 132 } finally { 133 org.opencms.db.oracle.CmsSqlManager.closeAllInTransaction( 134 m_sqlManager, 135 dbc, 136 conn, 137 stmt, 138 res, 139 commit, 140 wasInTransaction); 141 } 142 } 143}