KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > db > oracle > CmsVfsDriver


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/db/oracle/CmsVfsDriver.java,v $
3  * Date : $Date: 2005/06/24 16:27:52 $
4  * Version: $Revision: 1.36 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.db.oracle;
33
34 import org.opencms.db.CmsDbContext;
35 import org.opencms.db.CmsDbEntryNotFoundException;
36 import org.opencms.db.CmsDbIoException;
37 import org.opencms.db.CmsDbSqlException;
38 import org.opencms.db.generic.CmsSqlManager;
39 import org.opencms.db.generic.Messages;
40 import org.opencms.file.CmsDataAccessException;
41 import org.opencms.file.CmsProject;
42 import org.opencms.util.CmsUUID;
43
44 import java.io.IOException JavaDoc;
45 import java.io.OutputStream JavaDoc;
46 import java.sql.Connection JavaDoc;
47 import java.sql.PreparedStatement JavaDoc;
48 import java.sql.ResultSet JavaDoc;
49 import java.sql.SQLException JavaDoc;
50
51 import org.apache.commons.dbcp.DelegatingResultSet;
52
53 /**
54  * Oracle implementation of the VFS driver methods.<p>
55  *
56  * @author Thomas Weckert
57  * @author Carsten Weinholz
58  *
59  * @version $Revision: 1.36 $
60  *
61  * @since 6.0.0
62  */

63 public class CmsVfsDriver extends org.opencms.db.generic.CmsVfsDriver {
64
65     /**
66      * @see org.opencms.db.I_CmsVfsDriver#createContent(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID, byte[], int)
67      */

68     public void createContent(CmsDbContext dbc, CmsProject project, CmsUUID resourceId, byte[] content, int versionId)
69     throws CmsDataAccessException {
70
71         PreparedStatement JavaDoc stmt = null;
72         Connection JavaDoc conn = null;
73
74         try {
75             conn = m_sqlManager.getConnection(dbc, project.getId());
76             stmt = m_sqlManager.getPreparedStatement(conn, project, "C_ORACLE_CONTENTS_ADD");
77
78             // first insert new file without file_content, then update the file_content
79
// these two steps are necessary because of using BLOBs in the Oracle DB
80
stmt.setString(1, new CmsUUID().toString());
81             stmt.setString(2, resourceId.toString());
82
83             stmt.executeUpdate();
84         } catch (SQLException JavaDoc e) {
85             throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container(
86                 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
87                 CmsDbSqlException.getErrorQuery(stmt)), e);
88         } finally {
89             m_sqlManager.closeAll(dbc, conn, stmt, null);
90         }
91
92         // now update the file content
93
writeContent(dbc, project, resourceId, content);
94     }
95
96     /**
97      * @see org.opencms.db.I_CmsVfsDriver#initSqlManager(String)
98      */

99     public org.opencms.db.generic.CmsSqlManager initSqlManager(String JavaDoc classname) {
100
101         return CmsSqlManager.getInstance(classname);
102     }
103
104     /**
105      * @see org.opencms.db.I_CmsVfsDriver#writeContent(org.opencms.db.CmsDbContext, org.opencms.file.CmsProject, org.opencms.util.CmsUUID, byte[])
106      */

107     public void writeContent(CmsDbContext dbc, CmsProject project, CmsUUID resourceId, byte[] content)
108     throws CmsDataAccessException {
109
110         PreparedStatement JavaDoc stmt = null;
111         PreparedStatement JavaDoc commit = null;
112         PreparedStatement JavaDoc rollback = null;
113         Connection JavaDoc conn = null;
114         ResultSet JavaDoc res = null;
115
116         boolean wasInTransaction = false;
117         try {
118             conn = m_sqlManager.getConnection(dbc, project.getId());
119             stmt = m_sqlManager.getPreparedStatement(conn, project, "C_ORACLE_CONTENTS_UPDATECONTENT");
120
121             wasInTransaction = !conn.getAutoCommit();
122             if (!wasInTransaction) {
123                 conn.setAutoCommit(false);
124             }
125
126             // update the file content in the contents table
127
stmt.setString(1, resourceId.toString());
128             res = ((DelegatingResultSet)stmt.executeQuery()).getInnermostDelegate();
129             if (!res.next()) {
130                 throw new CmsDbEntryNotFoundException(Messages.get().container(
131                     Messages.LOG_READING_RESOURCE_1,
132                     resourceId));
133             }
134
135             // write file content
136
OutputStream JavaDoc output = CmsUserDriver.getOutputStreamFromBlob(res, "FILE_CONTENT");
137             output.write(content);
138             output.close();
139
140             if (!wasInTransaction) {
141                 commit = m_sqlManager.getPreparedStatement(conn, "C_COMMIT");
142                 commit.execute();
143                 m_sqlManager.closeAll(dbc, null, commit, null);
144             }
145
146             m_sqlManager.closeAll(dbc, null, stmt, res);
147
148             commit = null;
149             stmt = null;
150             res = null;
151
152             if (!wasInTransaction) {
153                 conn.setAutoCommit(true);
154             }
155
156         } catch (IOException JavaDoc e) {
157             throw new CmsDbIoException(Messages.get().container(Messages.ERR_WRITING_TO_OUTPUT_STREAM_1, resourceId), e);
158         } catch (SQLException JavaDoc e) {
159             throw new CmsDbSqlException(org.opencms.db.generic.Messages.get().container(
160                 org.opencms.db.generic.Messages.ERR_GENERIC_SQL_1,
161                 CmsDbSqlException.getErrorQuery(stmt)), e);
162         } finally {
163
164             if (res != null) {
165                 try {
166                     res.close();
167                 } catch (SQLException JavaDoc exc) {
168                     // ignore
169
}
170             }
171
172             if (commit != null) {
173                 try {
174                     commit.close();
175                 } catch (SQLException JavaDoc exc) {
176                     // ignore
177
}
178             }
179
180             if (!wasInTransaction) {
181                 if (stmt != null) {
182                     try {
183                         rollback = m_sqlManager.getPreparedStatement(conn, "C_ROLLBACK");
184                         rollback.execute();
185                         rollback.close();
186                     } catch (SQLException JavaDoc se) {
187                         // ignore
188
}
189                     try {
190                         stmt.close();
191                     } catch (SQLException JavaDoc exc) {
192                         // ignore
193
}
194                 }
195                 if (conn != null) {
196                     try {
197                         conn.setAutoCommit(true);
198                         conn.close();
199                     } catch (SQLException JavaDoc se) {
200                         // ignore
201
}
202                 }
203             }
204         }
205     }
206 }
207
Popular Tags