001 /** 002 * Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 003 * This program is free software; you can redistribute it and/or modify 004 * it under the terms of version 2 of the GNU General Public License as 005 * published by the Free Software Foundation. 006 * There are special exceptions to the terms and conditions of the GPL 007 * as it is applied to this software. See the GNU General Public License for more details. 008 * 009 * This program is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 012 * GNU General Public License for more details. 013 * 014 * You should have received a copy of the GNU General Public License 015 * along with this program; if not, write to the Free Software 016 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 017 */ 018 019 package org.dbreplicator.replication.column; 020 021 import java.io.*; 022 import java.sql.*; 023 import java.util.*; 024 025 import org.dbreplicator.replication.*; 026 import org.dbreplicator.replication.xml.*; 027 import org.dbreplicator.replication.DBHandler.AbstractDataBaseHandler; 028 029 //import test.*; 030 031 /** 032 * As ClassCast was occuring when setClob was done in oracle ,so we have kept new oracleClobObject 033 *This class is also used by postgresql as no clob data was coming from oracle to postgresql 034 */ 035 public class ClobStreamObject extends AbstractColumnObject 036 { 037 038 int sqlType; 039 AbstractDataBaseHandler abstractDBHandler; 040 041 /** 042 * sets the SQL type fro clob datatype 043 * @param sqlType0 044 */ 045 046 public ClobStreamObject(int sqlType0,AbstractDataBaseHandler abstractDBHandler0) 047 { 048 // System.out.println("OracleClobObject.OracleClobObject(sqlType0)"); 049 sqlType = sqlType0; 050 abstractDBHandler=abstractDBHandler0; 051 } 052 053 /** 054 * set the value for the corresponding datatype i.e CLob 055 * @param pst 056 * @param element 057 * @param index 058 * @throws SQLException 059 */ 060 public void setColumnObject(PreparedStatement pst, XMLElement element, 061 int index) throws SQLException 062 { 063 XMLElement[] elements = (XMLElement[]) element.getChildElements().toArray(new 064 XMLElement[2]); 065 int start = 0; 066 int length = 0; 067 try 068 { 069 start = Integer.parseInt(elements[0].elementValue); 070 length = Integer.parseInt(elements[1].elementValue); 071 // System.out.println(" start = "+start+" length ="+length); 072 } 073 catch (NullPointerException ex) 074 { 075 String value = element.elementValue; 076 int lenghtIndex = value.indexOf("length"); 077 int startIndex = value.indexOf("start"); 078 //System.out.println(" lenghtIndex ="+lenghtIndex+" startIndex = "+startIndex); 079 if (lenghtIndex == -1 && startIndex == -1) 080 { 081 pst.setObject(index, value); 082 return; 083 } 084 else if (lenghtIndex != -1) 085 { 086 start = Integer.parseInt(value.substring(5, lenghtIndex)); 087 length = Integer.parseInt(value.substring(lenghtIndex + 6)); 088 //System.out.println(" start = "+start+" length = "+length); 089 } 090 } 091 if (length == -1) 092 { 093 pst.setNull(index, Types.CLOB); 094 } 095 else 096 { 097 RClob rclob = new RClob(start, length); 098 InputStream inputstream = rclob.getAsciiStream(); 099 InputStreamReader reader = new InputStreamReader(inputstream); 100 try { 101 pst.setCharacterStream(index, reader, inputstream.available()); 102 } 103 catch (IOException ex1) { 104 // ex1.printStackTrace(); 105 RepConstants.writeERROR_FILE(ex1); 106 } 107 // pst.setClob(index, new RClob(start, length)); 108 } 109 } 110 111 public void setColumnObject(PreparedStatement pst, String value, int index) throws SQLException 112 { 113 int start = -1, length = -1; 114 int lenghtIndex = value.indexOf("length"); 115 int startIndex = value.indexOf("start"); 116 if (lenghtIndex == -1 && startIndex == -1) 117 { 118 pst.setObject(index, value); 119 return; 120 } 121 else if (lenghtIndex != -1) 122 { 123 start = Integer.parseInt(value.substring(5, lenghtIndex)); 124 length = Integer.parseInt(value.substring(lenghtIndex + 6)); 125 } 126 if (length == -1) 127 { 128 pst.setNull(index, Types.CLOB); 129 } 130 else 131 { 132 pst.setClob(index, new RClob(start, length)); 133 } 134 } 135 136 /** 137 * writes a clob values in XML file 138 * @param os 139 * @param rs 140 * @param index 141 * @throws IOException 142 * @throws SQLException 143 */ 144 145 public void write(Writer os, ResultSet rs, int index,ArrayList encodedCols,String col) throws 146 SQLException, IOException 147 { 148 Object objrowValue = getObject(rs, index); 149 InputStream rowValue; 150 if (objrowValue == null) 151 { 152 rowValue = null; 153 } 154 else 155 { 156 rowValue = (InputStream) objrowValue; 157 // new ByteArrayInputStream((objrowValue.toString()).getBytes()); 158 159 // System.out.println("<--------------------PRINT------------------------------->"); 160 // int len = rowValue.available(); 161 // System.out.println(" Length of data in Client ="+len); 162 // byte[] bClient=new byte[len]; 163 // int noOfByteRead1=rowValue.read(bClient); 164 // System.out.println("noOfByteRead1 "+noOfByteRead1+" clobObject = "+new String(bClient)); 165 // System.out.println("<--------------------PRINT------------------------------->"); 166 } 167 os.write("<start>"); 168 os.write("" + clobst.getStreamStart()); 169 os.write("</start>"); 170 //RepPrinter.print(" Bfter Writing the Clob to file " + clobst); 171 os.write("<length>"); 172 if (rowValue != null) 173 { 174 os.write("" + clobst.write(rowValue)); 175 } 176 else 177 { 178 os.write("-1"); 179 } 180 //RepPrinter.print(" After Writing the Clob to file " +clobst); 181 os.write("</length>"); 182 } 183 184 /** 185 * puts the value for the column against the column Name 186 * @param os 187 * @param rows 188 * @param oldResultSet 189 * @param index 190 * @param modifiedColumns 191 * @param columnName 192 * @throws SQLException 193 * @throws IOException 194 */ 195 196 public void writeUpdate(Writer os, ResultSet rows, 197 ResultSet oldResultSet 198 , int index, HashMap modifiedColumns, 199 String columnName,ArrayList encodedCols) throws SQLException, IOException 200 { 201 InputStream obj1 = null; 202 203 try 204 { 205 //System.out.println("Object = "+rows.getObject(index)+" CLASS rows.getObject(index) ="+rows.getObject(index).getClass()); 206 Object clobObject =rows.getObject(index); 207 if(clobObject instanceof Clob) { 208 Clob clob = (Clob) clobObject; 209 obj1 = clob.getAsciiStream(); 210 } else { 211 obj1 = new ByteArrayInputStream( ( (String) (clobObject)).getBytes()); 212 } 213 // obj1 = (InputStream)rows.getObject(index); 214 if (obj1 == null) 215 { 216 write(os, null,encodedCols,columnName); 217 } 218 else 219 { 220 String data = getClobNotNullData(obj1); 221 write(os, data,encodedCols,columnName); 222 modifiedColumns.put(columnName, data); 223 } 224 } 225 catch (Exception ex) 226 { 227 RepConstants.writeERROR_FILE(ex); 228 } 229 230 } 231 232 public void writeUpdateBKUP_11_17_04(OutputStreamWriter os, ResultSet rows, 233 ResultSet oldResultSet 234 , int index, HashMap modifiedColumns, 235 String columnName) throws SQLException, IOException 236 { 237 Clob newObject = null; 238 Clob oldObject = null; 239 try 240 { 241 newObject = (Clob) rows.getObject(index); 242 oldObject = (Clob) oldResultSet.getObject(index); 243 244 // InputStream newObject = obj1.getBinaryStream();// new ByteArrayInputStream(newValue.getBytes()); 245 // InputStream oldObject = obj2.getBinaryStream();//new ByteArrayInputStream((obj2.toString()).getBytes()); 246 247 if (newObject == null) 248 { 249 // write(os, null); 250 if (oldObject != null) 251 { 252 modifiedColumns.put(columnName, "NULL"); 253 } 254 } 255 else 256 { 257 String data = getClobNotNullData(newObject.getAsciiStream()); 258 259 int legthOfStream = newObject.getAsciiStream().available(); 260 byte[] buf = new byte[legthOfStream]; 261 int noOfBytesRead = newObject.getAsciiStream().read(buf); 262 String InputString = new String(buf); 263 // write(os, data); 264 if (oldObject != null) 265 { 266 if (!checkInputStreamEquality(oldObject.getAsciiStream(), 267 newObject.getAsciiStream())) 268 { 269 modifiedColumns.put(columnName, data); 270 } 271 } 272 else 273 { 274 modifiedColumns.put(columnName, data); 275 } 276 277 } 278 } 279 catch (Exception ex) 280 { 281 RepConstants.writeERROR_FILE(ex); 282 } 283 284 } 285 286 /* This method was previously written and running fine */ 287 public void writeUpdate1(OutputStreamWriter os, ResultSet rows, 288 ResultSet oldResultSet 289 , int index, HashMap modifiedColumns, 290 String columnName) throws SQLException, IOException 291 { 292 Clob newObject = rows.getClob(index); 293 //RepPrinter.print(" Inside ClobObject newObject =" + newObject); 294 Clob oldObject = oldResultSet.getClob(index); 295 //RepPrinter.print(" Inside ClobObject oldObject =" + oldObject); 296 if (newObject == null) 297 { 298 // write(os, "NULL"); 299 if (oldObject != null) 300 { 301 modifiedColumns.put(columnName, "NULL"); 302 } 303 } 304 else 305 { 306 String data = getClobNotNullData(newObject); 307 // write(os, data); 308 if (oldObject != null) 309 { 310 if (!checkBlobEquality(oldObject, newObject)) 311 { 312 modifiedColumns.put(columnName, data); 313 } 314 } 315 else 316 { 317 modifiedColumns.put(columnName, data); 318 } 319 320 } 321 } 322 323 /*public void writeUpdate1(OutputStreamWriter os, ResultSet rows, ResultSet oldResultSet 324 , int index, HashMap modifiedColumns, String columnName) throws SQLException, IOException{ 325 Clob newObject = rows.getClob(index); 326 Clob oldObject = oldResultSet.getClob(index); 327 328 if( newObject == null ){ 329 String data = getBlobNullData(); 330 write(os,data); 331 if( oldObject != null ) 332 modifiedColumns.put( columnName ,data); 333 } 334 else { 335 String data = getBlobNotNullData(newObject); 336 write(os, data); 337 try { 338 if (oldObject != null) { 339 if (!checkBlobEquality(oldObject,newObject)) { 340 modifiedColumns.put(columnName, data); 341 } 342 } 343 else { 344 modifiedColumns.put(columnName, data); 345 } 346 } 347 catch (Exception ex) { 348 RepConstants.writeERROR_FILE(ex); 349 } 350 } 351 }*/ 352 353 public Object getObject(String value) 354 { 355 //RepPrinter.print("ClobObject.getObject(value)" + value); 356 int start = 0, length = 0; 357 try 358 { 359 int lenghtIndex = value.indexOf("length"); 360 start = Integer.parseInt(value.substring(5, lenghtIndex)); 361 length = Integer.parseInt(value.substring(lenghtIndex + 6)); 362 } 363 catch (Exception ex) 364 { 365 RepConstants.writeERROR_FILE(ex); 366 } 367 return new RClob(start, length); 368 } 369 370 /** 371 * returns the Object corresponding to the index passed from the resultSet 372 * @param row 373 * @param index 374 * @return 375 * @throws SQLException 376 */ 377 private Object getObject(ResultSet row, int index) throws SQLException 378 { 379 Object object = row.getObject(index); 380 //System.out.println("Clob object CLASS ="+object.getClass()); 381 if (object == null) 382 { 383 return null; 384 } 385 if(object instanceof Clob ) { 386 Clob clob = (Clob) object; 387 return clob.getAsciiStream(); 388 } else { 389 InputStream objInputStream = new ByteArrayInputStream((object.toString()).getBytes()); 390 return objInputStream; 391 } 392 // return row.getBinaryStream(index); 393 } 394 395 /** 396 * writes a Clob values in XML file 397 * @param os 398 * @param rs 399 * @param index 400 * @throws IOException 401 * @throws SQLException 402 */ 403 private void write(Writer os, Object rowValue,ArrayList encodedCols,String col) throws 404 SQLException, IOException 405 { 406 try 407 { 408 os.write("<![CDATA[" + rowValue.toString() + "]]>"); 409 } 410 catch (NullPointerException ex) 411 { 412 os.write("<start>"); 413 os.write("" + clobst.getStreamStart()); 414 os.write("</start><length>-1</length>"); 415 } 416 } 417 418 /*private void write(OutputStreamWriter os, String rowValue) throws SQLException, IOException { 419 os.write(rowValue); 420 } 421 422 private String getClobDataToWriteInXMLFile1(Object rowValue) { 423 return rowValue != null ? getClobNotNullData1(rowValue) 424 : getClobNullData1(); 425 } 426 427 private String getClobNullData1() { 428 StringBuffer sb = new StringBuffer(); 429 sb.append("<start>").append(clobst.getStreamStart()).append("</start>") 430 .append("<length>").append("-1").append("</length>"); 431 return sb.toString(); 432 } 433 434 private String getClobNotNullData1(Object rowValue) { 435 StringBuffer sb = new StringBuffer(); 436 InputStream objInputStrea; 437 if(rowValue==null) 438 objInputStrea=null; 439 else 440 objInputStrea= new ByteArrayInputStream((rowValue.toString()).getBytes()); 441 sb.append("<start>").append(clobst.getStreamStart()).append("</start>") 442 .append("<length>").append(clobst.write((InputStream)rowValue)) 443 .append("</length>"); 444 return sb.toString(); 445 } 446 */ 447 private String getClobNotNullData(Object rowValue) 448 { 449 StringBuffer sb = new StringBuffer(); 450 try 451 { 452 //InputStream inputStram =new ByteArrayInputStream((rowValue.toString()).getBytes()); 453 sb.append("start").append(clobst.getStreamStart()) 454 .append("length").append(clobst.write( (InputStream) rowValue)); 455 //(InputStream)rowValue) 456 } 457 catch (Exception ex) 458 { 459 RepConstants.writeERROR_FILE(ex); 460 } 461 return sb.toString(); 462 } 463 464 private boolean checkBlobEquality(Clob clob1, Clob clob2) 465 { 466 try 467 { 468 return checkInputStreamEquality(clob1.getAsciiStream(), 469 clob2.getAsciiStream()); 470 } 471 catch (SQLException ex) 472 { 473 return false; 474 } 475 } 476 477 private boolean checkInputStreamEquality(InputStream stream1, 478 InputStream stream2) 479 { 480 try 481 { 482 long len1 = stream1.available(); 483 long len2 = stream2.available(); 484 if (len1 != len2) 485 { 486 return false; 487 } 488 long read = 0; 489 do 490 { 491 byte[] buf1 = new byte[1024]; 492 byte[] buf2 = new byte[1024]; 493 int byte1 = stream1.read(buf1); 494 int byte2 = stream2.read(buf2); 495 if (byte1 != byte2) 496 { 497 return false; 498 } 499 for (int i = 0; i < buf1.length; i++) 500 { 501 if (buf1[i] != buf2[i]) 502 { 503 return false; 504 } 505 } 506 read += buf1.length; 507 } 508 while (read < len1); 509 } 510 catch (IOException ex) 511 { 512 return false; 513 } 514 return true; 515 } 516 517 }

