| 1 23 24 package org.apache.slide.store.impl.rdbms; 25 26 import java.lang.reflect.Constructor ; 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.sql.Statement ; 32 import java.util.Date ; 33 import java.util.Enumeration ; 34 import java.util.Hashtable ; 35 import java.util.Vector ; 36 37 import org.apache.slide.common.Service; 38 import org.apache.slide.common.ServiceAccessException; 39 import org.apache.slide.common.Uri; 40 import org.apache.slide.content.NodeProperty; 41 import org.apache.slide.content.NodeRevisionContent; 42 import org.apache.slide.content.NodeRevisionDescriptor; 43 import org.apache.slide.content.NodeRevisionDescriptors; 44 import org.apache.slide.content.NodeRevisionNumber; 45 import org.apache.slide.content.RevisionAlreadyExistException; 46 import org.apache.slide.content.RevisionDescriptorNotFoundException; 47 import org.apache.slide.content.RevisionNotFoundException; 48 import org.apache.slide.lock.LockTokenNotFoundException; 49 import org.apache.slide.lock.NodeLock; 50 import org.apache.slide.security.NodePermission; 51 import org.apache.slide.structure.LinkNode; 52 import org.apache.slide.structure.ObjectAlreadyExistsException; 53 import org.apache.slide.structure.ObjectNode; 54 import org.apache.slide.structure.ObjectNotFoundException; 55 import org.apache.slide.util.logger.Logger; 56 57 80 public class OldJDBCAdapter extends AbstractRDBMSAdapter { 81 82 protected static final String LOG_CHANNEL = 83 StandardRDBMSAdapter.class.getName(); 84 86 88 90 protected static final int OBJECTS_URI = 1; 91 protected static final int OBJECTS_CLASS = 2; 92 93 protected static final int CHILDREN_URI = 1; 94 protected static final int CHILDREN_CHILDURI = 2; 95 96 protected static final int LINKS_LINK = 1; 97 protected static final int LINKS_LINKTO = 2; 98 99 101 protected static final int PERMISSIONS_OBJECT = 1; 102 protected static final int PERMISSIONS_REVISION_NUMBER = 2; 103 protected static final int PERMISSIONS_SUBJECT = 3; 104 protected static final int PERMISSIONS_ACTION = 4; 105 protected static final int PERMISSIONS_INHERITABLE = 5; 106 protected static final int PERMISSIONS_NEGATIVE = 6; 107 108 110 protected static final int LOCKS_ID = 1; 111 protected static final int LOCKS_OBJECT = 2; 112 protected static final int LOCKS_SUBJECT = 3; 113 protected static final int LOCKS_TYPE = 4; 114 protected static final int LOCKS_EXPIRATIONDATE = 5; 115 protected static final int LOCKS_INHERITABLE = 6; 116 protected static final int LOCKS_EXCLUSIVE = 7; 117 protected static final int LOCKS_OWNER = 8; 118 119 121 protected static final int REVISIONS_URI = 1; 122 protected static final int REVISIONS_ISVERSIONED = 2; 123 protected static final int REVISIONS_INITIALREVISION = 3; 124 125 protected static final int WORKINGREVISION_URI = 1; 126 protected static final int WORKINGREVISION_BASEREVISION = 2; 127 protected static final int WORKINGREVISION_NUMBER = 3; 128 129 protected static final int LATESTREVISIONS_URI = 1; 130 protected static final int LATESTREVISIONS_BRANCHNAME = 2; 131 protected static final int LATESTREVISIONS_NUMBER = 3; 132 133 protected static final int BRANCHES_URI = 1; 134 protected static final int BRANCHES_NUMBER = 2; 135 protected static final int BRANCHES_CHILDNUMBER = 3; 136 137 protected static final int REVISION_URI = 1; 138 protected static final int REVISION_NUMBER = 2; 139 protected static final int REVISION_BRANCHNAME = 3; 140 141 protected static final int LABEL_URI = 1; 142 protected static final int LABEL_NUMBER = 2; 143 protected static final int LABEL_LABEL = 3; 144 145 protected static final int PROPERTY_URI = 1; 146 protected static final int PROPERTY_NUMBER = 2; 147 protected static final int PROPERTY_NAME = 3; 148 protected static final int PROPERTY_VALUE = 4; 149 protected static final int PROPERTY_NAMESPACE = 5; 150 protected static final int PROPERTY_TYPE = 6; 151 protected static final int PROPERTY_PROTECTED = 7; 152 153 public OldJDBCAdapter(Service service, Logger logger) { 154 super(service, logger); 155 } 156 157 159 166 public ObjectNode retrieveObject(Connection connection, Uri uri) 167 throws ServiceAccessException, ObjectNotFoundException { 168 169 ObjectNode result = null; 170 PreparedStatement statement = null; 171 172 try { 173 174 statement = 175 connection.prepareStatement( 176 "select * from objects where uri= ?"); 177 statement.setString(1, uri.toString()); 178 179 ResultSet res = statement.executeQuery(); 180 181 183 String className; 184 185 if (res.next()) { 186 className = res.getString(OBJECTS_CLASS); 188 } else { 189 throw new ObjectNotFoundException(uri); 191 } 192 193 closeStatement(statement); 194 195 statement = 197 connection.prepareStatement( 198 "select * from children where uri= ?"); 199 statement.setString(1, uri.toString()); 200 res = statement.executeQuery(); 201 202 Vector childrenVector = new Vector (); 203 204 while (res.next()) { 206 childrenVector.addElement(res.getString(CHILDREN_CHILDURI)); 208 } 209 closeStatement(statement); 210 211 statement = 212 connection.prepareStatement( 213 "select * from links where linkto= ?"); 214 statement.setString(1, uri.toString()); 215 res = statement.executeQuery(); 216 217 Vector linksVector = new Vector (); 218 219 while (res.next()) { 221 linksVector.addElement(res.getString(LINKS_LINKTO)); 223 } 224 225 closeStatement(statement); 226 227 if (className.equals("org.apache.slide.structure.LinkNode")) { 228 229 String linkTo = new String (); 230 statement = 231 connection.prepareStatement( 232 "select * from links where link= ?"); 233 statement.setString(1, uri.toString()); 234 res = statement.executeQuery(); 235 236 if (res.next()) 237 linkTo = res.getString(LINKS_LINKTO); 238 239 closeStatement(statement); 240 241 result = 242 new LinkNode( 243 uri.toString(), 244 childrenVector, 245 linksVector, 246 linkTo); 247 248 } else { 249 250 try { 251 Class objclass = Class.forName(className); 252 253 Class [] argClasses = 254 { 255 Class.forName("java.lang.String"), 256 Class.forName("java.util.Vector"), 257 Class.forName("java.util.Vector")}; 258 Object [] arguments = 259 { uri.toString(), childrenVector, linksVector }; 260 261 Constructor constructor = 262 objclass.getConstructor(argClasses); 263 result = (ObjectNode) constructor.newInstance(arguments); 264 } catch (Exception e) { 265 throw createException(e, uri); 267 } 268 269 } 270 271 } catch (SQLException e) { 272 throw createException(e, uri); 273 } finally { 274 closeStatement(statement); 275 } 276 return result; 277 } 278 279 286 public void storeObject(Connection connection, Uri uri, ObjectNode object) 287 throws ServiceAccessException, ObjectNotFoundException { 288 289 PreparedStatement statement = null; 290 291 try { 292 statement = 293 connection.prepareStatement( 294 "select * from objects where uri= ?"); 295 statement.setString(1, uri.toString()); 296 297 ResultSet res = statement.executeQuery(); 298 299 301 if (!res.next()) { 302 throw new ObjectNotFoundException(uri); 303 } 304 305 closeStatement(statement); 306 307 statement = 309 connection.prepareStatement( 310 "delete from children where uri= ?"); 311 statement.setString(1, object.getUri()); 312 statement.execute(); 313 closeStatement(statement); 314 315 statement = null; 316 Enumeration children = object.enumerateChildren(); 317 while (children.hasMoreElements()) { 318 if (statement == null) { 319 statement = 320 connection.prepareStatement( 321 "insert into children values(?, ?)"); 322 } 323 statement.setString(1, object.getUri()); 324 statement.setString(2, (String ) children.nextElement()); 325 statement.execute(); 326 } 327 closeStatement(statement); 328 329 341 342 statement = 344 connection.prepareStatement("delete from links where link= ?"); 345 statement.setString(1, object.getUri()); 346 statement.execute(); 347 closeStatement(statement); 348 349 if (object instanceof LinkNode) { 350 statement = 351 connection.prepareStatement( 352 "insert into links values(?,?)"); 353 statement.setString(1, object.getUri()); 354 statement.setString(2, ((LinkNode) object).getLinkedUri()); 355 statement.execute(); 356 closeStatement(statement); 357 } 358 359 } catch (SQLException e) { 360 throw createException(e, uri); 361 } finally { 362 closeStatement(statement); 363 } 364 365 } 366 367 376 public void createObject(Connection connection, Uri uri, ObjectNode object) 377 throws ServiceAccessException, ObjectAlreadyExistsException { 378 379 PreparedStatement statement = null; 380 try { 381 382 String className = object.getClass().getName(); 383 statement = 384 connection.prepareStatement( 385 "select * from objects where uri= ?"); 386 statement.setString(1, uri.toString()); 387 ResultSet res = statement.executeQuery(); 388 if (res.next()) { 390 throw new ObjectAlreadyExistsException(uri.toString()); 391 } 392 393 closeStatement(statement); 394 statement = 395 connection.prepareStatement("insert into objects values(?,?)"); 396 statement.setString(1, uri.toString()); 397 statement.setString(2, className); 398 statement.execute(); 399 closeStatement(statement); 400 statement = null; 401 Enumeration children = object.enumerateChildren(); 403 while (children.hasMoreElements()) { 404 if (statement == null) { 405 statement = 406 connection.prepareStatement( 407 "insert into children values(?,?)"); 408 } 409 statement.setString(1, uri.toString()); 410 statement.setString(2, (String ) children.nextElement()); 411 statement.execute(); 412 } 413 closeStatement(statement); 414 if (object instanceof LinkNode) { 425 statement = 426 connection.prepareStatement( 427 "insert into links values(?,?)"); 428 statement.setString(1, uri.toString()); 429 statement.setString(2, ((LinkNode) object).getLinkedUri()); 430 statement.execute(); 431 closeStatement(statement); 432 } 433 434 } catch (SQLException e) { 435 throw createException(e, uri); 436 } finally { 437 closeStatement(statement); 438 } 439 440 } 441 442 449 public void removeObject(Connection connection, Uri uri, ObjectNode object) 450 throws ServiceAccessException, ObjectNotFoundException { 451 452 PreparedStatement statement = null; 453 454 try { 455 456 statement = 458 connection.prepareStatement("delete from objects where uri= ?"); 459 statement.setString(1, object.getUri()); 460 statement.execute(); 461 closeStatement(statement); 462 463 statement = 465 connection.prepareStatement("delete from children where uri=?"); 466 statement.setString(1, object.getUri()); 467 statement.execute(); 468 closeStatement(statement); 469 470 475 476 statement = 478 connection.prepareStatement("delete from links where link= ?"); 479 statement.setString(1, object.getUri()); 480 statement.execute(); 481 closeStatement(statement); 482 483 } catch (SQLException e) { 484 throw createException(e, uri); 485 } 486 } 487 488 494 public void grantPermission(Connection connection, Uri uri, NodePermission permission) 495 throws ServiceAccessException { 496 497 PreparedStatement statement = null; 498 499 try { 500 int inheritable = 0; 501 if (permission.isInheritable()) { 502 inheritable = 1; 503 } 504 505 int negative = 0; 506 if (permission.isNegative()) { 507 negative = 1; 508 } 509 510 NodeRevisionNumber revisionNumber = permission.getRevisionNumber(); 511 String revisionNumberStr = 512 (revisionNumber == null) ? null : revisionNumber.toString(); 513 514 statement = connection.prepareStatement 515 ("insert into permissions values(?,?,?,?,?,?)"); 516 statement.setString(1, permission.getObjectUri()); 517 statement.setString(2, revisionNumberStr); 518 statement.setString(3, permission.getSubjectUri()); 519 statement.setString(4, permission.getActionUri()); 520 statement.setInt(5, inheritable); 521 statement.setInt(6, negative); 522 statement.execute(); 523 } catch (SQLException e) { 524 throw createException(e,uri); 525 } finally { 526 closeStatement(statement); 527 } 528 529 } 530 531 532 538 public void revokePermission(Connection connection,Uri uri, NodePermission permission) 539 throws ServiceAccessException { 540 541 542 543 PreparedStatement statement = null; 544 545 try { 546 NodeRevisionNumber revisionNumber = permission.getRevisionNumber(); 547 if(revisionNumber != null) { 548 statement = connection.prepareStatement 549 ("delete from permissions where object= ? and subject = ? and action = ? and revisionnumber = ? "); 550 statement.setString(4, revisionNumber.toString()); 551 } 552 else { 553 statement = connection.prepareStatement 554 ("delete from permissions where object = ? and subject = ? and action = ? and revisionnumber is NULL"); 555 } 556 557 statement.setString(1, permission.getObjectUri()); 558 statement.setString(2, permission.getSubjectUri()); 559 statement.setString(3, permission.getActionUri()); 560 561 statement.execute(); 562 } catch (SQLException e) { 563 throw createException(e,uri); 564 } finally { 565 closeStatement(statement); 566 } 567 568 } 569 570 571 577 public void revokePermissions(Connection connection,Uri uri) 578 throws ServiceAccessException { 579 580 PreparedStatement statement = null; 581 582 try { 583 584 statement = connection.prepareStatement 585 ("delete from permissions where object= ?"); 586 statement.setString(1, uri.toString()); 587 statement.execute(); 588 } catch (SQLException e) { 589 throw createException(e,uri); 590 } finally { 591 closeStatement(statement); 592 } 593 594 } 595 596 597 603 public Enumeration enumeratePermissions(Connection connection,Uri uri) 604 throws ServiceAccessException { 605 606 Vector permissionVector = new Vector (); 607 PreparedStatement statement = null; 608 609 try { 610 statement = connection.prepareStatement 611 ("select * from permissions where object= ?"); 612 statement.setString(1, uri.toString()); 613 ResultSet res = statement.executeQuery(); 614 615 while (res.next()) { 616 String object = res.getString(PERMISSIONS_OBJECT); 617 String revision = res.getString(PERMISSIONS_REVISION_NUMBER); 618 String subject = res.getString(PERMISSIONS_SUBJECT); 619 String action = res.getString(PERMISSIONS_ACTION); 620 621 boolean inheritable = false; 622 if (res.getInt(PERMISSIONS_INHERITABLE) == 1) { 623 inheritable = true; 624 } 625 boolean negative = false; 626 if (res.getInt(PERMISSIONS_NEGATIVE) == 1) { 627 negative = true; 628 } 629 NodePermission permission = 630 new NodePermission(object,revision,subject, 631 action,inheritable,negative); 632 permissionVector.addElement(permission); 633 } 634 635 } catch (SQLException e) { 636 throw createException(e,uri); 637 } finally { 638 closeStatement(statement); 639 } 640 641 return permissionVector.elements(); 642 } 643 644 650 public void putLock(Connection connection,Uri uri, NodeLock lock) 651 throws ServiceAccessException { 652 653 PreparedStatement statement = null; 654 655 try { 656 int inheritable = 0; 657 if (lock.isInheritable()) { 658 inheritable = 1; 659 } 660 661 int exclusive = 0; 662 if (lock.isExclusive()) { 663 exclusive = 1; 664 } 665 666 statement = connection.prepareStatement 667 ("insert into locks values(?,?,?,?,?,?,?,?)"); 668 statement.setString(1, lock.getLockId()); 669 statement.setString(2, lock.getObjectUri()); 670 statement.setString(3, lock.getSubjectUri()); 671 statement.setString(4, lock.getTypeUri()); 672 statement.setString 673 (5, String.valueOf(lock.getExpirationDate().getTime())); 674 statement.setInt(6,inheritable); 675 statement.setInt(7, exclusive); 676 statement.setString(8,lock.getOwnerInfo()); 677 statement.execute(); 678 } catch (SQLException e) { 679 throw createException(e,uri); 680 } finally { 681 closeStatement(statement); 682 } 683 684 } 685 686 687 694 public void renewLock(Connection connection,Uri uri, NodeLock lock) 695 throws ServiceAccessException, LockTokenNotFoundException { 696 697 PreparedStatement statement = null; 698 699 try { 700 701 int inheritable = 0; 702 if (lock.isInheritable()) { 703 inheritable = 1; 704 } 705 706 int exclusive = 0; 707 if (lock.isExclusive()) { 708 exclusive = 1; 709 } 710 711 statement = connection.prepareStatement 712 ("delete from locks where id=?"); 713 statement.setString(1, lock.getLockId()); 714 statement.execute(); 715 closeStatement(statement); 716 717 statement = connection.prepareStatement 718 ("insert into locks values(?,?,?,?,?,?,?,?)"); 719 statement.setString(1, lock.getLockId()); 720 statement.setString(2, lock.getObjectUri()); 721 statement.setString(3, lock.getSubjectUri()); 722 statement.setString(4, lock.getTypeUri()); 723 statement.setString 724 (5, String.valueOf(lock.getExpirationDate().getTime())); 725 statement.setInt(6, inheritable); 726 statement.setInt(7, exclusive); 727 statement.setString(8,lock.getOwnerInfo()); 728 statement.execute(); 729 730 } catch (SQLException e) { 731 throw createException(e,uri); 732 } finally { 733 closeStatement(statement); 734 } 735 736 } 737 738 739 746 public void removeLock(Connection connection,Uri uri, NodeLock lock) 747 throws ServiceAccessException, LockTokenNotFoundException { 748 749 Statement statement = null; 750 751 try { 752 753 statement = connection.createStatement(); 754 755 String s = null; 756 757 s = "delete from locks where id='" + lock.getLockId() + "'"; 758 statement.execute(s); 759 760 } catch (SQLException e) { 761 throw createException(e,uri); 762 } <
|