001: /*
002: * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/oracle/CmsUserDriver.java,v $
003: * Date : $Date: 2008-02-27 12:05:43 $
004: * Version: $Revision: 1.59 $
005: *
006: * This library is part of OpenCms -
007: * the Open Source Content Management System
008: *
009: * Copyright (c) 2002 - 2008 Alkacon Software GmbH (http://www.alkacon.com)
010: *
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Lesser General Public
013: * License as published by the Free Software Foundation; either
014: * version 2.1 of the License, or (at your option) any later version.
015: *
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Lesser General Public License for more details.
020: *
021: * For further information about Alkacon Software GmbH, please see the
022: * company website: http://www.alkacon.com
023: *
024: * For further information about OpenCms, please see the
025: * project website: http://www.opencms.org
026: *
027: * You should have received a copy of the GNU Lesser General Public
028: * License along with this library; if not, write to the Free Software
029: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
030: */
031:
032: package org.opencms.db.oracle;
033:
034: import org.opencms.db.CmsDbContext;
035: import org.opencms.db.CmsDbEntryNotFoundException;
036: import org.opencms.db.CmsDbIoException;
037: import org.opencms.db.CmsDbSqlException;
038: import org.opencms.db.generic.CmsSqlManager;
039: import org.opencms.db.generic.Messages;
040: import org.opencms.file.CmsDataAccessException;
041: import org.opencms.util.CmsDataTypeUtil;
042: import org.opencms.util.CmsUUID;
043:
044: import java.io.IOException;
045: import java.io.OutputStream;
046: import java.sql.Blob;
047: import java.sql.Connection;
048: import java.sql.PreparedStatement;
049: import java.sql.ResultSet;
050: import java.sql.SQLException;
051:
052: import org.apache.commons.dbcp.DelegatingResultSet;
053:
054: /**
055: * Oracle implementation of the user driver methods.<p>
056: *
057: * @author Thomas Weckert
058: * @author Carsten Weinholz
059: *
060: * @version $Revision: 1.59 $
061: *
062: * @since 6.0.0
063: */
064: public class CmsUserDriver extends org.opencms.db.generic.CmsUserDriver {
065:
066: /**
067: * Generates an Output stream that writes to a blob, also truncating the existing blob if required.<p>
068: *
069: * Apparently Oracle requires some non-standard handling here.<p>
070: *
071: * @param res the result set where the blob is located in
072: * @param name the name of the database column where the blob is located
073: * @return an Output stream from a blob
074: * @throws SQLException if something goes wring
075: */
076: public static OutputStream getOutputStreamFromBlob(ResultSet res,
077: String name) throws SQLException {
078:
079: // TODO: perform blob check only once and store Oracle version in a static private member
080: // TODO: best do this during system startup / db init phase once
081: Blob blob = res.getBlob(name);
082: try {
083: // jdbc standard
084: blob.truncate(0);
085: return blob.setBinaryStream(0L);
086: } catch (SQLException e) {
087: // oracle 9 & 8 (if using the same jdbc driver as provided by oracle9: ojdbc14.jar)
088: ((oracle.sql.BLOB) blob).trim(0);
089: return ((oracle.sql.BLOB) blob).getBinaryOutputStream();
090: }
091: }
092:
093: /**
094: * @see org.opencms.db.I_CmsUserDriver#initSqlManager(String)
095: */
096: public org.opencms.db.generic.CmsSqlManager initSqlManager(
097: String classname) {
098:
099: return CmsSqlManager.getInstance(classname);
100: }
101:
102: /**
103: * @see org.opencms.db.I_CmsUserDriver#writeUserInfo(CmsDbContext, CmsUUID, String, Object)
104: */
105: public void writeUserInfo(CmsDbContext dbc, CmsUUID userId,
106: String key, Object value) throws CmsDataAccessException {
107:
108: PreparedStatement stmt = null;
109: Connection conn = null;
110:
111: try {
112:
113: // get connection
114: conn = m_sqlManager.getConnection(dbc);
115:
116: // write data to database
117: stmt = m_sqlManager.getPreparedStatement(conn,
118: "C_ORACLE_USERDATA_WRITE_3");
119: stmt.setString(1, userId.toString());
120: stmt.setString(2, key);
121: stmt.setString(3, value.getClass().getName());
122: stmt.executeUpdate();
123: } catch (SQLException e) {
124: throw new CmsDbSqlException(
125: org.opencms.db.generic.Messages
126: .get()
127: .container(
128: org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
129: CmsDbSqlException
130: .getErrorQuery(stmt)), e);
131: } finally {
132: m_sqlManager.closeAll(dbc, conn, stmt, null);
133: }
134: internalUpdateUserInfo(dbc, userId, key, value);
135: }
136:
137: /**
138: * Updates the given user information entry.<p>
139: *
140: * @param dbc the current database context
141: * @param userId the id of the user to update
142: * @param key the user info entry key
143: * @param value the user info entry value
144: *
145: * @throws CmsDataAccessException if something goes wrong
146: */
147: protected void internalUpdateUserInfo(CmsDbContext dbc,
148: CmsUUID userId, String key, Object value)
149: throws CmsDataAccessException {
150:
151: PreparedStatement stmt = null;
152: PreparedStatement commit = null;
153: ResultSet res = null;
154: Connection conn = null;
155:
156: boolean wasInTransaction = false;
157:
158: try {
159: // get connection
160: conn = m_sqlManager.getConnection(dbc);
161: wasInTransaction = !conn.getAutoCommit();
162: if (!wasInTransaction) {
163: conn.setAutoCommit(false);
164: }
165:
166: // update user_info in this special way because of using blob
167: stmt = m_sqlManager.getPreparedStatement(conn,
168: "C_ORACLE_USERDATA_UPDATE_2");
169: stmt.setString(1, userId.toString());
170: stmt.setString(2, key);
171: res = ((DelegatingResultSet) stmt.executeQuery())
172: .getInnermostDelegate();
173: if (!res.next()) {
174: throw new CmsDbEntryNotFoundException(Messages.get()
175: .container(Messages.ERR_NO_USER_WITH_ID_1,
176: userId));
177: }
178: // write serialized user info
179: OutputStream output = getOutputStreamFromBlob(res,
180: "DATA_VALUE");
181: output.write(CmsDataTypeUtil.dataSerialize(value));
182: output.close();
183: value = null;
184:
185: if (!wasInTransaction) {
186: commit = m_sqlManager.getPreparedStatement(conn,
187: "C_COMMIT");
188: commit.execute();
189: m_sqlManager.closeAll(dbc, null, commit, null);
190: }
191: m_sqlManager.closeAll(dbc, null, stmt, res);
192:
193: // this is needed so the finally block works correctly
194: commit = null;
195: stmt = null;
196: res = null;
197:
198: if (!wasInTransaction) {
199: conn.setAutoCommit(true);
200: }
201: } catch (SQLException e) {
202: throw new CmsDbSqlException(
203: org.opencms.db.generic.Messages
204: .get()
205: .container(
206: org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
207: CmsDbSqlException
208: .getErrorQuery(stmt)), e);
209: } catch (IOException e) {
210: throw new CmsDbIoException(Messages.get().container(
211: Messages.ERR_SERIALIZING_USER_DATA_1, userId), e);
212: } finally {
213: org.opencms.db.oracle.CmsSqlManager.closeAllInTransaction(
214: m_sqlManager, dbc, conn, stmt, res, commit,
215: wasInTransaction);
216: }
217: }
218: }
|