KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > store > impl > rdbms > OracleRDBMSAdapter


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/stores/org/apache/slide/store/impl/rdbms/OracleRDBMSAdapter.java,v 1.9.2.3 2004/11/13 14:42:00 dsavazzi Exp $
3  * $Revision: 1.9.2.3 $
4  * $Date: 2004/11/13 14:42:00 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2003 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.store.impl.rdbms;
25
26 import java.io.*;
27 import java.lang.reflect.*;
28 import java.sql.Connection JavaDoc;
29 import java.sql.PreparedStatement JavaDoc;
30 import java.sql.CallableStatement JavaDoc;
31 import java.sql.ResultSet JavaDoc;
32 import java.sql.SQLException JavaDoc;
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 /**
43  * Adapter for Oracle 9 and 10.
44  *
45  * @version $Revision: 1.9.2.3 $
46  */

47 public class OracleRDBMSAdapter extends CommonRDBMSAdapter implements SequenceAdapter {
48
49     // Constructor
50

51     public OracleRDBMSAdapter(Service service, Logger logger)
52         throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc
53     {
54         super(service, logger);
55         
56         Class JavaDoc bCls = Class.forName("oracle.sql.BLOB");
57         blobGetOutStreamMethod = bCls.getMethod("getBinaryOutputStream", new Class JavaDoc[] {});
58     }
59
60
61     // Public Methods
62

63     protected void storeContent(
64         Connection JavaDoc connection, Uri uri,
65         NodeRevisionDescriptor revisionDescriptor,
66         NodeRevisionContent revisionContent) throws IOException, SQLException JavaDoc
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                 // fix RevisionDescriptor Content Length
83
if (revisionDescriptor.getContentLength() == -1) {
84                     revisionDescriptor.setContentLength(ziputil.getInitialContentLength());
85                 }
86                 
87                 blobLength = ziputil.getContentLength();
88             } else {
89                 // fix RevisionDescriptor Content Length
90
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 JavaDoc 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                 // do these two lines using reflection
136
// BLOB blob = (BLOB) statement.getBlob(2);
137
// OutputStream os = blob.getBinaryOutputStream();
138

139                 Object JavaDoc bObj = statement.getBlob(2);
140                 OutputStream os = null;
141                 try {
142                     os = (OutputStream) blobGetOutStreamMethod.invoke(bObj, new Object JavaDoc[] {});
143                 } catch (Exception JavaDoc 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 JavaDoc ex) {
167                         // ignore
168
}
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                         // XXX some JDBC drivers seem to close the stream upon
181
// closing of
182
// the statement; if so this will raise an IOException
183
// silently ignore it...
184
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 JavaDoc normalizeSequenceName(String JavaDoc sequenceName) {
196         return sequenceName.replace('-', '_').toUpperCase() + "_SEQ";
197     }
198     
199     public boolean isSequenceSupported(Connection JavaDoc conn) {
200         return true;
201     }
202
203     public boolean createSequence(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
204
205         String JavaDoc query = "CREATE SEQUENCE \"" + normalizeSequenceName(sequenceName) + "\"";
206
207         PreparedStatement JavaDoc statement = null;
208
209         try {
210             statement = conn.prepareStatement(query);
211             statement.executeUpdate();
212             return true;
213         } catch (SQLException JavaDoc e) {
214             throw new ServiceAccessException(service, e);
215         } finally {
216             close(statement);
217         }
218
219     }
220
221     public long nextSequenceValue(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
222         String JavaDoc selectQuery = "SELECT \"" + normalizeSequenceName(sequenceName)+"\".nextval FROM DUAL";
223
224         PreparedStatement JavaDoc selectStatement = null;
225         ResultSet JavaDoc 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 JavaDoc e) {
236             throw new ServiceAccessException(service, e);
237         } finally {
238             close(selectStatement, res);
239         }
240     }
241
242     public boolean sequenceExists(Connection JavaDoc conn, String JavaDoc sequenceName) throws ServiceAccessException {
243
244         PreparedStatement JavaDoc selectStatement = null;
245         ResultSet JavaDoc 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 JavaDoc e) {
253             return false;
254         } finally {
255             close(selectStatement, res);
256         }
257     }
258
259     
260     // Attributes
261

262     protected Method blobGetOutStreamMethod;
263 }
264
265
266
Popular Tags