📄 cmsdbaccess.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/file/oraclesql/Attic/CmsDbAccess.java,v $
* Date : $Date: 2003/01/21 13:27:35 $
* Version: $Revision: 1.8 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.file.oraclesql;
import com.opencms.boot.I_CmsLogChannels;
import com.opencms.core.A_OpenCms;
import com.opencms.core.CmsException;
import com.opencms.core.I_CmsConstants;
import com.opencms.file.CmsBackupProject;
import com.opencms.file.CmsGroup;
import com.opencms.file.CmsUser;
import com.opencms.util.SqlHelper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Hashtable;
import java.util.Vector;
import oracle.jdbc.driver.OracleResultSet;
import source.org.apache.java.util.Configurations;
/**
* This is the generic access module to load and store resources from and into
* the database.
*
* @author Andreas Schouten
* @author Michael Emmerich
* @author Hanjo Riege
* @author Anders Fugmann
* @version $Revision: 1.8 $ $Date: 2003/01/21 13:27:35 $ *
*/
public class CmsDbAccess extends com.opencms.file.genericSql.CmsDbAccess implements I_CmsConstants, I_CmsLogChannels {
/**
* Instanciates the access-module and sets up all required modules and connections.
* @param config The OpenCms configuration.
* @throws CmsException Throws CmsException if something goes wrong.
*/
public CmsDbAccess(Configurations config)
throws CmsException {
super(config);
}
/**
* Creates a serializable object in the systempropertys.
*
* @param name The name of the property.
* @param object The property-object.
* @return object The property-object.
* @throws CmsException Throws CmsException if something goes wrong.
*/
public Serializable addSystemProperty(String name, Serializable object) throws CmsException {
byte[] value;
PreparedStatement statement = null;
PreparedStatement statement2 = null;
PreparedStatement nextStatement = null;
Connection con = null;
ResultSet res = null;
try {
int id = nextId(C_TABLE_SYSTEMPROPERTIES);
// serialize the object
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(object);
oout.close();
value = bout.toByteArray();
// create the object
// first insert the new systemproperty with empty systemproperty_value, then update
// the systemproperty_value. These two steps are necessary because of using Oracle BLOB
con = DriverManager.getConnection(m_poolName);
statement = con.prepareStatement(m_cq.get("C_ORACLE_SYSTEMPROPERTIES_FORINSERT"));
statement.setInt(1, id);
statement.setString(2, name);
//statement.setBytes(3,value);
statement.executeUpdate();
//statement.close();
// now update the systemproperty_value
statement2 = con.prepareStatement(m_cq.get("C_ORACLE_SYSTEMPROPERTIES_FORUPDATE"));
statement2.setInt(1, id);
con.setAutoCommit(false);
res = statement2.executeQuery();
while (res.next()) {
oracle.sql.BLOB blob = ((OracleResultSet) res).getBLOB("SYSTEMPROPERTY_VALUE");
ByteArrayInputStream instream = new ByteArrayInputStream(value);
OutputStream outstream = blob.getBinaryOutputStream();
byte[] chunk = new byte[blob.getChunkSize()];
int i = -1;
while ((i = instream.read(chunk)) != -1) {
outstream.write(chunk, 0, i);
}
instream.close();
outstream.close();
}
// for the oracle-driver commit or rollback must be executed manually
// because setAutoCommit = false
nextStatement = con.prepareStatement(m_cq.get("C_COMMIT"));
nextStatement.execute();
nextStatement.close();
con.setAutoCommit(true);
} catch (SQLException e) {
throw new CmsException("[" + this.getClass().getName() + "]" + e.getMessage(), CmsException.C_SQL_ERROR, e);
} catch (IOException e) {
throw new CmsException("[" + this.getClass().getName() + "]" + CmsException.C_SERIALIZATION, e);
} finally {
if (res != null) {
try {
res.close();
} catch (SQLException se) {
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException exc){
}
}
if (statement2 != null) {
try {
statement2.close();
} catch (SQLException exc){
}
try {
nextStatement = con.prepareStatement(m_cq.get("C_ROLLBACK"));
nextStatement.execute();
} catch (SQLException exc){
// nothing to do here
}
}
if (nextStatement != null) {
try {
nextStatement.close();
} catch (SQLException exc){
}
}
if (con != null) {
try {
con.setAutoCommit(true);
} catch (SQLException exc){
// nothing to do here
}
try {
con.close();
} catch (SQLException e){
}
}
}
return readSystemProperty(name);
}
/**
* Adds a user to the database.
*
* @param name username
* @param password user-password
* @param description user-description
* @param firstname user-firstname
* @param lastname user-lastname
* @param email user-email
* @param lastlogin user-lastlogin
* @param lastused user-lastused
* @param flags user-flags
* @param additionalInfos user-additional-infos
* @param defaultGroup user-defaultGroup
* @param address user-defauladdress
* @param section user-section
* @param type user-type
*
* @return the created user.
* @throws thorws CmsException if something goes wrong.
*/
public CmsUser addUser(String name, String password, String description, String firstname, String lastname, String email, long lastlogin, long lastused, int flags, Hashtable additionalInfos, CmsGroup defaultGroup, String address, String section, int type) throws CmsException {
int id = nextId(C_TABLE_USERS);
byte[] value = null;
PreparedStatement statement = null;
PreparedStatement statement2 = null;
PreparedStatement nextStatement = null;
Connection con = null;
ResultSet res = null;
try {
// serialize the hashtable
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oout = new ObjectOutputStream(bout);
oout.writeObject(additionalInfos);
oout.close();
value = bout.toByteArray();
// write data to database
// first insert the data without user_info
con = DriverManager.getConnection(m_poolName);
statement = con.prepareStatement(m_cq.get("C_ORACLE_USERSFORINSERT"));
statement.setInt(1, id);
statement.setString(2, name);
// crypt the password with MD5
statement.setString(3, digest(password));
statement.setString(4, digest(""));
statement.setString(5, checkNull(description));
statement.setString(6, checkNull(firstname));
statement.setString(7, checkNull(lastname));
statement.setString(8, checkNull(email));
statement.setTimestamp(9, new Timestamp(lastlogin));
statement.setTimestamp(10, new Timestamp(lastused));
statement.setInt(11, flags);
//statement.setBytes(12,value);
statement.setInt(12, defaultGroup.getId());
statement.setString(13, checkNull(address));
statement.setString(14, checkNull(section));
statement.setInt(15, type);
statement.executeUpdate();
statement.close();
// now update user_info of the new user
statement2 = con.prepareStatement(m_cq.get("C_ORACLE_USERSFORUPDATE"));
statement2.setInt(1, id);
con.setAutoCommit(false);
res = statement2.executeQuery();
while (res.next()) {
oracle.sql.BLOB blob = ((OracleResultSet) res).getBLOB("USER_INFO");
ByteArrayInputStream instream = new ByteArrayInputStream(value);
OutputStream outstream = blob.getBinaryOutputStream();
byte[] chunk = new byte[blob.getChunkSize()];
int i = -1;
while ((i = instream.read(chunk)) != -1) {
outstream.write(chunk, 0, i);
}
instream.close();
outstream.close();
}
statement2.close();
res.close();
// for the oracle-driver commit or rollback must be executed manually
// because setAutoCommit = false in CmsDbPool.CmsDbPool
nextStatement = con.prepareStatement(m_cq.get("C_COMMIT"));
nextStatement.execute();
nextStatement.close();
con.setAutoCommit(true);
} catch (SQLException e) {
throw new CmsException("[" + this.getClass().getName() + "]" + e.getMessage(), CmsException.C_SQL_ERROR, e);
} catch (IOException e) {
throw new CmsException("[CmsAccessUserInfoMySql/addUserInformation(id,object)]:" + CmsException.C_SERIALIZATION, e);
} finally {
if (res != null) {
try {
res.close();
} catch (SQLException se) {
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException exc){
}
}
if (statement2 != null) {
try {
statement2.close();
} catch (SQLException exc){
}
try {
nextStatement = con.prepareStatement(m_cq.get("C_ROLLBACK"));
nextStatement.execute();
} catch (SQLException se) {
}
}
if (nextStatement != null) {
try {
nextStatement.close();
} catch (SQLException exc){
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -