| 1 | |
package org.apache.ojb.broker.platforms; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
import org.apache.ojb.broker.util.logging.Logger; |
| 19 | |
import org.apache.ojb.broker.util.logging.LoggerFactory; |
| 20 | |
|
| 21 | |
import java.io.*; |
| 22 | |
import java.sql.Connection; |
| 23 | |
import java.lang.reflect.InvocationTargetException; |
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public class Oracle9iLobHandler |
| 34 | |
{ |
| 35 | |
|
| 36 | |
protected static Logger logger = LoggerFactory.getLogger(Oracle9iLobHandler.class); |
| 37 | |
|
| 38 | |
private static ClobWrapper createTempCLOB(Connection connection, ClobWrapper clob) |
| 39 | |
{ |
| 40 | |
if (clob == null) { |
| 41 | |
return null; |
| 42 | |
} |
| 43 | |
|
| 44 | |
ClobWrapper tempClob = null; |
| 45 | |
try |
| 46 | |
{ |
| 47 | |
|
| 48 | |
tempClob = ClobWrapper.createTemporary(connection, true, ClobWrapper.getDurationSessionValue()); |
| 49 | |
|
| 50 | |
|
| 51 | |
clob.open(ClobWrapper.getModeReadOnlyValue()); |
| 52 | |
|
| 53 | |
|
| 54 | |
tempClob.open(ClobWrapper.getModeReadWriteValue()); |
| 55 | |
|
| 56 | |
|
| 57 | |
int bytesread; |
| 58 | |
|
| 59 | |
|
| 60 | |
Reader clobReader = clob.getCharacterStream(); |
| 61 | |
|
| 62 | |
|
| 63 | |
Writer tempClobWriter = tempClob.getCharacterOutputStream(); |
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
char[] charbuffer = new char[clob.getBufferSize()]; |
| 68 | |
|
| 69 | |
|
| 70 | |
while ((bytesread = clobReader.read(charbuffer)) != -1) |
| 71 | |
{ |
| 72 | |
tempClobWriter.write(charbuffer, 0, bytesread); |
| 73 | |
} |
| 74 | |
|
| 75 | |
|
| 76 | |
tempClobWriter.flush(); |
| 77 | |
tempClobWriter.close(); |
| 78 | |
clobReader.close(); |
| 79 | |
|
| 80 | |
|
| 81 | |
clob.close(); |
| 82 | |
tempClob.close(); |
| 83 | |
} |
| 84 | |
catch (Exception e) |
| 85 | |
{ |
| 86 | |
logger.error("Error during temporary CLOB write", e); |
| 87 | |
freeTempLOB(tempClob, null); |
| 88 | |
} |
| 89 | |
return tempClob; |
| 90 | |
} |
| 91 | |
|
| 92 | |
public static String convertCLOBtoString(Connection connection, Object nativeclob) |
| 93 | |
{ |
| 94 | |
ClobWrapper temp = new ClobWrapper(); |
| 95 | |
temp.setClob(nativeclob); |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
ClobWrapper clob = createTempCLOB(connection, temp); |
| 100 | |
|
| 101 | |
if (clob == null) { |
| 102 | |
return null; |
| 103 | |
} |
| 104 | |
|
| 105 | |
String retval = null; |
| 106 | |
|
| 107 | |
StringBuffer clobdata = new StringBuffer(); |
| 108 | |
|
| 109 | |
int bytesread = 0; |
| 110 | |
try |
| 111 | |
{ |
| 112 | |
|
| 113 | |
clob.open(ClobWrapper.getModeReadOnlyValue()); |
| 114 | |
|
| 115 | |
Reader clobReader = clob.getCharacterStream(); |
| 116 | |
|
| 117 | |
|
| 118 | |
char[] charbuffer = new char[clob.getBufferSize()]; |
| 119 | |
|
| 120 | |
|
| 121 | |
while ((bytesread = clobReader.read(charbuffer)) != -1) |
| 122 | |
{ |
| 123 | |
clobdata.append(charbuffer, 0, bytesread); |
| 124 | |
} |
| 125 | |
|
| 126 | |
clobReader.close(); |
| 127 | |
|
| 128 | |
clob.close(); |
| 129 | |
retval = clobdata.toString(); |
| 130 | |
clobdata = null; |
| 131 | |
} |
| 132 | |
catch (Exception e) |
| 133 | |
{ |
| 134 | |
logger.error("Error during CLOB read", e); |
| 135 | |
freeTempLOB(clob, null); |
| 136 | |
} |
| 137 | |
return retval; |
| 138 | |
} |
| 139 | |
|
| 140 | |
public static Object createCLOBFromString(Connection conn, String clobData) |
| 141 | |
{ |
| 142 | |
if (clobData == null) { |
| 143 | |
return null; |
| 144 | |
} |
| 145 | |
ClobWrapper clob = null; |
| 146 | |
try |
| 147 | |
{ |
| 148 | |
clob = ClobWrapper.createTemporary(conn, true, ClobWrapper.getDurationSessionValue()); |
| 149 | |
if (clob != null) { |
| 150 | |
|
| 151 | |
clob.open(ClobWrapper.getModeReadWriteValue()); |
| 152 | |
|
| 153 | |
|
| 154 | |
clob.trim(0); |
| 155 | |
|
| 156 | |
|
| 157 | |
Writer tempClobWriter = clob.getCharacterOutputStream(); |
| 158 | |
|
| 159 | |
if (tempClobWriter != null) { |
| 160 | |
|
| 161 | |
tempClobWriter.write(clobData); |
| 162 | |
|
| 163 | |
|
| 164 | |
tempClobWriter.flush(); |
| 165 | |
tempClobWriter.close(); |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
clob.close(); |
| 170 | |
} |
| 171 | |
} |
| 172 | |
catch (InvocationTargetException ite) { |
| 173 | |
Throwable t = ite.getTargetException(); |
| 174 | |
freeTempLOB(clob, null); |
| 175 | |
if (t instanceof java.lang.UnsatisfiedLinkError) { |
| 176 | |
logger.error("Oracle JDBC-driver version does not match installed OCI-driver"); |
| 177 | |
} else { |
| 178 | |
logger.error("Error during temporary CLOB write", t); |
| 179 | |
} |
| 180 | |
} |
| 181 | |
catch (Exception e) |
| 182 | |
{ |
| 183 | |
logger.error("Error during temporary CLOB write", e); |
| 184 | |
freeTempLOB(clob, null); |
| 185 | |
} |
| 186 | |
return clob == null ? null : clob.getClob(); |
| 187 | |
} |
| 188 | |
|
| 189 | |
private static BlobWrapper createTempBLOB(Connection connection, BlobWrapper blob) |
| 190 | |
{ |
| 191 | |
if (blob == null) { |
| 192 | |
return null; |
| 193 | |
} |
| 194 | |
|
| 195 | |
BlobWrapper tempBlob = null; |
| 196 | |
try |
| 197 | |
{ |
| 198 | |
|
| 199 | |
tempBlob = BlobWrapper.createTemporary(connection, true, BlobWrapper.getDurationSessionValue()); |
| 200 | |
|
| 201 | |
|
| 202 | |
blob.open(BlobWrapper.getModeReadOnlyValue()); |
| 203 | |
|
| 204 | |
|
| 205 | |
tempBlob.open(BlobWrapper.getModeReadWriteValue()); |
| 206 | |
|
| 207 | |
|
| 208 | |
int bytesread; |
| 209 | |
|
| 210 | |
|
| 211 | |
InputStream blobInputStream = blob.getBinaryStream(); |
| 212 | |
|
| 213 | |
|
| 214 | |
OutputStream tempBlobOutputStream = tempBlob.getBinaryOutputStream(); |
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
byte[] bytebuffer = new byte[blob.getBufferSize()]; |
| 219 | |
|
| 220 | |
|
| 221 | |
while ((bytesread = blobInputStream.read(bytebuffer)) != -1) |
| 222 | |
{ |
| 223 | |
tempBlobOutputStream.write(bytebuffer, 0, bytesread); |
| 224 | |
} |
| 225 | |
|
| 226 | |
|
| 227 | |
tempBlobOutputStream.flush(); |
| 228 | |
tempBlobOutputStream.close(); |
| 229 | |
blobInputStream.close(); |
| 230 | |
|
| 231 | |
|
| 232 | |
blob.close(); |
| 233 | |
tempBlob.close(); |
| 234 | |
} |
| 235 | |
catch (Exception e) |
| 236 | |
{ |
| 237 | |
logger.error("Error during temporary BLOB write", e); |
| 238 | |
freeTempLOB(null, tempBlob); |
| 239 | |
} |
| 240 | |
return tempBlob; |
| 241 | |
} |
| 242 | |
|
| 243 | |
public static byte[] convertBLOBtoByteArray(Connection connection, Object nativeblob) |
| 244 | |
{ |
| 245 | |
BlobWrapper temp = new BlobWrapper(); |
| 246 | |
temp.setBlob(nativeblob); |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
BlobWrapper blob = createTempBLOB(connection, temp); |
| 251 | |
if (blob == null) { |
| 252 | |
return null; |
| 253 | |
} |
| 254 | |
|
| 255 | |
byte[] retval = null; |
| 256 | |
|
| 257 | |
ByteArrayOutputStream blobdata = new ByteArrayOutputStream(); |
| 258 | |
|
| 259 | |
int bytesread = 0; |
| 260 | |
try |
| 261 | |
{ |
| 262 | |
|
| 263 | |
blob.open(BlobWrapper.getModeReadOnlyValue()); |
| 264 | |
|
| 265 | |
InputStream blobInputStream = blob.getBinaryStream(); |
| 266 | |
|
| 267 | |
|
| 268 | |
byte[] bytebuffer = new byte[blob.getBufferSize()]; |
| 269 | |
|
| 270 | |
|
| 271 | |
while ((bytesread = blobInputStream.read(bytebuffer)) != -1) |
| 272 | |
{ |
| 273 | |
blobdata.write(bytebuffer, 0, bytesread); |
| 274 | |
} |
| 275 | |
|
| 276 | |
blobInputStream.close(); |
| 277 | |
blobdata.flush(); |
| 278 | |
blobdata.close(); |
| 279 | |
|
| 280 | |
|
| 281 | |
blob.close(); |
| 282 | |
retval = blobdata.toByteArray(); |
| 283 | |
blobdata = null; |
| 284 | |
} |
| 285 | |
catch (Exception e) |
| 286 | |
{ |
| 287 | |
logger.error("Error during BLOB read", e); |
| 288 | |
freeTempLOB(null, blob); |
| 289 | |
} |
| 290 | |
return retval; |
| 291 | |
} |
| 292 | |
|
| 293 | |
public static Object createBLOBFromByteArray(Connection conn, byte[] blobData) |
| 294 | |
{ |
| 295 | |
if (blobData == null) { |
| 296 | |
return null; |
| 297 | |
} |
| 298 | |
|
| 299 | |
BlobWrapper blob = null; |
| 300 | |
try |
| 301 | |
{ |
| 302 | |
blob = BlobWrapper.createTemporary(conn, true, BlobWrapper.getDurationSessionValue()); |
| 303 | |
|
| 304 | |
|
| 305 | |
blob.open(BlobWrapper.getModeReadWriteValue()); |
| 306 | |
|
| 307 | |
|
| 308 | |
blob.trim(0); |
| 309 | |
|
| 310 | |
|
| 311 | |
OutputStream tempBlobOutputStream = blob.getBinaryOutputStream(); |
| 312 | |
|
| 313 | |
|
| 314 | |
tempBlobOutputStream.write(blobData); |
| 315 | |
|
| 316 | |
|
| 317 | |
tempBlobOutputStream.flush(); |
| 318 | |
tempBlobOutputStream.close(); |
| 319 | |
|
| 320 | |
|
| 321 | |
blob.close(); |
| 322 | |
} |
| 323 | |
catch (InvocationTargetException ite) { |
| 324 | |
Throwable t = ite.getTargetException(); |
| 325 | |
freeTempLOB(null, blob); |
| 326 | |
if (t instanceof java.lang.UnsatisfiedLinkError) { |
| 327 | |
logger.error("Oracle JDBC-driver version does not match installed OCI-driver"); |
| 328 | |
} else { |
| 329 | |
logger.error("Error during temporary BLOB write", t); |
| 330 | |
} |
| 331 | |
} |
| 332 | |
catch (Exception e) |
| 333 | |
{ |
| 334 | |
logger.error("Error during temporary BLOB write", e); |
| 335 | |
freeTempLOB(null, blob); |
| 336 | |
} |
| 337 | |
return blob == null ? null : blob.getBlob(); |
| 338 | |
} |
| 339 | |
|
| 340 | |
|
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | |
|
| 346 | |
|
| 347 | |
private static void freeTempLOB(ClobWrapper clob, BlobWrapper blob) |
| 348 | |
{ |
| 349 | |
try |
| 350 | |
{ |
| 351 | |
if (clob != null) |
| 352 | |
{ |
| 353 | |
|
| 354 | |
if (clob.isOpen()) |
| 355 | |
{ |
| 356 | |
clob.close(); |
| 357 | |
} |
| 358 | |
|
| 359 | |
|
| 360 | |
clob.freeTemporary(); |
| 361 | |
} |
| 362 | |
|
| 363 | |
if (blob != null) |
| 364 | |
{ |
| 365 | |
|
| 366 | |
if (blob.isOpen()) |
| 367 | |
{ |
| 368 | |
blob.close(); |
| 369 | |
} |
| 370 | |
|
| 371 | |
|
| 372 | |
blob.freeTemporary(); |
| 373 | |
} |
| 374 | |
} |
| 375 | |
catch (Exception e) |
| 376 | |
{ |
| 377 | |
logger.error("Error during temporary LOB release", e); |
| 378 | |
} |
| 379 | |
} |
| 380 | |
|
| 381 | |
} |