| 1 58 59 package org.apache.jetspeed.services.cms.repository.slide; 60 61 import java.lang.reflect.Constructor ; 62 import java.util.Hashtable ; 63 import java.util.Enumeration ; 64 import java.util.Vector ; 65 import java.util.Date ; 66 import java.io.FileWriter ; 67 import java.io.IOException ; 68 import java.sql.*; 69 70 import javax.transaction.xa.XAException ; 71 import javax.transaction.xa.Xid ; 72 73 import org.apache.slide.common.*; 74 import org.apache.slide.store.*; 75 import org.apache.slide.structure.*; 76 import org.apache.slide.security.*; 77 import org.apache.slide.lock.*; 78 import org.apache.slide.content.*; 79 import org.apache.slide.util.logger.Logger; 80 81 import org.apache.jetspeed.services.resources.JetspeedResources; 82 95 96 public class CmsDescriptorsStore 97 extends AbstractSimpleService 98 implements 99 LockStore, 100 NodeStore, 101 RevisionDescriptorsStore, 102 RevisionDescriptorStore, 103 SecurityStore { 104 105 107 109 111 protected static final int OBJECTS_URI = 1; 112 protected static final int OBJECTS_CLASS = 2; 113 114 protected static final int CHILDREN_URI = 1; 115 protected static final int CHILDREN_CHILDURI = 2; 116 117 protected static final int LINKS_LINK = 1; 118 protected static final int LINKS_LINKTO = 2; 119 120 122 protected static final int PERMISSIONS_OBJECT = 1; 123 protected static final int PERMISSIONS_REVISION_NUMBER = 2; 124 protected static final int PERMISSIONS_SUBJECT = 3; 125 protected static final int PERMISSIONS_ACTION = 4; 126 protected static final int PERMISSIONS_INHERITABLE = 5; 127 protected static final int PERMISSIONS_NEGATIVE = 6; 128 129 131 protected static final int LOCKS_ID = 1; 132 protected static final int LOCKS_OBJECT = 2; 133 protected static final int LOCKS_SUBJECT = 3; 134 protected static final int LOCKS_TYPE = 4; 135 protected static final int LOCKS_EXPIRATIONDATE = 5; 136 protected static final int LOCKS_INHERITABLE = 6; 137 protected static final int LOCKS_EXCLUSIVE = 7; 138 139 141 protected static final int REVISIONS_URI = 1; 142 protected static final int REVISIONS_ISVERSIONED = 2; 143 protected static final int REVISIONS_INITIALREVISION = 3; 144 145 protected static final int WORKINGREVISION_URI = 1; 146 protected static final int WORKINGREVISION_BASEREVISION = 2; 147 protected static final int WORKINGREVISION_NUMBER = 3; 148 149 protected static final int LATESTREVISIONS_URI = 1; 150 protected static final int LATESTREVISIONS_BRANCHNAME = 2; 151 protected static final int LATESTREVISIONS_NUMBER = 3; 152 153 protected static final int BRANCHES_URI = 1; 154 protected static final int BRANCHES_NUMBER = 2; 155 protected static final int BRANCHES_CHILDNUMBER = 3; 156 157 protected static final int REVISION_URI = 1; 158 protected static final int REVISION_NUMBER = 2; 159 protected static final int REVISION_BRANCHNAME = 3; 160 161 protected static final int LABEL_URI = 1; 162 protected static final int LABEL_NUMBER = 2; 163 protected static final int LABEL_LABEL = 3; 164 165 protected static final int PROPERTY_URI = 1; 166 protected static final int PROPERTY_NUMBER = 2; 167 protected static final int PROPERTY_NAME = 3; 168 protected static final int PROPERTY_VALUE = 4; 169 protected static final int PROPERTY_NAMESPACE = 5; 170 protected static final int PROPERTY_TYPE = 6; 171 protected static final int PROPERTY_PROTECTED = 7; 172 173 175 178 protected Connection connection; 179 180 183 protected String driver; 184 185 188 protected String url; 189 190 193 protected String user; 194 195 198 protected String password; 199 200 203 protected int jdbcVersion; 204 205 209 protected boolean alreadyEnlisted = false; 210 211 213 216 protected String [] getDatabaseCreateStatements() { 217 String [] statements = 218 { 219 "create table objects(uri varchar(65536) primary key," 220 + " classname varchar(4096))", 221 "create table children(uri varchar(65536), " 222 + " childuri varchar(65536))", 223 "create table links(link varchar(65536), " 224 + " linkto varchar(65536))", 225 "create table permissions(object varchar(65536)," 226 + " revisionnumber varchar(20), " 227 + " subject varchar(65536), action varchar(65536), " 228 + " inheritable int, negative int)", 229 "create table locks(id varchar(65536), object varchar(4096)," 230 + " subject varchar(4096), type varchar(4096), " 231 + " expirationdate varchar(15), inheritable int, " 232 + " xexclusive int)", 233 "create table revisions(uri varchar(65536) primary key, " 234 + " isversioned int, initialrevision varchar(10))", 235 "create table workingrevision(uri varchar(65536), " 236 + " baserevision varchar(20), xnumber varchar(20))", 237 "create table latestrevisions(uri varchar(65536), " 238 + " branchname varchar(4096), xnumber varchar(20))", 239 "create table branches(uri varchar(65536), xnumber varchar(20)," 240 + " childnumber varchar(20))", 241 "create table revision(uri varchar(65536), xnumber varchar(20)," 242 + " branchname varchar(4096))", 243 "create table label(uri varchar(65536), xnumber varchar(20)," 244 + " label varchar(4096))", 245 "create table property(uri varchar(65536), xnumber varchar(20)," 246 + " name varchar(4096), value varchar(65536), " 247 + " namespace varchar(4096), type varchar(100), protected int)" }; 248 249 return statements; 250 } 251 252 260 public void setParameters(Hashtable parameters) 261 throws ServiceParameterErrorException, ServiceParameterMissingException { 262 263 driver = 265 JetspeedResources.getString( 266 "services.CmsService.slide.descriptor.driver"); 267 268 url = 270 JetspeedResources.getString( 271 "services.CmsService.slide.descriptor.url"); 272 273 if (!url.startsWith("jdbc:")) { 277 url = "jdbc:" + url; 278 } 279 281 user = 283 JetspeedResources.getString( 284 "services.CmsService.slide.descriptor.user"); 285 if (user == null) { 286 user = new String (); 287 } 288 289 password = 291 JetspeedResources.getString( 292 "services.CmsService.slide.descriptor.password"); 293 if (password == null) { 294 password = new String (); 295 } 296 297 jdbcVersion = 1; 299 String version = 300 JetspeedResources.getString( 301 "services.CmsService.slide.descriptor.jdbcversion"); 302 if (version != null) { 303 jdbcVersion = (new Integer (version)).intValue(); 304 } 305 } 306 307 313 public synchronized void connect() 314 throws ServiceConnectionFailedException { 315 getLogger().log( 316 "Connecting to \"" + url + "\" as user \"" + user + "\"", 317 LOG_CHANNEL, 318 Logger.INFO); 319 try { 320 connection = DriverManager.getConnection(url, user, password); 321 } catch (SQLException e) { 322 getLogger().log( 323 "Connecting to \"" + url + "\" as user \"" + user + "\" failed", 324 LOG_CHANNEL, 325 Logger.ERROR); 326 getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 327 throw new ServiceConnectionFailedException(this, e); 328 } 329 330 try { 332 connection.setAutoCommit(true); 333 } catch (SQLException e) { 334 } 335 353 alreadyEnlisted = false; 355 } 356 357 363 public void disconnect() throws ServiceDisconnectionFailedException { 364 getLogger().log( 365 "Disconnecting from \"" + url + "\" as user \"" + user + "\"", 366 LOG_CHANNEL, 367 Logger.INFO); 368 try { 369 if (connection != null) { 370 connection.close(); 371 } 372 connection = null; 373 } catch (SQLException e) { 374 getLogger().log( 375 "Disconnecting from \"" 376 + url 377 + "\" as user \"" 378 + user 379 + "\" failed", 380 LOG_CHANNEL, 381 Logger.ERROR); 382 getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 383 throw new ServiceDisconnectionFailedException(this, e); 384 } 385 } 386 387 399 public synchronized void initialize(NamespaceAccessToken token) 400 throws ServiceInitializationFailedException { 401 try { 402 token.getLogger().log( 404 "Loading and registering driver: " + driver, 405 LOG_CHANNEL, 406 Logger.INFO); 407 Class driverClass = Class.forName(driver); 408 Driver databaseDriver = (Driver) driverClass.newInstance(); 409 DriverManager.registerDriver(databaseDriver); 410 } catch (ClassNotFoundException e) { 411 token.getLogger().log( 412 "Loading and registering driver " + driver + " failed", 413 LOG_CHANNEL, 414 Logger.ERROR); 415 token.getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 416 throw new ServiceInitializationFailedException( 417 this, 418 e.getMessage()); 419 } catch (InstantiationException e) { 420 token.getLogger().log( 421 "Loading and registering driver " + driver + " failed", 422 LOG_CHANNEL, 423 Logger.ERROR); 424 token.getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 425 throw new ServiceInitializationFailedException( 426 this, 427 e.getMessage()); 428 } catch (IllegalAccessException e) { 429 token.getLogger().log( 430 "Loading and registering driver " + driver + " failed", 431 LOG_CHANNEL, 432 Logger.ERROR); 433 token.getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 434 throw new ServiceInitializationFailedException( 435 this, 436 e.getMessage()); 437 } catch (SQLException e) { 438 token.getLogger().log( 439 "Loading and registering driver " + driver + " failed", 440 LOG_CHANNEL, 441 Logger.ERROR); 442 token.getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 443 throw new ServiceInitializationFailedException( 444 this, 445 e.getMessage()); 446 } catch (ClassCastException e) { 447 token.getLogger().log( 448 "Loading and registering driver " + driver + " failed", 449 LOG_CHANNEL, 450 Logger.ERROR); 451 token.getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 452 throw new ServiceInitializationFailedException( 453 this, 454 e.getMessage()); 455 } catch (Exception e) { 456 token.getLogger().log( 457 "Loading and registering driver " + driver + " failed", 458 LOG_CHANNEL, 459 Logger.ERROR); 460 token.getLogger().log(e.toString(), LOG_CHANNEL, Logger.ERROR); 461 throw new ServiceInitializationFailedException( 462 this, 463 e.getMessage()); 464 } 465 } 466 467 472 public synchronized void reset() throws ServiceResetFailedException { 473 Statement statement = null; 474 531 } 532 533 539 public boolean isConnected() throws ServiceAccessException { 540 try { 541 return ((connection != null) && (!connection.isClosed())); 542 } catch (SQLException e) { 543 throw new ServiceAccessException(this, e); 544 } 545 } 546 547 549 552 public void commit(Xid xid, boolean onePhase) throws XAException { 553 super.commit(xid, onePhase); 554 555 try { 556 connection.commit(); 558 } catch (SQLException e) { 559 throw new XAException (XAException.XA_RBCOMMFAIL); 560 } 561 alreadyEnlisted = false; 562 } 563 564 568 public void rollback(Xid xid) throws XAException { 569 super.rollback(xid); 570 571 try { 572 connection.rollback(); 574 } catch (SQLException e) { 575 throw new XAException (XAException.XA_HEURCOM); 576 } 577 alreadyEnlisted = false; 578 } 579 580 583 public void start(Xid xid, int flags) throws XAException { 584 super.start(xid, flags); 585 if (!alreadyEnlisted) { 586 try { 587 connection.rollback(); 590 } catch (SQLException e) { 591 throw new XAException (XAException.XAER_RMERR); 592 } 593 alreadyEnlisted = true; 594 } 595 } 596 597 599 606 public ObjectNode retrieveObject(Uri uri) 607 throws ServiceAccessException, ObjectNotFoundException { 608 609 ObjectNode result = null; 610 PreparedStatement statement = null; 611 612 try { 613 614 statement = 615 connection.prepareStatement( 616 "select * from objects where uri= ?"); 617 statement.setString(1, uri.toString()); 618 619 ResultSet res = statement.executeQuery(); 620 621 623 String className; 624 625 if (res.next()) { 626 className = res.getString(OBJECTS_CLASS); 628 } else { 629 throw new ObjectNotFoundException(uri); 631 } 632 633 closeStatement(statement); 634 635 statement = 637 connection.prepareStatement( 638 "select * from children where uri= ?"); 639 statement.setString(1, uri.toString()); 640 res = statement.executeQuery(); 641 642 Vector childrenVector = new Vector (); 643 644 while (res.next()) { 646 childrenVector.addElement(res.getString(CHILDREN_CHILDURI)); 648 } 649 closeStatement(statement); 650 651 statement = 652 connection.prepareStatement( 653 "select * from links where linkto= ?"); 654 statement.setString(1, uri.toString()); 655 res = statement.executeQuery(); 656 657 Vector linksVector = new Vector (); 658 659 while (res.next()) { 661 linksVector.addElement(res.getString(LINKS_LINKTO)); 663 } 664 665 closeStatement(statement); 666 667 if (className.equals("org.apache.slide.structure.LinkNode")) { 668 669 String linkTo = new String (); 670 statement = 671 connection.prepareStatement( 672 "select * from links where link= ?"); 673 statement.setString(1, uri.toString()); 674 res = statement.executeQuery(); 675 676 if (res.next()) { 677 linkTo = res.getString(LINKS_LINKTO); 678 } 679 680 closeStatement(statement); 681 682 result = 683 new LinkNode( 684 uri.toString(), 685 childrenVector, 686 linksVector, 687 linkTo); 688 689 } else { 690 691 try { 692 Class objclass = Class.forName(className); 693 694 Class [] argClasses = 695 { 696 Class.forName("java.lang.String"), 697 Class.forName("java.util.Vector"), 698 Class.forName("java.util.Vector")}; 699 Object [] arguments = 700 { uri.toString(), childrenVector, linksVector }; 701 702 Constructor constructor = 703 objclass.getConstructor(argClasses); 704 result = (ObjectNode) constructor.newInstance(arguments); 705 } catch (Exception e) { 706 throw new ServiceAccessException(this, e); 708 } 709 } 710 } catch (SQLException e) { 711 getLogger().log(e, LOG_CHANNEL, Logger.ERROR); 712 throw new ServiceAccessException(this, e); 713 } finally { 714 closeStatement(statement); 715 } 716 return result; 717 } 718 719 726 public void storeObject(Uri uri, ObjectNode object) 727 throws ServiceAccessException, ObjectNotFoundException { 728 729 PreparedStatement statement = null; 730 731 try { 732 statement = 733 connection.prepareStatement( 734 "select * from objects where uri= ?"); 735 statement.setString(1, uri.toString()); 736 737 ResultSet res = statement.executeQuery(); 738 739 741 if (!res.next()) { 742 throw new ObjectNotFoundException(uri); 743 } 744 745 closeStatement(statement); 746 747 statement = 749 connection.prepareStatement( 750 "delete from children where uri= ?"); 751 statement.setString(1, object.getUri()); 752 statement.execute(); 753 closeStatement(statement); 754 755 statement = null; 756 Enumeration children = object.enumerateChildren(); 757 while (children.hasMoreElements()) { 758 if (statement == null) { 759 statement = 760 connection.prepareStatement( 761 "insert into children values(?, ?)"); 762 } 763 statement.setString(1, object.getUri()); 764 statement.setString(2, (String ) children.nextElement()); 765 statement.execute(); 766 } 767 closeStatement(statement); 768 769 781 782 statement = 784 connection.prepareStatement("delete from links where link= ?"); 785 statement.setString(1, object.getUri()); 786 statement.execute(); 787 closeStatement(statement); 788 789 if (object instanceof LinkNode) { 790 statement = 791 connection.prepareStatement( 792 "insert into links values(?,?)"); 793 statement.setString(1, object.getUri()); 794 statement.setString(2, ((LinkNode) object).getLinkedUri()); 795 statement.execute(); 796 closeStatement(statement); 797 } 798 } catch (SQLException e) { 799 getLogger().log(e, LOG_CHANNEL, Logger.ERROR); 800 throw new ServiceAccessException(this, e); 801 } finally { 802 closeStatement(statement); 803 } 804 } 805 806 815 public void createObject(Uri uri, ObjectNode object) 816 throws ServiceAccessException, ObjectAlreadyExistsException { 817 818 PreparedStatement statement = null; 819 820 try { 821 822 String className = object.getClass().getName(); 823 824 statement = 825 connection.prepareStatement( 826 "select * from objects where uri= ?"); 827 statement.setString(1, uri.toString()); 828 829 ResultSet res = statement.executeQuery(); 830 831 833 if (res.next()) { 834 throw new ObjectAlreadyExistsException(uri.toString()); 835 } 836 837 closeStatement(statement); 838 839 statement = 840 connection.prepareStatement("insert into objects values(?,?)"); 841 statement.setString(1, uri.toString()); 842 &
|